import { usePage } from '@inertiajs/vue3';

function toNormalCase(value: string): string {
    return value
        .replace(/[._-]+/g, ' ')
        .replace(/\s+/g, ' ')
        .trim()
        .toLowerCase()
        .replace(/\b\w/g, (char) => char.toUpperCase());
}

export function trans(key: string, replacements: Record<string, string> = {}): string {
    const page = usePage();
    const translations = page.props.translations as Record<string, string> || {};

    let translation = translations[key] || toNormalCase(key);

    Object.keys(replacements).forEach(replacement => {
        translation = translation.replace(`:${replacement}`, replacements[replacement]);
    });

    return translation;
}

export const wTrans = trans;
