"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.filterCustomPluginsAllowedForSpecificFile = filterCustomPluginsAllowedForSpecificFile;
exports.filterPluginsAllowedForSpecificFile = filterPluginsAllowedForSpecificFile;
exports.getEnabledPlugins = getEnabledPlugins;
exports.mapDefaultSupportedPlugins = mapDefaultSupportedPlugins;

var _checkIsPatternsMatchFilename = require("../bundler/webpack/loaderConfigs/checkIsPatternsMatchFilename");

var _getSpecificPostCssPlugin = require("./getSpecificPostCssPlugin");

/* eslint-disable no-use-before-define */
function mapDefaultSupportedPlugins(pluginsOptions) {
  return _getSpecificPostCssPlugin.supportedPluginsOptionNames.map(pluginName => ({ ...pluginsOptions[pluginName],
    pluginName
  }));
}

function getEnabledPlugins(pluginsOptions) {
  function isEnabled(pluginName) {
    return pluginsOptions[pluginName].enable;
  }

  return _getSpecificPostCssPlugin.supportedPluginsOptionNames.filter(isEnabled).map(pluginName => ({ ...pluginsOptions[pluginName],
    pluginName
  }));
}

function filterPluginsAllowedForSpecificFile(plugins, filename) {
  return plugins.filter(pluginOption => checkIsPluginAllowed(pluginOption, filename)).map(pluginOption => (0, _getSpecificPostCssPlugin.getSpecificPostCssPlugin)(pluginOption)).filter(Boolean);
}

function checkIsPluginAllowed(pluginOption, filename) {
  return (0, _checkIsPatternsMatchFilename.checkIsPatternsMatchFilename)(pluginOption.patterns, filename);
}

function getCustomPlugins(pluginOption) {
  if (typeof pluginOption.plugin === 'string') {
    // eslint-disable-next-line import/no-dynamic-require, global-require
    return require(pluginOption.plugin).plugin(pluginOption);
  }

  if (typeof pluginOption.plugin === 'function') {
    return pluginOption.plugin(pluginOption);
  }

  return null;
}

function filterCustomPluginsAllowedForSpecificFile(customPlugins, filename) {
  return customPlugins.filter(pluginOption => checkIsPluginAllowed(pluginOption, filename)).map(getCustomPlugins).filter(Boolean);
}