"use strict";

exports.__esModule = true;
exports.alterDataSourceList = alterDataSourceList;
exports.getApiParams = getApiParams;
exports.getApiParamsByDataSourceId = getApiParamsByDataSourceId;
exports.getApiParamsByMainAndApiList = getApiParamsByMainAndApiList;
exports.getCreateDataSourceItem = getCreateDataSourceItem;
exports.getDataSourceDataById = getDataSourceDataById;
exports.getInitDrawerConfig = getInitDrawerConfig;
exports.getOptionsByDataSourceList = getOptionsByDataSourceList;
exports.isChangeApiParams = isChangeApiParams;
exports.updatedApiParamsByMainDataSource = updatedApiParamsByMainDataSource;
exports.updatedApiParamsDataMain = updatedApiParamsDataMain;
var _lowcodeEngine = require("@alilc/lowcode-engine");
var _lodash = require("lodash");
var _formKeyType = require("../../../_utils/formKeyType");
var _utils = require("../../../_utils/utils");
var _type = require("../../../../src/utils/pactList/type");
function getDataSourceList() {
  var result = [];
  var schema = _lowcodeEngine.project.exportSchema();
  var dataSource = Object.assign({}, schema.componentsTree[0].dataSource);
  if (dataSource && dataSource.list !== undefined) {
    result = (0, _lodash.cloneDeep)(dataSource.list);
  }
  return result;
}
/**
 * 
 * @returns 
 */
function getOptionsByDataSourceList() {
  var result = [];
  var dataSourceList = getDataSourceList();
  result = dataSourceList.map(function (item) {
    return {
      label: item.options.dataSourceName,
      value: item.id
    };
  });
  return result;
}

/**
 * 每次点击都重新修正数据源配置 
 * @param list 
 * @returns 
 */
function updatedApiParamsByMainDataSource(list) {
  var result = [];
  var dataSourceList = getDataSourceList();
  var dataSourceObj = dataSourceList === null || dataSourceList === void 0 ? void 0 : dataSourceList.reduce(function (pre, cur) {
    var _cur$options, _cur$options$dataSour;
    pre[cur.id] = (_cur$options = cur.options) === null || _cur$options === void 0 ? void 0 : (_cur$options$dataSour = _cur$options.dataSource) === null || _cur$options$dataSour === void 0 ? void 0 : _cur$options$dataSour.apiParams;
    return pre;
  }, {});
  list.forEach(function (item) {
    var mainApiList = dataSourceObj[item.dataSourceId];
    if (mainApiList) {
      item.apiParams = getApiParamsByMainAndApiList(mainApiList, item.apiParams);
    }
    result.push(item);
  });
  return result;
}

/**
 * 做一个统一的方法去获取最新的apiParams数据
 * 有改变直接使用数据源里面的转换数据模块，如果没有改变则使用自身模块
 * @param mainApiList 
 * @param apiList 
 * @returns 
 */
function getApiParamsByMainAndApiList(mainApiList, apiList) {
  var res = isChangeApiParams(mainApiList, apiList);
  return res ? getApiParams(mainApiList) : updatedApiParamsDataMain(mainApiList, apiList);
}

/**
 * 同步固定范围数据，固定不可修改的以maiApiList为主
 * @param mainApiList 
 * @param apiList 
 * @returns 
 */
function updatedApiParamsDataMain(mainApiList, apiList) {
  var result = apiList;
  var apiListObj = apiList.reduce(function (pre, cur) {
    pre[cur.id] = cur;
    return pre;
  }, {});
  result = mainApiList.map(function (item) {
    // 只处理选中模块存在的
    var oldApiItem = apiListObj[item.id];
    if (oldApiItem) {
      // 主等于物料数据时，以辅为主
      if (item.formKey === _formKeyType.DataSourceType.materielType) {
        item = apiListObj[item.id];
      } else if ((0, _lodash.isArray)(item.data) && item.data.length && (item.type === _type.PactNameType.tupleList || item.type === _type.PactNameType.tuple)) {
        var mainApiItemData = item.data;
        var apiListItemData = (0, _utils.compatibleTupleData)(oldApiItem.data);
        if (apiListItemData.originType === _formKeyType.TupleOriginType["default"] && apiListItemData.defaultData) {
          var newData = apiListItemData.defaultData;
          var len = mainApiItemData.length;
          for (var i = 0; i < len; i++) {
            newData[i] = updatedApiParamsDataMain(mainApiItemData[i], newData[i]);
          }
          item.data = newData;
        } else if (apiListItemData.originType === _formKeyType.TupleOriginType.customData && apiListItemData.customData) {
          item.data = apiListItemData;
        }
      }
    }
    return item;
  });
  return result;
}

/**
 * 但凡只要存在数量和key值不一致则就说明主辅不一致了
 * @param mainApiList 
 * @param apiList 
 */
function isChangeApiParams(mainApiList, apiList) {
  var result = false;

  // 数量不同终止
  if (mainApiList.length !== apiList.length) {
    return true;
  }
  for (var i = 0; i < mainApiList.length; i++) {
    var mainItem = mainApiList[i];
    var apiItem = apiList[i];
    // 顺序name,type,formKey 不同终止
    if (mainItem.name !== apiItem.name || mainItem.type !== apiItem.type || mainItem.formKey !== _formKeyType.DataSourceType.materielType && mainItem.formKey !== apiItem.formKey) {
      result = true;
      break;
    }
    if (mainItem.type === apiItem.type && (mainItem.type === _type.PactNameType.tupleList || mainItem.type === _type.PactNameType.tuple) && (0, _lodash.isArray)(mainItem.data)) {
      var apiItemData = (0, _utils.compatibleTupleData)(apiItem.data);
      if (apiItemData.originType === _formKeyType.TupleOriginType["default"] && apiItemData.defaultData) {
        var res = isChangeApiParamsTupleList(mainItem.data, apiItemData.defaultData, mainItem.type);
        if (res) {
          result = true;
          break;
        }
      }
    }
  }
  return result;
}
function isChangeApiParamsTupleList(mainApiList, apiList, type) {
  var result = false;
  if (mainApiList.length !== apiList.length) {
    return true;
  }
  if (type === _type.PactNameType.tupleList) {
    for (var i = 0; i < mainApiList.length; i++) {
      var mainItem = mainApiList[i];
      var apiItem = apiList[i];
      var res = isChangeApiParams(mainItem, apiItem);
      if (res) {
        result = true;
        break;
      }
    }
  } else {
    var _res = isChangeApiParams(mainApiList, apiList);
    if (_res) {
      result = true;
    }
  }
  return result;
}

// function updatedApiParamsListMain(mainApiList: RequestParamMappingDataType[][], apiList: RequestParamMappingDataType[][]) {
//     let result = apiList

//     let len = mainApiList.length
//     for (let i = 0; i < len; i++) {
//         result[i] = updatedApiParamsDataMain(mainApiList[i], apiList[i])
//     }
//     return result
// }

/**
 * 通过id获取id的信息
 * @param id 
 * @returns 
 */
function getDataSourceDataById(id) {
  var result = null;
  var dataSourceList = getDataSourceList();
  var res = dataSourceList.filter(function (item) {
    return item.id === id;
  });
  if (res.length) {
    var _data$options, _data$options$dataSou, _data$options2, _data$options2$dataSo;
    var data = res[0];
    result = {
      data: data,
      selectValue: data === null || data === void 0 ? void 0 : (_data$options = data.options) === null || _data$options === void 0 ? void 0 : (_data$options$dataSou = _data$options.dataSource) === null || _data$options$dataSou === void 0 ? void 0 : _data$options$dataSou.selectValue,
      pactAddress: data === null || data === void 0 ? void 0 : (_data$options2 = data.options) === null || _data$options2 === void 0 ? void 0 : (_data$options2$dataSo = _data$options2.dataSource) === null || _data$options2$dataSo === void 0 ? void 0 : _data$options2$dataSo.pactAddress
    };
  }
  return result;
}

/**
 * 根据选中id从schema中获取apiParams
 * @param id 
 * @returns 
 */
function getApiParamsByDataSourceId(id) {
  var result = [];
  var dataSourceList = getDataSourceList();
  var dFilter = dataSourceList.filter(function (item) {
    return item.id === id;
  });
  if (dFilter.length) {
    var apiParams = dFilter[0].options.dataSource.apiParams;
    if (apiParams) {
      result = apiParams;
    }
  }
  return result;
}

/**
 * 存在一些接口需要物料自身自己配置，将状态变更
 * @param list 
 * @returns 
 */
function getApiParams(list) {
  var newList = (0, _lodash.isArray)(list) ? list : [list];
  newList = (0, _lodash.cloneDeep)(newList);
  var result = newList.map(function (apiItem) {
    if (apiItem.formKey === _formKeyType.DataSourceType.materielType) {
      apiItem.disabled = false;
      apiItem.formKey = _formKeyType.DataSourceType.defaultType;
    } else {
      apiItem.disabled = true;
    }
    if ((0, _lodash.isArray)(apiItem.data) && apiItem.data.length) {
      if (apiItem.type === _type.PactNameType.tupleList || apiItem.type === _type.PactNameType.tuple) {
        apiItem.data = apiItem.data.map(function (dataItem) {
          dataItem = getApiParams(dataItem);
          return dataItem;
        });
      }
      // else if (apiItem.type === PactNameType.tuple) {
      //     apiItem.data = getApiParams(apiItem.data)
      // }
    }
    return apiItem;
  }) || [];
  return result;
}
function getCreateDataSourceItem() {
  return {
    id: (0, _utils.uuid)(),
    dataSourceId: '',
    dataSourceName: '',
    apiParams: []
  };
}
function alterDataSourceList(alter, list) {
  return list.map(function (item) {
    if (item.id === alter.id) {
      return alter;
    }
    return item;
  });
}
function getInitDrawerConfig() {
  return {
    index: 0,
    visible: false,
    data: {
      id: "",
      dataSourceId: "",
      dataSourceName: "",
      apiParams: []
    }
  };
}