"use strict";

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

var _constants = require("./constants");

var _readI18nValues = require("./readI18nValues");

/* eslint-disable no-use-before-define */
function optionsHandler(options) {
  const {
    disableDefault,
    jsResource,
    propertiesFolder
  } = options;
  const {
    jsResourceI18nKeys,
    allI18nObject,
    locales
  } = (0, _readI18nValues.readI18nValues)({
    jsResource,
    propertiesFolder,
    disableDefault
  });
  return {
    filename: options.filename || _constants.DEFAULT_FILENAME,
    chunkFilename: options.chunkFilename || chunkNameDecider(options.filename),
    templateLabel: options.templateLabel,
    publicPath: options.publicPath,
    disableDefault: options.disableDefault,
    i18nManifestFileName: options.i18nManifestFileName,
    jsonpFunc: options.jsonpFunc,
    localeVarName: options.localeVarName,
    jsResource: options.jsResource,
    propertiesFolder: options.propertiesFolder,
    jsResourceI18nKeys,
    allI18nObject,
    locales,
    // template: (object, locale) => `window.loadI18n(${JSON.stringify(object)}, ${JSON.stringify(locale)})`,
    runtime: true,
    runtimeOptions: {
      insert: options.insert,
      scriptType: typeof options.scriptType === 'boolean' && options.scriptType === true || typeof options.scriptType === 'undefined' ? 'text/javascript' : options.scriptType,
      attributes: options.attributes
    }
  };
}

function chunkNameDecider(filename) {
  if (typeof filename !== 'function') {
    const hasName = filename.includes('[name]');
    const hasId = filename.includes('[id]');
    const hasChunkHash = filename.includes('[chunkhash]');
    const hasContentHash = filename.includes('[contenthash]');

    if (hasChunkHash || hasContentHash || hasName || hasId) {
      return filename;
    }

    return filename.replace(/(^|\/)([^/]*(?:\?|$))/, '$1[id].$2');
  }

  return _constants.DEFAULT_CHUNK_FILENAME;
}