
export function titlecase(str: string) {
    return str.replaceAll(/-/g, " ").replaceAll(/_/g, " ").split(" ").map(word => {
        if (word == "and" || word == "or" || word == "the" || word == "in" || word == "on" || word == "at" || word == "to" || word == "for" || word == "but" || word == "is" || word == "of") return word;
        return word.charAt(0).toUpperCase() + word.slice(1);
    }).join(" ");
}

export function typeToTitle(type: string | null | undefined) {
    if (!type) return "";
    const partsOfSpeech = ["noun", "verb", "adjective", "adverb"];
    const maybePlural = partsOfSpeech.includes(type.toLowerCase()) ? "s" : "";
    return titlecase(type!) + maybePlural;
}

export function dateToTitle(dateStr: string) {
    const date = new Date(dateStr + 'T00:00:00Z'); // Parse as UTC
    if (isNaN(date.getTime())) return dateStr; // Invalid date, return as is

    const options: Intl.DateTimeFormatOptions = { year: 'numeric', month: 'long', day: 'numeric', timeZone: 'UTC' };
    return date.toLocaleDateString('en-US', options);
}