import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { I as IImportType } from '../types-f29b26c8.js';

function parseOneReplaceConfig(config) {
  if (typeof config === 'string') {
    return {
      sourceName: config,
      sourceType: IImportType.ImportSpecifier,
      targetName: config,
      targetType: IImportType.ImportSpecifier
    };
  }
  if (Array.isArray(config)) {
    return {
      sourceName: config[0],
      sourceType: IImportType.ImportSpecifier,
      targetName: config[1] || config[0],
      targetType: IImportType.ImportSpecifier
    };
  }
  return {
    sourceName: config.sourceName || '',
    sourceType: config.sourceType || IImportType.ImportSpecifier,
    targetName: config.targetName || config.sourceName || '',
    targetType: config.targetType || config.sourceType || 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: IImportType.FAKE,
        targetType: IImportType.FAKE
      }));
    } else {
      acc.push.apply(acc, _toConsumableArray(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(list));
    return acc;
  }, []);
  return newResult;
}

export { parseReplaceConfig };
