"use strict";

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

var _htmlWebpackPlugin = _interopRequireDefault(require("html-webpack-plugin"));

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

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

// const HtmlWebpackPlugin = require('html-webpack-plugin');
const pluginName = 'OverwriteCssPathForRTL'; // const pluginName = 'rtl-css-transform-webpack-plugin';
// if we choose file name as style.ltr.css and style.rtl.css it  will be easy for html template
// this  plugin code copy from https://github.com/nicolashemonic/rtl-css-transform-webpack-plugin

class OverwriteCssPathForRTL {
  constructor(options = {
    dirVarName: 'document.dir'
  }) {
    this.dirVarName = options.dirVarName || 'document.dir';
    this.templateLabel = options.templateLabel || '{{--dir}}';
  }

  overwriteCssPathinHTML(compilation) {
    _htmlWebpackPlugin.default.getHooks(compilation).beforeAssetTagGeneration.tapAsync(pluginName, (data, cb) => {
      let {
        assets
      } = data;
      cb(null, Object.assign({}, data, {
        assets: Object.assign({}, assets, {
          css: assets.css && (0, _replaceCssDirTemplate.replaceCssDirTemplateMapper)(assets.css, this.templateLabel)
        })
      }));
    });
  }

  overwriteCssPathinRequireEnsure(compilation) {
    // NOTE: if you goig to change an thing here you may check  with EFCPlugin, ServiceWorkerPlugin
    const {
      mainTemplate
    } = compilation; // TODO: must verify when we move to webpack5 ay be  we need to change this
    // TODO it's may not be perfect solution, but it will work
    // mainTemplate.hooks.assetPath.tap(pluginName, (path, data) => {

    mainTemplate.hooks.assetPath.tap(pluginName, filePath => {
      if (!filePath.endsWith('.css"')) {
        return filePath;
      }

      const ENDS = '.ltr.css"'; // if  we need total differend temlate for  file name  it will be too hard

      const templateText = `"+(${this.dirVarName} === "rtl" ? ".rtl": ".ltr")+".css"`; // const templateText = `"+(${this.dirVarName} === "rtl" ? ".rtl": "")+".css"`;
      // NOTE:
      // we don't use rtlFilename as full replace because it may increace runtime chunk file size
      //  they replace [chunkhash] with object full chunk hash so it may dub
      // const templateText = `(${this.dirVarName} === "rtl" ? ${JSON.stringify(this.rtlFilename)}: ${filePath})`;
      // const templateText = '."+(document.dir === "rtl" ? ".rtl": "")+".css"';
      // File  path going to  be like  below
      //  '"style.css"' ==>> `"style."+document.dir+".css"`
      //  '"style.css"' ==>> `"style."+(document.dir === "rtl" ? ".rtl": "")+".css"`
      // replace like this
      //  '.ltr.css"' ==>> `."+(document.dir === "rtl" ? ".rtl": ".ltr")+".css"`

      return filePath.replace(ENDS, templateText);
    });
  }

  apply(compiler) {
    compiler.hooks.thisCompilation.tap(pluginName, compilation => {
      this.overwriteCssPathinHTML(compilation);
      this.overwriteCssPathinRequireEnsure(compilation);
    });
  }

}

exports.default = OverwriteCssPathForRTL;