"use strict";

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

var _Template = _interopRequireDefault(require("webpack/lib/Template"));

var _htmlWebpackInjectAttributesPlugin = _interopRequireDefault(require("html-webpack-inject-attributes-plugin"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* eslint-disable no-use-before-define */
class CustomAttributePlugin {
  constructor(options) {
    this.options = {
      attributes: options.attributes,
      useHTMLAttribute: options.attributes && !options.jsAttributes && !options.cssAttributes && !options.i18nAttributes,
      jsAttributes: options.jsAttributes || options.attributes || {},
      cssAttributes: options.cssAttributes || options.attributes || {},
      i18nAttributes: options.i18nAttributes || options.attributes || {}
    };
  }

  addCustomAttributeToDynamicScriptTags(mainTemplate) {
    const {
      jsAttributes,
      cssAttributes,
      i18nAttributes
    } = this.options; // NOTE: To add custom Attributes to js Script tags

    mainTemplate.hooks.jsonpScript.tap('CustomAttributePlugin', source => {
      const str = attributeSetTemplate(jsAttributes, 'script');
      return _Template.default.asString([source, ...str]);
    }); // NOTE: To add custom Attributes to css link tags

    mainTemplate.hooks.requireEnsure.tap('CustomAttributePlugin', source => {
      const str = attributeSetTemplate(cssAttributes, 'linkTag');
      const replacedStr = source.replace('head.appendChild(linkTag);', `${str.join('')}head.appendChild(linkTag);`); //   console.log({ s: source, r: replacedStr });

      return replacedStr;
    }); // NOTE: To add custom Attributes to i18n split js Script tags

    mainTemplate.hooks.requireEnsure.tap('CustomAttributePlugin', source => {
      const str = attributeSetTemplate(i18nAttributes, 'scriptTag');
      const replacedStr = source.replace('document.body.appendChild(scriptTag);', `${str.join('')}document.body.appendChild(scriptTag);`); //   console.log({ s: source, r: replacedStr });

      return replacedStr;
    });
  }

  apply(compiler) {
    // NOTE: we not using this, Reason currently this option is only need for EFC,
    // So it do not needed.
    const {
      attributes,
      useHTMLAttribute
    } = this.options;
    useHTMLAttribute && new _htmlWebpackInjectAttributesPlugin.default(attributes).apply(compiler);
    compiler.hooks.thisCompilation.tap({
      name: 'CustomAttributePlugin',
      stage: 1,
      fn: compilation => {
        this.addCustomAttributeToDynamicScriptTags(compilation.mainTemplate);
      }
    });
  }

}

exports.CustomAttributePlugin = CustomAttributePlugin;

function attributeSetTemplate(jsAttributes, variableName) {
  let str = [];
  Object.keys(jsAttributes).forEach(key => {
    const val = jsAttributes[key];
    str.push(`${variableName}.setAttribute(${JSON.stringify(key)}, ${JSON.stringify(val)});`);
  }); //   console.log({ str });

  return str;
}