import { updateLanguagesInDayjsConfigurationTask } from '../../client/support/update-languages.js';
import { generateLanguagesWebappOptions } from '../../languages/support/index.js';
function generateDateTimeFormat(language, index, length) {
    let config = `  '${language}': {\n`;
    config += '    short: {\n';
    config += "      year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric'\n";
    config += '    },\n';
    config += '    medium: {\n';
    config += "      year: 'numeric', month: 'short', day: 'numeric',\n";
    config += "      weekday: 'short', hour: 'numeric', minute: 'numeric'\n";
    config += '    },\n';
    config += '    long: {\n';
    config += "      year: 'numeric', month: 'long', day: 'numeric',\n";
    config += "      weekday: 'long', hour: 'numeric', minute: 'numeric'\n";
    config += '    }\n';
    config += '  }';
    if (index !== length - 1) {
        config += ',';
    }
    config += '\n';
    return config;
}
function updateLanguagesInPipeTask({ application, control = {} }) {
    const { clientSrcDir, languagesDefinition = [] } = application;
    const { ignoreNeedlesError: ignoreNonExisting } = control;
    const newContent = `$1{
  ${generateLanguagesWebappOptions(languagesDefinition).join(',\n  ')},
  // jhipster-needle-i18n-language-key-pipe - JHipster will add/remove languages in this object
    }`;
    this.editFile(`${clientSrcDir}app/shared/config/languages.ts`, { ignoreNonExisting }, content => content.replace(/(languages =.*)\{([^\]]*jhipster-needle-i18n-language-key-pipe[^}]*)}/g, newContent));
}
function updateLanguagesInConfigTask({ application, control = {} }) {
    const { clientSrcDir, languages } = application;
    const { ignoreNeedlesError: ignoreNonExisting } = control;
    let i18nConfig = 'const datetimeFormats: IntlDateTimeFormats = {\n';
    languages?.forEach((ln, i) => {
        i18nConfig += generateDateTimeFormat(ln, i, languages.length);
    });
    i18nConfig += '  // jhipster-needle-i18n-language-date-time-format - JHipster will add/remove format options in this object\n';
    i18nConfig += '}';
    this.editFile(`${clientSrcDir}app/shared/config/config.ts`, { ignoreNonExisting }, content => content.replace(/const datetimeFormats.*\{([^\]]*jhipster-needle-i18n-language-date-time-format[^}]*)}/g, i18nConfig));
}
function updateLanguagesInWebpackTask({ application, control = {} }) {
    const { clientSrcDir, languages } = application;
    const { ignoreNeedlesError: ignoreNonExisting } = control;
    let newContent = 'groupBy: [\n';
    languages?.forEach(language => {
        newContent += `          { pattern: './${clientSrcDir}i18n/${language}/*.json', fileName: './i18n/${language}.json' },\n`;
    });
    newContent += '          // jhipster-needle-i18n-language-webpack - JHipster will add/remove languages in this array\n        ]';
    this.editFile('webpack/webpack.common.js', { ignoreNonExisting }, content => content.replace(/groupBy:.*\[([^\]]*jhipster-needle-i18n-language-webpack[^\]]*)\]/g, newContent));
}
export default function updateLanguagesTask(taskParam) {
    updateLanguagesInPipeTask.call(this, taskParam);
    updateLanguagesInConfigTask.call(this, taskParam);
    if (taskParam.application.clientBundlerWebpack) {
        updateLanguagesInWebpackTask.call(this, taskParam);
    }
    updateLanguagesInDayjsConfigurationTask.call(this, taskParam, {
        configurationFile: `${taskParam.application.clientSrcDir}app/shared/config/dayjs.ts`,
        commonjs: true,
    });
}
