/**
 * Copyright 2013-2026 the original author or authors from the JHipster project.
 *
 * This file is part of the JHipster project, see https://www.jhipster.tech/
 * for more information.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * Converts xx-yy to xx_yy.
 * @param language
 */
export function languageSnakeCase(language) {
    return language.replace(/-/g, '_');
}
/**
 * Converts language country to upper case.
 * @example
 * // returns xx-YY
 * languageUpperCaseCountry('xx-yy');
 * @example
 * // returns xx-Latn-YY
 * languageUpperCaseCountry('xx-Latn-yy');
 * @example
 * // returns xx-Latn-YY-modifier
 * languageUpperCaseCountry('xx-Latn-yy-modifier');
 * @param language
 */
function languageUpperCaseCountry(language) {
    const split = splitLanguageTag(language);
    split.country = split.country ? split.country.toUpperCase() : split.country;
    return buildLanguageTag(split);
}
/**
 * Convert a language tag to java locale tag.
 * @param language
 */
export function languageToJavaLanguage(language) {
    return languageSnakeCase(languageUpperCaseCountry(language));
}
/**
 * Convert a language tag to faker exported name.
 */
export function languageToFakerLanguage(language) {
    return languageSnakeCase(languageUpperCaseCountry(language));
}
const languageToFakerjsLanguage = (languageTag) => languageUpperCaseCountry(languageTag);
const languageToAngularLanguage = (languageTag) => languageUpperCaseCountry(languageTag);
const languageToDayjsLanguage = (languageTag) => {
    const split = splitLanguageTag(languageTag);
    split.script = split.script ? split.script.toLowerCase() : split.script;
    return buildLanguageTag(split);
};
/**
 * Languages specifications, defaults will be applied to build the supportedLanguages
 */
const partialLanguages = [
    {
        name: 'Albanian',
        displayName: 'Shqip',
        languageTag: 'al',
        dayjsLocale: 'sq',
        angularLocale: 'sq',
    },
    {
        name: 'Arabic (Libya)',
        displayName: 'العربية',
        languageTag: 'ar-ly',
        rtl: true,
    },
    {
        name: 'Armenian',
        displayName: 'Հայերեն',
        languageTag: 'hy',
        dayjsLocale: 'hy-am',
    },
    {
        name: 'Azerbaijani (Latin)',
        displayName: 'Azərbaycan dili',
        languageTag: 'az-Latn-az',
        angularLocale: 'az-Latn',
        dayjsLocale: 'az',
    },
    {
        name: 'Belarusian',
        displayName: 'Беларускі',
        languageTag: 'by',
        dayjsLocale: 'be',
        angularLocale: 'be',
    },
    {
        name: 'Bengali',
        displayName: 'বাংলা',
        languageTag: 'bn',
        dayjsLocale: 'bn',
    },
    { name: 'Bulgarian', displayName: 'Български', languageTag: 'bg' },
    {
        name: 'Catalan',
        displayName: 'Català',
        languageTag: 'ca',
    },
    {
        name: 'Chinese (Simplified)',
        displayName: '中文（简体）',
        languageTag: 'zh-cn',
        angularLocale: 'zh-Hans',
    },
    {
        name: 'Chinese (Traditional)',
        displayName: '繁體中文',
        languageTag: 'zh-tw',
        angularLocale: 'zh-Hant',
    },
    { name: 'Croatian', displayName: 'Hrvatski', languageTag: 'hr' },
    { name: 'Czech', displayName: 'Český', languageTag: 'cs' },
    { name: 'Danish', displayName: 'Dansk', languageTag: 'da' },
    { name: 'Dutch', displayName: 'Nederlands', languageTag: 'nl' },
    { name: 'English', displayName: 'English', languageTag: 'en' },
    { name: 'Estonian', displayName: 'Eesti', languageTag: 'et' },
    {
        name: 'Farsi',
        displayName: 'فارسی',
        languageTag: 'fa',
        rtl: true,
    },
    { name: 'Finnish', displayName: 'Suomi', languageTag: 'fi' },
    { name: 'French', displayName: 'Français', languageTag: 'fr' },
    { name: 'Galician', displayName: 'Galego', languageTag: 'gl' },
    { name: 'German', displayName: 'Deutsch', languageTag: 'de' },
    { name: 'Greek', displayName: 'Ελληνικά', languageTag: 'el' },
    { name: 'Hebrew', displayName: 'עברית', languageTag: 'he', rtl: true },
    { name: 'Hindi', displayName: 'हिंदी', languageTag: 'hi' },
    { name: 'Hungarian', displayName: 'Magyar', languageTag: 'hu' },
    { name: 'Indonesian', displayName: 'Bahasa Indonesia', languageTag: 'id' },
    { name: 'Italian', displayName: 'Italiano', languageTag: 'it' },
    { name: 'Japanese', displayName: '日本語', languageTag: 'ja' },
    { name: 'Korean', displayName: '한국어', languageTag: 'ko' },
    { name: 'Marathi', displayName: 'मराठी', languageTag: 'mr' },
    { name: 'Myanmar', displayName: 'မြန်မာ', languageTag: 'my' },
    { name: 'Polish', displayName: 'Polski', languageTag: 'pl' },
    {
        name: 'Portuguese (Brazilian)',
        displayName: 'Português (Brasil)',
        languageTag: 'pt-br',
        angularLocale: 'pt',
    },
    {
        name: 'Portuguese',
        displayName: 'Português',
        languageTag: 'pt-pt',
        dayjsLocale: 'pt',
    },
    {
        name: 'Punjabi',
        displayName: 'ਪੰਜਾਬੀ',
        languageTag: 'pa',
        dayjsLocale: 'pa-in',
    },
    { name: 'Romanian', displayName: 'Română', languageTag: 'ro' },
    { name: 'Russian', displayName: 'Русский', languageTag: 'ru' },
    { name: 'Slovak', displayName: 'Slovenský', languageTag: 'sk' },
    { name: 'Serbian', displayName: 'Srpski', languageTag: 'sr' },
    { name: 'Sinhala', displayName: 'සිංහල', languageTag: 'si' },
    { name: 'Spanish', displayName: 'Español', languageTag: 'es' },
    { name: 'Swedish', displayName: 'Svenska', languageTag: 'sv' },
    { name: 'Turkish', displayName: 'Türkçe', languageTag: 'tr' },
    { name: 'Tamil', displayName: 'தமிழ்', languageTag: 'ta' },
    { name: 'Telugu', displayName: 'తెలుగు', languageTag: 'te' },
    { name: 'Thai', displayName: 'ไทย', languageTag: 'th' },
    {
        name: 'Ukrainian',
        displayName: 'Українська',
        languageTag: 'ua',
        angularLocale: 'uk',
        dayjsLocale: 'uk',
    },
    {
        name: 'Uzbek (Cyrillic)',
        displayName: 'Ўзбекча',
        languageTag: 'uz-Cyrl-uz',
        angularLocale: 'uz-Cyrl',
        dayjsLocale: 'uz',
    },
    {
        name: 'Uzbek (Latin)',
        displayName: 'O`zbekcha',
        languageTag: 'uz-Latn-uz',
        angularLocale: 'uz-Latn',
        dayjsLocale: 'uz-latn',
    },
    { name: 'Vietnamese', displayName: 'Tiếng Việt', languageTag: 'vi' },
    {
        name: 'Karakalpak (Latin)',
        displayName: 'Qaraqalpaqsha',
        languageTag: 'kr-Latn-kr',
        angularLocale: 'en',
        dayjsLocale: 'en',
    },
];
export const supportedLanguages = partialLanguages.map(language => Object.freeze({
    rtl: false,
    javaLocaleMessageSourceSuffix: languageToJavaLanguage(language.languageTag),
    angularLocale: languageToAngularLanguage(language.languageTag),
    dayjsLocale: languageToDayjsLanguage(language.languageTag),
    fakerjsLocale: languageToFakerjsLanguage(language.languageTag),
    ...language,
}));
export const findLanguageForTag = (languageTag, languages = supportedLanguages) => languages.find(lang => lang.languageTag === languageTag);
export const defaultLanguage = findLanguageForTag('en');
/**
 * Generate language objects in array of "'en': { name: 'English' }" format
 * @param languages
 */
export const generateLanguagesWebappOptions = (languages) => languages.map(language => `'${language.languageTag}': { name: '${language.displayName}'${language.rtl ? ', rtl: true' : ''} },`);
/**
 * Split a language tag.
 * @param languageTag
 */
function splitLanguageTag(languageTag) {
    const [language, ...split] = languageTag.split('-');
    if (split.length === 0) {
        return { language };
    }
    let script;
    if (split[0].length > 2) {
        script = split.shift();
    }
    const country = split.shift();
    const variant = split.shift();
    return { language, script, country, variant };
}
/**
 * Build a language tag.
 * @param parts
 */
function buildLanguageTag(parts) {
    const { language, script, country, variant } = parts;
    let languageTag = language;
    if (script) {
        languageTag = languageTag.concat('-', script);
    }
    if (country) {
        languageTag = languageTag.concat('-', country);
    }
    if (variant) {
        languageTag = languageTag.concat('-', variant);
    }
    return languageTag;
}
export function languagesAsChoices(languages = supportedLanguages) {
    return languages.map(language => ({ value: language.languageTag, name: language.name }));
}
