--
-- * Case insensitive name lookups
--

-- | Does a case-insensitive search by package name.
--
-- If there is only one package that compares case-insensitively to this name
-- then the search is unambiguous and we get back all versions of that package.
-- If several match case-insensitively but one matches exactly then it is also
-- unambiguous.
--
-- If however several match case-insensitively and none match exactly then we
-- have an ambiguous result, and we get back all the versions of all the
-- packages. The list of ambiguous results is split by exact package name. So
-- it is a non-empty list of non-empty lists.
--
searchByName :: PackageIndex pkg
             -> String -> [(PackageName, [pkg])]
searchByName (PackageIndex m) name =
    [ pkgs
    | pkgs@(PackageName name',_) <- Map.toList m
    , lowercase name' == lname ]
  where
    lname = lowercase name
searchByName :: PackageIndex pkg
             -> String -> [(PackageName, [pkg])]
searchByName (PackageIndex m) name =
    [ pkgs
    | pkgs@(PackageName name',_) <- Map.toList m
    , lowercase name' == lname ]
  where
    lname = lowercase name
searchByName (PackageIndex m) name =
    [ pkgs
    | pkgs@(PackageName name',_) <- Map.toList m
    , lowercase name' == lname ]
  where
    lname = lowercase name