"use strict";

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

var _ignore = _interopRequireDefault(require("ignore"));

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

const path = require('path');

const aliasNames = {
  valueReplacer: 'postcss-value-replacer',
  selectorReplace: 'postcss-selector-replace-new',
  hasRTL: 'postcss-rtl',
  combinerMediaQuery: 'postcss-combine-media-query',
  hoverActive: 'postcss-mobile-hover',
  cssVariableReplacement: 'postcss-variable-report',
  composeMinification: 'postcss-compose-plugin'
};
/*

unique scenario

*/
// export function filterFileCssUniqueness(filename, filterObj) {
//   let rootDir = 'supportapp';
//   let include = false;
//   const regex = `^(.+?)${rootDir}?\\\\`;
//   const newFilename = windowsModificationFile(filename).replace(
//     new RegExp(regex, 'gi'),
//     ''
//   );
//   Object.keys(filterObj).forEach(key => {
//     const ig = ignore().add(filterObj[key]);
//     if (ig.ignores(newFilename)) {
//       include = true;
//     }
//   });
//   return include;
// }

function isFileNameMatchingPattern({
  filename,
  filterObject,
  plugins,
  order
}) {
  const pluginObj = {};
  plugins.forEach(p => {
    pluginObj[p.postcssPlugin] = p;
  });
  const finalPlugins = order.filter(key => {
    const pluginSpecificFilter = filterObject[key];

    if (!pluginSpecificFilter || pluginSpecificFilter.length === 0) {
      return true;
    } // eslint-disable-next-line no-use-before-define


    const isMatching = isFilePathMatchingPattern(filename, pluginSpecificFilter);
    return isMatching;
  }).map(key => {
    const p = pluginObj[aliasNames[key]];
    return p;
  }); // if unknown key given in plugins and pattern loop will run but post-css plugin won't be available and will be undefined in array
  // So we do filter as safety measure.

  return finalPlugins.filter(Boolean);
}

function isFilePathMatchingPattern(filePath, patterns) {
  if (patterns.length === 0) {
    return true;
  }

  const ig = (0, _ignore.default)({
    allowRelativePaths: true
  }).add(patterns);
  return ig.ignores(filePath);
}

function isFileNameMatchingPluginPattern({
  filename,
  filterArr
}) {
  const newFilename = path.relative(path.parse(process.cwd()).base, filename);
  return isFilePathMatchingPattern(newFilename, filterArr);
}