'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var types = require('../types-4940caff.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);

function parseOneReplaceConfig(config) {
  if (typeof config === 'string') {
    return {
      sourceName: config,
      sourceType: types.IImportType.ImportSpecifier,
      targetName: config,
      targetType: types.IImportType.ImportSpecifier
    };
  }
  if (Array.isArray(config)) {
    return {
      sourceName: config[0],
      sourceType: types.IImportType.ImportSpecifier,
      targetName: config[1] || config[0],
      targetType: types.IImportType.ImportSpecifier
    };
  }
  return {
    sourceName: config.sourceName || '',
    sourceType: config.sourceType || types.IImportType.ImportSpecifier,
    targetName: config.targetName || config.sourceName || '',
    targetType: config.targetType || config.sourceType || types.IImportType.ImportSpecifier
  };
}
/**
 * 解析替换配置
 *
 * @param {Array<IReplaceConfig>} configList 配置列表
 * @returns {array} 处理后的配置列表
 *
 * @example
 * ```ts
 * parseReplaceConfig([{
 *   source: '',
 *   target: '',
 * }])
 * ```
 */
function parseReplaceConfig(configList) {
  var result = configList.reduce(function (acc, item) {
    var importedList = item.importedList,
      source = item.source,
      target = item.target;
    var newSource = Array.isArray(source) ? source : [source];
    var list = importedList.map(function (item) {
      return Object.assign({
        source: newSource,
        target: target
      }, parseOneReplaceConfig(item));
    });
    if (!importedList.length) {
      acc.push(Object.assign(Object.assign({}, item), {
        source: newSource,
        sourceName: 'FAKE',
        targetName: 'FAKE',
        sourceType: types.IImportType.FAKE,
        targetType: types.IImportType.FAKE
      }));
    } else {
      acc.push.apply(acc, _toConsumableArray__default["default"](list));
    }
    return acc;
  }, []);
  var newResult = result.reduce(function (acc, item) {
    var source = item.source;
    var list = source.map(function (sourceItem) {
      return Object.assign(Object.assign({}, item), {
        source: sourceItem
      });
    });
    acc.push.apply(acc, _toConsumableArray__default["default"](list));
    return acc;
  }, []);
  return newResult;
}

exports.parseReplaceConfig = parseReplaceConfig;
