import { createTreeRelationItem } from "../../cTreeDataRelation-setter/comFn";
import { DataSourceType } from "../../../_utils/formKeyType";
import { cloneDeep, isArray } from "lodash";
import { PactNameType } from "../../../../src/utils/pactList/type";
export function getPbiDataItemData(list) {
  var result = [];
  list.forEach(function (item) {
    var treeItem = createTreeRelationItem({
      name: "" + item.name,
      type: item.type,
      disabled: item.editable !== undefined ? !item.editable : true,
      value: item.value,
      required: item.required,
      formKey: DataSourceType.defaultType
    });

    // 补充精度
    if (item.decimals !== undefined) {
      treeItem.decimals = item.decimals;
    }

    // 处理元组
    if (item.type === PactNameType.tuple || item.type === PactNameType.tupleList) {
      treeItem.formKey = DataSourceType.tupleType;
      treeItem.addable = item.addable !== undefined ? item.addable : 0;
      if (item.components.length) {
        treeItem.components = getPbiDataItemData(item.components);
      } else {
        treeItem.components = [];
        treeItem.addable = 0;
        // if (item.value) {
        //     treeItem.data = getValidString(item.value)
        // }
      }
      // 是元组类型的value存在都需要处理
      if (item.value) {
        treeItem.data = getTreeItemValueByItem(item.value);
      }
    }
    result.push(treeItem);
  });
  return result;
}
function getTreeItemValueByItem(list) {
  var result = [];
  if (isArray(list)) {
    list.forEach(function (item) {
      var res = getPbiDataItemData(item);
      result.push(res);
    });
  }
  return result;
}
export function getChangePactAddressObj(list, pactAddress) {
  var result = null;
  var obj = cloneDeep(pactAddress);
  if (obj) {
    list.forEach(function (item) {
      var key = item.key,
        value = item.value;
      obj[key] = value;
    });
    result = obj;
  }
  return result;
}