import { ReactNode } from "react";
import {NameItem} from "../../api/models/Types.tsx";
import { GetName } from "../../api/CommonPropertiesHelper.tsx";

export type TitleNames = {
    titleElement: ReactNode | null
    // titleString: string
    namesUsed: Array<NameItem>,
};

export function GetTitleNames(names: Array<NameItem> | null | undefined, primaryLanguageId?: string | null | undefined, emptyNotFoundValue?: boolean | null | undefined) {
    const titleNames = GetName(names, primaryLanguageId);

    if (titleNames != null){
        if (titleNames.nameInUserLanguage != null){
            if (titleNames.nameOriginal != null){
                return {
                    titleElement: (
                        <>
                            <span className="user-select-all">{titleNames.nameInUserLanguage}</span> / <span className="user-select-all">{titleNames.nameOriginal}</span> [{titleNames.languageCodeIso3}]
                        </>
                    ),
                    namesUsed: titleNames.namesUsed,
                } as TitleNames;
            }
            else{
                return {
                    titleElement: (
                        <>
                            <span className="user-select-all">{titleNames.nameInUserLanguage}</span>
                        </>
                    ),
                    namesUsed: titleNames.namesUsed,
                } as TitleNames
            }
        }
        else if (titleNames.nameOriginal != null){
            return {
                titleElement: (
                    <>
                        <span className="user-select-all">{titleNames.nameOriginal}</span> [{titleNames.languageCodeIso3}]
                    </>
                ),
                namesUsed: titleNames.namesUsed,
            } as TitleNames
        }
    }

    return {
        titleElement: (
            <span>{emptyNotFoundValue ? '' : 'NO NAME'}</span>
        ),
        namesUsed: [],
    } as TitleNames
}