"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.cssDirTemplate = void 0;
exports.getInitialAssetsFuncTemplate = getInitialAssetsFuncTemplate;
exports.localeTemplate = void 0;

var _utils = require("../utils");

var _replaceCssDirTemplate = require("./RtlSplitPlugin/replaceCssDirTemplate");

var _getInitialI18nAssetsArrayStr = require("./getInitialI18nAssetsArrayStr");

const cssDirTemplate = '@dir@';
exports.cssDirTemplate = cssDirTemplate;
const localeTemplate = '@locale@';
exports.localeTemplate = localeTemplate;

function getInitialAssetsFuncTemplate({
  entryPoint,
  enableRTLSplit,
  compilation,
  i18nStore,
  chunkSplitEnable,
  i18nFileNameTemplate,
  functionName = 'getInitialAssets'
}) {
  const initialFiles = entryPoint.getFiles();
  let initialJsFiles = initialFiles.filter(_utils.isJsFile);
  let initialCssFiles = initialFiles.filter(_utils.isCssFile);

  if (enableRTLSplit) {
    initialCssFiles = (0, _replaceCssDirTemplate.replaceCssDirTemplateMapper)(initialCssFiles, cssDirTemplate);
  }

  if (chunkSplitEnable) {
    initialJsFiles = (0, _replaceCssDirTemplate.replaceI18nLocaleTemplateMapper)(initialJsFiles, localeTemplate);
  }

  const initialI18nAssets = (0, _getInitialI18nAssetsArrayStr.getInitialI18nAssetsArrayStr)({
    entryPoint,
    compilation,
    i18nStore,
    chunkSplitEnable,
    i18nFileNameTemplate
  });
  const initialAssetsFuncTemplate = `function ${functionName}({ lang, dir}) {
    return {
      js: ${JSON.stringify(initialJsFiles)}.map(urlPath => urlPath.replace(/@locale@/g, lang)),
      css: ${JSON.stringify(initialCssFiles)}${enableRTLSplit ? '.map(urlPath => urlPath.replace(/@dir@/g, dir))' : ''},
      i18n: ${initialI18nAssets},
    }
  }`;
  return initialAssetsFuncTemplate;
}