"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.templateFileName = templateFileName;

var _templateHashHelpers = require("./templateHashHelpers");

function templateFileName({
  compiler,
  compilation,
  dynamicFilename,
  locale,
  chunks,
  contentHashes
}) {
  const {
    webpack
  } = compiler;
  const {
    Template,
    RuntimeGlobals
  } = webpack;
  const MODULE_TYPE = 'text/mini-i18n';
  const {
    mapExpr,
    mapExprWithLength
  } = (0, _templateHashHelpers.templateHashHelpers)(chunks); // "i18n-chunk/[locale]/[name].[chunkhash].js"

  const i18nURLPathTemplate = compilation.getPath(JSON.stringify(dynamicFilename).replace(/\[locale\]/gi, '"+ locale +"'), {
    hash: `" + ${RuntimeGlobals.getFullHash}() + "`,
    hashWithLength: length => `" + ${RuntimeGlobals.getFullHash}().slice(0, ${length}) + "`,
    chunk: {
      id: '" + chunkId + "',
      hash: mapExpr(c => c.renderedHash),
      hashWithLength: mapExprWithLength(c => c.renderedHash),
      name: mapExpr(c => c.name || c.id),
      contentHash: {
        [MODULE_TYPE]: mapExpr(c => contentHashes[c.id])
      },
      contentHashWithLength: {
        [MODULE_TYPE]: mapExprWithLength(c => contentHashes[c.id])
      }
    },
    contentHashType: MODULE_TYPE
  });
  return Template.asString([`var locale = ${JSON.stringify(locale)};`, `${RuntimeGlobals.require}.miniI18nF = function(chunkId){return (${i18nURLPathTemplate})}`]);
}