"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isI18nFile = void 0;
exports.nameTemplates = nameTemplates;

var _modeUtils = require("./modeUtils");

// nameTemplate.js
// NOTE: these below code can be done in js,
//   we did copy paste in order to avoid unnecessary confusion
// NOTE: array[0] is dev mode file templates and array[1] is prod mode file templates
const defaultTemplates = {
  html: ['index.html', 'index.[contenthash].html'],
  js: ['js/[name].js', 'js/[name].[contenthash]_.js'],
  chunkjs: ['js-chunk/[name].js', 'js-chunks/[name].[contenthash]_.js'],
  i18njs: ['i18n-chunk/[locale]/[name].i18n.js', 'i18n-chunk/[locale]/[name].[chunkhash]_.i18n.js'],
  i18nchunkjs: ['i18n-chunk/[locale]/[name].i18n.js', // 'i18n-chunk/[locale]/[name].[chunkhash].i18n.js'
  'i18n-chunk/[locale]/[name].[contenthash]_.i18n.js'],
  i18nmanifest: ['i18n-manifest.json', 'i18n-manifest.json'],
  workerjs: ['js/[name].js', 'js/[name].[contenthash].js'],
  css: ['css/[name].css', 'css/[name].[contenthash]_.css'],

  /**
   * NOTE: when you change (cssdir) this line it  will have affect on Below files will be affected
   *   custom_plugins/RtlSplitPlugin/*
   *   pluginConfigs/configMiniCSSExtractPlugin.js
   *   pluginConfigs/configRtlCssPlugin.js
   *  */
  cssdir: ['css/[name].[dir].css', 'css/[name].[contenthash]_.[dir].css'],
  image: ['images/[name][ext]', 'images/[name].[contenthash][ext]'],
  font: ['fonts/[name][ext]', 'fonts/[name].[contenthash][ext]'],
  svg: ['fonts/[name][ext]', 'fonts/[name].[contenthash][ext]'],
  audio: ['fonts/[name][ext]', 'fonts/[name].[contenthash][ext]'],
  video: ['videos/[name][ext]', 'videos/[name].[contenthash][ext]']
};

function createNameTemplate(name, useHash) {
  // const ext = `${useHash ? '.[hash:20]' : ''}[ext]`;
  return useHash ? name[1] : name[0];
}

const defaultDevModeContentHashAllowedTypes = {
  image: true,
  font: true,
  svg: true,
  audio: true,
  video: true
};

function nameTemplates(type, options) {
  const {
    enableFileNameHashing,
    mode,
    devLikeHash,
    nameTemplateCustomization
  } = options;
  let {
    devModeContentHashAllowedTypes
  } = options;
  devModeContentHashAllowedTypes = devModeContentHashAllowedTypes || defaultDevModeContentHashAllowedTypes;
  const templates = { ...defaultTemplates,
    ...nameTemplateCustomization
  };
  const templateName = templates[type];

  if (!templateName) {
    throw Error(`specified type not valid Type: ${type}`);
  } // eslint-disable-next-line no-use-before-define


  const useHash = checkCanWeAllowHash({
    enableFileNameHashing,
    devLikeHash: devLikeHash || (0, _modeUtils.isDevelopmentMode)(mode),
    type,
    devModeContentHashAllowedTypes
  });
  return createNameTemplate(templateName, useHash);
}

function checkCanWeAllowHash({
  enableFileNameHashing,
  devLikeHash,
  type,
  devModeContentHashAllowedTypes
}) {
  if (!enableFileNameHashing) {
    return false;
  }

  if (devLikeHash) {
    return devModeContentHashAllowedTypes[type];
  }

  return true;
} // export function cssDirNameTemplate(dir, options) {
//   return nameTemplates('cssdir', options).replace(/\[dir\]/g, dir);
// }


const isI18nFile = url => url.endsWith('.i18n.js');

exports.isI18nFile = isI18nFile;