"use strict";

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

var _helperCompilationTargets = _interopRequireDefault(require("@babel/helper-compilation-targets"));

var _logger = require("../../logger");

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

/* eslint-disable no-use-before-define */
const supportedBrowserVersions = ['last 3 Chrome versions', 'last 3 Firefox versions', 'last 3 Edge versions', 'last 3 Safari versions'];

function customBabelPlugins(babelPlugins) {
  return babelPlugins.map(p => p).filter(Boolean);
}

function babelWebConfig(options, mode) {
  const {
    plugins: babelPlugins,
    browserList
  } = options.babelCustomizations;
  return {
    presets: [getBabelPresetEnvConfig(browserList, mode), require.resolve('@babel/preset-react')],
    plugins: customBabelPlugins(babelPlugins)
  };
}

function getBabelPresetEnvConfig(browserList, mode) {
  if (mode === 'lib') {
    return require.resolve('@babel/preset-env');
  }

  const browsers = browserList || supportedBrowserVersions;
  (0, _logger.verboseLogger)('getTargets', (0, _helperCompilationTargets.default)({
    browsers
  }));
  return [require.resolve('@babel/preset-env'), {
    modules: false,
    useBuiltIns: 'usage',
    corejs: 3,
    targets: {
      browsers
    }
  }];
}