"use strict";

var _object = _interopRequireDefault(require("object.omit"));

var _deepmerge = _interopRequireDefault(require("deepmerge"));

var _core = require("@babel/core");

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function arrayMerge(source = [], overrides = []) {
  return [...new Set([...source, ...overrides])];
}

function mergeArray(source = [], overrides = [], resolve, deepmergeOpts) {
  return [...source, ...overrides].reduce((reduction, override) => {
    const overrideName = resolve(Array.isArray(override) ? override[0] : override);
    const overrideOptions = Array.isArray(override) ? override[1] : {};
    const base = reduction.find(base => {
      const baseName = resolve(Array.isArray(base) ? base[0] : base);
      return baseName === overrideName || baseName.includes(overrideName);
    });
    const index = reduction.includes(base) ? reduction.indexOf(base) : reduction.length;
    const baseName = base ? resolve(Array.isArray(base) ? base[0] : base) : overrideName;
    const baseOptions = Array.isArray(base) ? base[1] : {};
    const options = (0, _deepmerge.default)(baseOptions, overrideOptions, _objectSpread({
      arrayMerge,
      isMergeableObject: value => Array.isArray(value)
    }, deepmergeOpts));
    reduction[index] = Object.keys(options).length ? [baseName, options] : baseName;
    return reduction;
  }, []);
}

function babelMerge(source = {}, overrides = {}, deepmergeOpts) {
  const plugins = mergeArray(source.plugins, overrides.plugins, _core.resolvePlugin, deepmergeOpts);
  const presets = mergeArray(source.presets, overrides.presets, _core.resolvePreset, deepmergeOpts);
  const sourceEnv = source.env || {};
  const overridesEnv = overrides.env || {};
  return Object.assign(presets.length ? {
    presets
  } : {}, plugins.length ? {
    plugins
  } : {}, _deepmerge.default.all([(0, _object.default)(source, ['plugins', 'presets', 'env']), (0, _object.default)(overrides, ['plugins', 'presets', 'env']), ...[...new Set([...Object.keys(sourceEnv), ...Object.keys(overridesEnv)])].map(name => ({
    env: {
      [name]: babelMerge(sourceEnv[name], overridesEnv[name], deepmergeOpts)
    }
  }))], _objectSpread({
    arrayMerge
  }, deepmergeOpts)));
}

Object.defineProperty(babelMerge, 'all', {
  value: (values = [], deepmergeOpts) => values.reduce((acc, value) => {
    if (value) {
      Object.assign(acc, babelMerge(acc, value, deepmergeOpts));
    }

    return acc;
  }, {})
});
module.exports = babelMerge;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 7388 characters]