class SassC::ImportHandler::FileImporter

def dir_exist?(path)

def dir_exist?(path)
  File.exist?(path) && File.directory?(path)
end

def exactly_one(paths)

def exactly_one(paths)
  return if paths.empty?
  return paths.first if paths.length == 1
  raise "It's not clear which file to import. Found:\n#{paths.map { |path| "  #{path}" }.join("\n")}"
end

def file_exist?(path)

def file_exist?(path)
  File.exist?(path) && File.file?(path)
end

def resolve_path(path, from_import)

def resolve_path(path, from_import)
  ext = File.extname(path)
  if ['.sass', '.scss', '.css'].include?(ext)
    if from_import
      result = exactly_one(try_path("#{without_ext(path)}.import#{ext}"))
      return result unless result.nil?
    end
    return exactly_one(try_path(path))
  end
  unless ext.empty?
    if from_import
      result = exactly_one(try_path("#{without_ext(path)}.import#{ext}"))
      return result unless result.nil?
    end
    result = exactly_one(try_path(path))
    return result unless result.nil?
  end
  if from_import
    result = exactly_one(try_path_with_ext("#{path}.import"))
    return result unless result.nil?
  end
  result = exactly_one(try_path_with_ext(path))
  return result unless result.nil?
  try_path_as_dir(path, from_import)
end

def try_path(path)

def try_path(path)
  partial = File.join(File.dirname(path), "_#{File.basename(path)}")
  result = []
  result.push(partial) if file_exist?(partial)
  result.push(path) if file_exist?(path)
  result
end

def try_path_as_dir(path, from_import)

def try_path_as_dir(path, from_import)
  return unless dir_exist? path
  if from_import
    result = exactly_one(try_path_with_ext(File.join(path, 'index.import')))
    return result unless result.nil?
  end
  exactly_one(try_path_with_ext(File.join(path, 'index')))
end

def try_path_with_ext(path)

def try_path_with_ext(path)
  result = try_path("#{path}.sass") + try_path("#{path}.scss")
  result.empty? ? try_path("#{path}.css") : result
end

def without_ext(path)

def without_ext(path)
  ext = File.extname(path)
  path.delete_suffix(ext)
end