module Config.Helpers.Converters exposing
    ( formatName
    , formatSocial
    , toTitleCase
    , wordCount
    )


formatName : String -> String
formatName name =
    name
        |> String.toLower
        |> String.replace " " ""
        |> String.replace "'" ""
        |> String.replace "." ""
        |> String.replace "-" ""
        |> String.replace "_" ""


formatSocial : String -> String
formatSocial name =
    name
        |> String.replace "https://x.com/" "@"
        |> String.replace "https://www.threads.net/@" "@"
        |> String.replace "https://bsky.app/profile/" "@"
        |> String.replace "https://www.instagram.com/" "@"
        |> String.replace "https://www.youtube.com/" "@"


wordCount : String -> Int
wordCount text =
    text
        |> String.words
        |> List.length


toTitleCase : String -> String
toTitleCase input =
    let
        lowercaseWords =
            [ "a", "an", "the", "and", "but", "or", "for", "nor", "on", "at", "to", "in", "of", "with", "by" ]

        words =
            String.words input

        capitalizeFirst word =
            case String.uncons word of
                Just ( first, rest ) ->
                    String.toUpper (String.fromChar first) ++ String.toLower rest

                Nothing ->
                    ""

        capitalizeWord index word =
            if index == 0 then
                capitalizeFirst word

            else if not (List.member (String.toLower word) lowercaseWords) then
                capitalizeFirst word

            else
                String.toLower word
    in
    words
        |> List.indexedMap capitalizeWord
        |> String.join " "
