import {NameItem, NameType} from "./models/Types.tsx";

export type CommonPropertiesName = {
    nameInUserLanguage: string | null,
    languageCodeIso3: string | null,
    nameOriginal: string | null,
    namesUsed: Array<NameItem>,
}

export function GetName(names: Array<NameItem> | null | undefined, primaryLanguageId?: string | null | undefined) : CommonPropertiesName | null {
    if (names != null) {
        const nameOriginal = names!.find(q => q.type == NameType.Original)
        if ((primaryLanguageId == null || primaryLanguageId == '') || nameOriginal?.language.id == primaryLanguageId) {
            if (nameOriginal != null) {
                // namesUsed.push(nameOriginal);
                // titleNames.titleString = `${nameOriginal.value} [${nameOriginal.language.codeIso3}]`;
                return GetCommonPropertiesName(null, nameOriginal);
            }
        }
        else {
            let result = null as CommonPropertiesName | null;
            const namesUsed = new Array<NameItem>();

            if (nameOriginal != null) {
                namesUsed.push(nameOriginal);
                // titleNames.titleString = `${nameOriginal.value} [${nameOriginal.language.codeIso3}]`;
                result = GetCommonPropertiesName(null, nameOriginal);
            }

            const originalNameInAnotherLanguage = names!.find(q => q.type == NameType.OriginalInAnotherLanguage && q.language.id == primaryLanguageId);
            if (originalNameInAnotherLanguage != null) {
                namesUsed.push(originalNameInAnotherLanguage);
                // titleNames.titleString = `${originalNameInAnotherLanguage.value}${nameOriginal != null ? ` / ${titleNames.titleString}` : ''}`;
                result = GetCommonPropertiesName(originalNameInAnotherLanguage, nameOriginal ?? null);
            }
            else {
                const translatedName = names!.find(q => q.type == NameType.Translation && q.language.id == primaryLanguageId);
                if (translatedName != null) {
                    namesUsed.push(translatedName);
                    result = GetCommonPropertiesName(translatedName, nameOriginal ?? null);
                }
            }
            
            if (result != null){
                result.namesUsed = namesUsed;   
            }

            return result;
        }
    }

    return null;
}

function GetCommonPropertiesName(nameInUserLanguage?: NameItem | null, nameOriginal?: NameItem | null) : CommonPropertiesName | null {
    if (nameInUserLanguage != null){
        if (nameOriginal != null){
            return {
                nameInUserLanguage: nameInUserLanguage.value,
                nameOriginal: nameOriginal.value,
                languageCodeIso3: nameOriginal!.language.codeIso3,
            } as CommonPropertiesName;
            // return `${nameInUserLanguage.value} / ${nameOriginal.value} [${nameOriginal!.language.codeIso3}]`;
        }
        else{
            return {
                nameInUserLanguage: nameInUserLanguage.value,
                nameOriginal: null,
                languageCodeIso3: null,
            } as CommonPropertiesName;
        }
    }
    else if (nameOriginal != null){
        return {
            nameInUserLanguage: null,
            nameOriginal: nameOriginal.value,
            languageCodeIso3: nameOriginal!.language.codeIso3,
        } as CommonPropertiesName;
        // return (
        // 	<>
        // 		<span className="user-select-all">{nameOriginal.value}</span> [{nameOriginal!.language.codeIso3}]
        // 	</>
        // )
    }
    else{
        return null;
        // return (
        // 	<span>NO NAME</span>
        // )
    }
}