"use strict";

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

var _path = _interopRequireDefault(require("path"));

var deprecationHandlers = _interopRequireWildcard(require("../deprecationLogger"));

var _windowsModification = require("../loaderUtils/windowsModification");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

let {
  deprecateMessage,
  deprecateOption,
  deprecationLoggerEnd
} = deprecationHandlers;

function modifyCssArr(arr) {
  return arr.map(x => {
    const includeCss = !x.startsWith('!');
    const modifier = includeCss ? '/*.css' : '';
    const filePath = x.replace(/^!/, ''); // Remove the "!" symbol if present

    return (0, _windowsModification.windowsModificationFile)(_path.default.join(includeCss ? '**' : '!**', filePath, '**', modifier));
  });
}

const defaultPostCssPluginOptions = {
  valueReplacer: null,
  hasRTL: false,
  selectorReplace: null,
  hoverActive: false,
  combinerMediaQuery: false,
  cssVariableReplacement: false,
  selectorWeight: false,
  minifier: false,
  composeMinification: false
};

function deprecationSupport(options, disableDeprecationWarning) {
  if (disableDeprecationWarning) {
    /* eslint-disable no-empty-function */
    deprecateMessage = () => {};

    deprecateOption = () => {};

    deprecationLoggerEnd = () => {};
    /* eslint-enable no-empty-function */

  }

  options.app.plugins = options.app.plugins || defaultPostCssPluginOptions;
  options.docs.plugins = options.docs.plugins || defaultPostCssPluginOptions;
  options.css.plugins = options.css.plugins || defaultPostCssPluginOptions; // if (selectn(options, ".app.hasRTL") === true) {

  if (options.app.hasRTL === true) {
    deprecateOption('app.hasRTL', 'app.plugins.hasRTL');
    options.app.plugins.hasRTL = true;
  }

  if (options.docs.hasRTL === true) {
    deprecateOption('docs.hasRTL', 'docs.plugins.hasRTL');
    options.docs.plugins.hasRTL = true;
  }

  if (options.app.selectorReplace !== null && options.app.plugins && (options.app.plugins.selectorReplace === null || options.app.plugins.selectorReplace === undefined)) {
    deprecateMessage('when you use app.selectorReplace mention enable or disable app.plugins.selectorReplace by setting value to true or false');
    options.app.plugins.selectorReplace = true;
  }

  if (options.docs.selectorReplace !== null && options.docs.plugins && (options.docs.plugins.selectorReplace === null || options.docs.plugins.selectorReplace === undefined)) {
    deprecateMessage('when you use docs.selectorReplace mention enable or disable docs.plugins.selectorReplace by setting value to true or false');
    options.docs.plugins.selectorReplace = true;
  }

  if (options.css.valueReplacer !== null && options.css.plugins && (options.app.plugins.valueReplacer === null || options.app.plugins.valueReplacer === undefined)) {
    deprecateMessage('when you use app.valueReplacer mention enable or disable app.plugins.valueReplacer by setting value to true or false');
    options.app.plugins.valueReplacer = true;
  }

  if (!options.app.patterns) {
    options.app.patterns = {};
  }

  function handleRTL(rtlExclude, rtl, hasRTL, type) {
    if (rtlExclude && rtlExclude.length > 0) {
      options[type].patterns.hasRTL = modifyCssArr(rtlExclude);
      deprecateOption(`${type}.rtlExclude`, `${type}.patterns.hasRTL`);
    }

    if (rtl && rtl.length > 0) {
      options[type].patterns.hasRTL = modifyCssArr(rtl);
      deprecateOption(`${type}.exclude.rtl`, `${type}.patterns.hasRTL`);
    }

    if (hasRTL && hasRTL.length > 0) {
      options[type].patterns.hasRTL = modifyCssArr(hasRTL);
      deprecateOption(`${type}.exclude.hasRTL`, `${type}.patterns.hasRTL`);
    }
  }

  function addExcludesToPattern(patterns, exclude, type) {
    Object.keys(defaultPostCssPluginOptions).forEach(key => {
      if (!exclude[key] || exclude[key].length === 0) {
        return;
      }

      if (patterns[key].length === 0) {
        deprecateOption(`${type}.exclude.${key}`, `${type}.patterns.${key}`);
        let tempArr = exclude[key];
        tempArr = tempArr.map(x => _path.default.join('!**', x, '**')); // Since patterns[key] is empty we need to wildcard for allow all others.

        tempArr.unshift('*');
        patterns[key] = tempArr;
      } else if (exclude[key].length > 0 && patterns[key].length > 0) {
        deprecateOption(`${type}.exclude.${key}`, `${type}.patterns.${key}`, ` And Since you have used ${type}.patterns.${key} we won't use ${type}.exclude.${key}. So please make appropriate changes`);
      }
    });
  }

  handleRTL(options.app.rtlExclude, options.app.exclude.rtl, options.app.exclude.hasRTL, 'app');
  handleRTL(options.docs.rtlExclude, options.docs.exclude.rtl, options.docs.exclude.hasRTL, 'docs');
  addExcludesToPattern(options.app.patterns, options.app.exclude, 'app');
  addExcludesToPattern(options.docs.patterns, options.docs.exclude, 'docs');
  deprecationLoggerEnd();
}