"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.TreeDataRelationService = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _lowcodeEngine = require("@alilc/lowcode-engine");
var _type = require("./type");
var _lodash = require("lodash");
var _index = require("../../../src/locale/index");
var _commonFn = require("../../_setters/cGroupManage-setter/commonFn");
var _utils = require("../utils");
var _schemaService = require("../schemaService");
var CodeList = (0, _commonFn.getDefaultIndexOptions)();
var TreeDataRelationService = exports.TreeDataRelationService = /*#__PURE__*/function () {
  function TreeDataRelationService() {}
  var _proto = TreeDataRelationService.prototype;
  /**
   * 得到表单配置内容
   * @returns
   */
  _proto.getFormStateDataBySchema = function getFormStateDataBySchema() {
    var componentsTree = this.getComponentsTreeBySchema();
    var result = {
      label: (0, _index.t)('setter.stateData'),
      key: _type.PageStateFlag.State,
      type: _type.PageStateFlag.State,
      children: []
    };
    result.children = this.getFormItemBySchemaChildren([componentsTree]);
    return [result];
  };
  _proto.getDataSourceBySchema = function getDataSourceBySchema() {
    var _componentsTree$dataS, _componentsTree$dataS2;
    var componentsTree = this.getComponentsTreeBySchema();
    var result = {
      label: (0, _index.t)('setter.dataSourceData'),
      key: _type.PageStateFlag.DataSourceMap,
      type: _type.PageStateFlag.DataSourceMap,
      children: []
    };
    if (componentsTree !== null && componentsTree !== void 0 && (_componentsTree$dataS = componentsTree.dataSource) !== null && _componentsTree$dataS !== void 0 && (_componentsTree$dataS2 = _componentsTree$dataS.list) !== null && _componentsTree$dataS2 !== void 0 && _componentsTree$dataS2.length) {
      var dataList = componentsTree.dataSource.list;
      dataList.forEach(function (dataItem) {
        var _result$children;
        var childItem = {
          label: dataItem.options.dataSourceName,
          key: dataItem.options.id,
          type: _type.TreeType.dataSourceMapItem
        };
        (_result$children = result.children) === null || _result$children === void 0 ? void 0 : _result$children.push(childItem);
      });
    }
    return [result];
  };
  _proto.getDictionaryRelation = function getDictionaryRelation() {
    var _componentsTree$dicti, _componentsTree$dicti2;
    var result = [];
    var componentsTree = this.getComponentsTreeBySchema();
    if (componentsTree !== null && componentsTree !== void 0 && (_componentsTree$dicti = componentsTree.dictionary) !== null && _componentsTree$dicti !== void 0 && (_componentsTree$dicti2 = _componentsTree$dicti.list) !== null && _componentsTree$dicti2 !== void 0 && _componentsTree$dicti2.length) {
      var list = componentsTree.dictionary.list;
      list.forEach(function (dataItem) {
        var childItem = {
          label: dataItem.name,
          key: dataItem.id,
          type: _type.TreeType.dictionaryItem
        };
        result.push(childItem);
      });
    }
    return result;
  };
  _proto.getEngineRuleRelation = function getEngineRuleRelation() {
    var result = [];
    var componentsTree = this.getComponentsTreeBySchema();
    if (componentsTree !== null && componentsTree !== void 0 && componentsTree.children) {
      result = this.getEnginRuleBySchemaChildren(componentsTree.children);
    }
    return result;
  };
  _proto.getLoopDataSelectedRelation = function getLoopDataSelectedRelation() {
    var result = [];
    var componentsTree = this.getComponentsTreeBySchema();
    if (componentsTree !== null && componentsTree !== void 0 && componentsTree.children) {
      result = this.getLoopDataSelectedChildren(componentsTree.children);
    }
    return result;
  }

  /**
   * 从schema中state中获取数据
   * 后期可以从newState中获取，newState直接就是对象
   */;
  _proto.getCustomStateBySchema = function getCustomStateBySchema() {
    var result = [];
    var componentsTree = this.getComponentsTreeBySchema();
    var state = componentsTree.state;
    if (state) {
      var newState = {};
      Object.keys(state).forEach(function (key) {
        if (key === _type.PageStateFlag.CustomState) {
          newState[key] = JSON.parse(state[key].value);
        }
      });
      if (Object.keys(newState).length) {
        result.push(this.getStateTreeData(newState, (0, _index.t)('setter.customData'), _type.PageStateFlag.CustomState, _type.TreeType.customStateItem));
      }
    }
    return result;
  };
  _proto.getModalDialogRelation = function getModalDialogRelation() {
    var _keyObj,
      _this = this;
    var result = [];
    var keyObj = (_keyObj = {}, _keyObj[_type.ComponentName.Modal] = (0, _index.t)('setter.Modal'), _keyObj[_type.ComponentName.Drawer] = (0, _index.t)('setter.Drawer'), _keyObj[_type.ComponentName.ModalDefault] = (0, _index.t)('setter.ModalDefault'), _keyObj);
    var componentsTree = this.getComponentsTreeBySchema();
    var componentNameMap = {};
    var modalList = [_type.ComponentName.Modal, _type.ComponentName.Drawer, _type.ComponentName.ModalDefault];
    if (componentsTree !== null && componentsTree !== void 0 && componentsTree.children && (0, _lodash.isArray)(componentsTree.children)) {
      componentsTree.children.forEach(function (item) {
        if (modalList.includes(item.componentName)) {
          if (componentNameMap[item.componentName]) {
            componentNameMap[item.componentName].push(item);
          } else {
            componentNameMap[item.componentName] = [item];
          }
        }
      });
    }
    Object.keys(componentNameMap).forEach(function (key) {
      var obj = {
        label: keyObj[key] || (0, _index.t)('setter.definition'),
        key: _type.ComponentName.Modal + "_" + _this.getParentKeyByChildren(componentNameMap[key]),
        type: _type.TreeType.modalItem,
        componentName: key,
        children: _this.getModalChildren(componentNameMap[key])
      };
      result.push(obj);
    });
    return result;
  };
  _proto.getWalletConnectRelation = function getWalletConnectRelation() {
    var result = [{
      label: (0, _index.t)('setter.walletConnectedState'),
      key: _type.WalletConnectType.WalletConnected,
      type: _type.TreeType.currentWalletConnectItem
    }, {
      label: (0, _index.t)('setter.addressData'),
      key: _type.WalletConnectType.Address,
      type: _type.TreeType.currentWalletConnectItem
    }, {
      label: (0, _index.t)('setter.chainIdData'),
      key: _type.WalletConnectType.ChainId,
      type: _type.TreeType.currentWalletConnectItem
    }, {
      label: (0, _index.t)('setter.walletConnectedEnv'),
      key: _type.WalletConnectType.Env,
      type: _type.TreeType.currentWalletConnectItem
    }, {
      label: (0, _index.t)('setter.timeTitle'),
      key: _type.WalletConnectType.Time,
      type: _type.TreeType.currentWalletConnectItem
    }, {
      label: (0, _index.t)('setter.blockTitle'),
      key: _type.WalletConnectType.Block,
      type: _type.TreeType.currentWalletConnectItem
    }, {
      label: (0, _index.t)('setter.customData'),
      key: _type.WalletConnectType.Custom,
      type: _type.TreeType.currentWalletConnectItem
    }];
    return result;
  };
  _proto.getUrlRelation = function getUrlRelation() {
    var result = [{
      label: (0, _index.t)('setter.locationOrigin'),
      key: _type.LocationUrlType.origin,
      type: _type.TreeType.locationItem
    }, {
      label: (0, _index.t)('setter.locationHost'),
      key: _type.LocationUrlType.host,
      type: _type.TreeType.locationItem
    }, {
      label: (0, _index.t)('setter.locationProtocol'),
      key: _type.LocationUrlType.protocol,
      type: _type.TreeType.locationItem
    }, {
      label: (0, _index.t)('setter.customData'),
      key: _type.LocationUrlType.custom,
      type: _type.TreeType.locationItem
    }];
    return result;
  };
  _proto.getStorageDataRelation = function getStorageDataRelation() {
    var result = [];
    var schemaService = new _schemaService.SchemaService();
    result = schemaService.getStorageDataRelation();
    return result;
  };
  _proto.getModalChildren = function getModalChildren(list) {
    var _this2 = this;
    var result = [];
    list.forEach(function (item) {
      var _item$props, _item$props2;
      if (item !== null && item !== void 0 && (_item$props = item.props) !== null && _item$props !== void 0 && _item$props.name && item !== null && item !== void 0 && (_item$props2 = item.props) !== null && _item$props2 !== void 0 && _item$props2.apiParams) {
        var obj = {
          label: item.props.name,
          key: item.props.name,
          type: _type.TreeType.modalParamsKeyItem,
          children: _this2.getModalParams(item.props.name, item.props.apiParams)
        };
        result.push(obj);
      }
    });
    return result;
  };
  _proto.getFormItemBySchemaChildren = function getFormItemBySchemaChildren(children) {
    var _this3 = this;
    var result = [];
    if (children && children.length) {
      children.forEach(function (item) {
        var _item$props$loopSlot, _item$props$loopSlot$, _item$props$loopSlot$2, _item$props$carouselS, _item$props$carouselS2, _item$props$carouselS3, _item$children;
        // 存在循环嵌套循环
        if (item.componentName === _type.ComponentName.FormItem && item.props.name !== undefined && item.props.componentName !== undefined) {
          result.push({
            label: item.props.componentName || item.props.name,
            key: item.props.name,
            type: _type.TreeType.stateItem
          });
        }
        if (item.componentName === _type.ComponentName.LoopBox && (_item$props$loopSlot = item.props.loopSlot) !== null && _item$props$loopSlot !== void 0 && (_item$props$loopSlot$ = _item$props$loopSlot.value) !== null && _item$props$loopSlot$ !== void 0 && (_item$props$loopSlot$2 = _item$props$loopSlot$.children) !== null && _item$props$loopSlot$2 !== void 0 && _item$props$loopSlot$2.length) {
          result = result.concat(_this3.getFormItemBySchemaChildren(item.props.loopSlot.value.children));
        }
        if (item.componentName === _type.ComponentName.CarouselBox && (_item$props$carouselS = item.props.carouselSlot) !== null && _item$props$carouselS !== void 0 && (_item$props$carouselS2 = _item$props$carouselS.value) !== null && _item$props$carouselS2 !== void 0 && (_item$props$carouselS3 = _item$props$carouselS2.children) !== null && _item$props$carouselS3 !== void 0 && _item$props$carouselS3.length) {
          result = result.concat(_this3.getFormItemBySchemaChildren(item.props.carouselSlot.value.children));
        }
        // tab的情况下需要特殊处理
        if (item.componentName === _type.ComponentName.Tabs) {
          var _item$props$items;
          var list = [];
          if ((_item$props$items = item.props.items) !== null && _item$props$items !== void 0 && _item$props$items.length) {
            item.props.items.forEach(function (itemOne) {
              var _itemOne$children, _itemOne$children$val;
              list = list.concat(((_itemOne$children = itemOne.children) === null || _itemOne$children === void 0 ? void 0 : (_itemOne$children$val = _itemOne$children.value) === null || _itemOne$children$val === void 0 ? void 0 : _itemOne$children$val.children) || []);
            });
          }
          if (list.length) {
            result = result.concat(_this3.getFormItemBySchemaChildren(list));
          }
        }
        if ((_item$children = item.children) !== null && _item$children !== void 0 && _item$children.length) {
          result = result.concat(_this3.getFormItemBySchemaChildren(item.children));
        }
      });
    }
    return result;
  };
  _proto.getEnginRuleBySchemaChildren = function getEnginRuleBySchemaChildren(children) {
    var _this4 = this;
    var result = [];
    if (children && children.length) {
      children.forEach(function (item) {
        var _item$props$loopSlot2, _item$props$loopSlot3, _item$props$loopSlot4, _item$props$carouselS4, _item$props$carouselS5, _item$props$carouselS6, _item$children2;
        // 存在循环嵌套循环
        if ((item.componentName === _type.ComponentName.EngineRulePro || item.componentName === _type.ComponentName.EngineRuleUltra) && item.props.componentKey !== undefined && item.props.componentName !== undefined) {
          result.push({
            label: item.props.componentName,
            key: item.props.componentKey,
            type: _type.TreeType.engineRuleItem,
            children: _this4.getEngineCodeList(CodeList, item.props.componentKey)
          });
        }
        if (item.componentName === _type.ComponentName.LoopBox && (_item$props$loopSlot2 = item.props.loopSlot) !== null && _item$props$loopSlot2 !== void 0 && (_item$props$loopSlot3 = _item$props$loopSlot2.value) !== null && _item$props$loopSlot3 !== void 0 && (_item$props$loopSlot4 = _item$props$loopSlot3.children) !== null && _item$props$loopSlot4 !== void 0 && _item$props$loopSlot4.length) {
          result = result.concat(_this4.getEnginRuleBySchemaChildren(item.props.loopSlot.value.children));
        }
        if (item.componentName === _type.ComponentName.CarouselBox && (_item$props$carouselS4 = item.props.carouselSlot) !== null && _item$props$carouselS4 !== void 0 && (_item$props$carouselS5 = _item$props$carouselS4.value) !== null && _item$props$carouselS5 !== void 0 && (_item$props$carouselS6 = _item$props$carouselS5.children) !== null && _item$props$carouselS6 !== void 0 && _item$props$carouselS6.length) {
          result = result.concat(_this4.getEnginRuleBySchemaChildren(item.props.carouselSlot.value.children));
        }
        // tab的情况下需要特殊处理
        if (item.componentName === _type.ComponentName.Tabs) {
          var _item$props$items2;
          var list = [];
          if ((_item$props$items2 = item.props.items) !== null && _item$props$items2 !== void 0 && _item$props$items2.length) {
            item.props.items.forEach(function (itemOne) {
              var _itemOne$children2, _itemOne$children2$va;
              list = list.concat(((_itemOne$children2 = itemOne.children) === null || _itemOne$children2 === void 0 ? void 0 : (_itemOne$children2$va = _itemOne$children2.value) === null || _itemOne$children2$va === void 0 ? void 0 : _itemOne$children2$va.children) || []);
            });
          }
          if (list.length) {
            result = result.concat(_this4.getEnginRuleBySchemaChildren(list));
          }
        }
        if ((_item$children2 = item.children) !== null && _item$children2 !== void 0 && _item$children2.length) {
          result = result.concat(_this4.getEnginRuleBySchemaChildren(item.children));
        }
      });
    }
    return result;
  };
  _proto.getLoopDataSelectedChildren = function getLoopDataSelectedChildren(children) {
    var _this5 = this;
    var result = [];
    if (children && children.length) {
      children.forEach(function (item) {
        var _item$children3;
        // 存在循环嵌套循环
        if ((item.componentName === _type.ComponentName.LoopBox || item.componentName === _type.ComponentName.CarouselBox) && item.props.componentKey !== undefined && item.props.componentName !== undefined) {
          var _item$props$loopSlot5, _item$props$loopSlot6, _item$props$loopSlot7, _item$props$carouselS7, _item$props$carouselS8, _item$props$carouselS9;
          result.push({
            label: item.props.componentName,
            key: item.props.componentKey,
            type: _type.TreeType.loopDataSelectedItem
          });
          if (item.componentName === _type.ComponentName.LoopBox && (_item$props$loopSlot5 = item.props.loopSlot) !== null && _item$props$loopSlot5 !== void 0 && (_item$props$loopSlot6 = _item$props$loopSlot5.value) !== null && _item$props$loopSlot6 !== void 0 && (_item$props$loopSlot7 = _item$props$loopSlot6.children) !== null && _item$props$loopSlot7 !== void 0 && _item$props$loopSlot7.length) {
            result = result.concat(_this5.getLoopDataSelectedChildren(item.props.loopSlot.value.children));
          }
          if (item.componentName === _type.ComponentName.CarouselBox && (_item$props$carouselS7 = item.props.carouselSlot) !== null && _item$props$carouselS7 !== void 0 && (_item$props$carouselS8 = _item$props$carouselS7.value) !== null && _item$props$carouselS8 !== void 0 && (_item$props$carouselS9 = _item$props$carouselS8.children) !== null && _item$props$carouselS9 !== void 0 && _item$props$carouselS9.length) {
            result = result.concat(_this5.getLoopDataSelectedChildren(item.props.carouselSlot.value.children));
          }
        }
        // tab的情况下需要特殊处理
        if (item.componentName === _type.ComponentName.Tabs) {
          var _item$props$items3;
          var list = [];
          if ((_item$props$items3 = item.props.items) !== null && _item$props$items3 !== void 0 && _item$props$items3.length) {
            item.props.items.forEach(function (itemOne) {
              var _itemOne$children3, _itemOne$children3$va;
              list = list.concat(((_itemOne$children3 = itemOne.children) === null || _itemOne$children3 === void 0 ? void 0 : (_itemOne$children3$va = _itemOne$children3.value) === null || _itemOne$children3$va === void 0 ? void 0 : _itemOne$children3$va.children) || []);
            });
          }
          if (list.length) {
            result = result.concat(_this5.getLoopDataSelectedChildren(list));
          }
        }
        if ((_item$children3 = item.children) !== null && _item$children3 !== void 0 && _item$children3.length) {
          result = result.concat(_this5.getLoopDataSelectedChildren(item.children));
        }
      });
    }
    return result;
  };
  _proto.getModalParams = function getModalParams(propsName, list) {
    var result = [];
    list.forEach(function (item) {
      var obj = {
        label: item.name || (0, _index.t)('setter.fileName'),
        key: propsName + ".params." + item.name || (0, _index.t)('setter.fileName'),
        type: _type.TreeType.modalParamsValueItem,
        children: []
      };
      result.push(obj);
    });
    return result;
  }

  // private getStateDataByKey(stateKey: PageStateFlag) {
  //     let result: treeDataType[] = []
  //     const componentsTree = this.getComponentsTreeBySchema()
  //     const state: any = componentsTree.state

  //     if (state) {
  //         let newState: any = {}
  //         Object.keys(state).forEach(key => {
  //             if (key === stateKey) {
  //                 newState[key] = JSON.parse(state[key].value)
  //             }
  //         })

  //         if (Object.keys(newState).length) {
  //             result.push(this.getStateTreeData(newState, t('setter.customData'), PageStateFlag.CustomState, TreeType.customStateItem))
  //         }

  //     }
  //     return result
  // }

  /**
   * 将treeData数据转换成下拉数组需要的数据
   * @param treeData
   * @returns
   */;
  _proto.createRelationDataByTreeData = function createRelationDataByTreeData(treeData) {
    var _this6 = this;
    var result = [];
    result = treeData.map(function (item) {
      var oo = (0, _extends2["default"])({}, item, {
        value: item.key
      });
      if (item.children && item.children.length) {
        oo.children = _this6.createRelationDataByTreeData(item.children);
      }
      return oo;
    });
    return result;
  };
  _proto.getComponentsTreeBySchema = function getComponentsTreeBySchema() {
    var schema = _lowcodeEngine.project.exportSchema();
    return schema.componentsTree[0];
  };
  _proto.getStateMap = function getStateMap(stateObj) {
    var result = [];
    Object.keys(stateObj).forEach(function (key) {
      // if (key === PageStateFlag.State) {
      //     result.push(this.getStateTreeData(stateObj, t('setter.stateData'), PageStateFlag.State, TreeType.stateItem))
      // }
      // else if (key === PageStateFlag.DataSourceMap) {
      //     result.push(this.getStateTreeData(stateObj, t('setter.dataSourceData'), PageStateFlag.DataSourceMap, TreeType.dataSourceMapItem))
      // }
      // else if (key === TreeType.submitReturnData) {
      //     result.push(this.getStateTreeData(stateObj, t('setter.submitReturnData'), PageStateFlag.SubmitReturnData, TreeType.submitReturnDataItem))
      // }
      // else if (key === PageStateFlag.CustomState) {
      //     result.push(this.getStateTreeData(stateObj, t('setter.customData'), PageStateFlag.CustomState, TreeType.customStateItem))
      // }
    });
    return result;
  };
  _proto.getStateTreeData = function getStateTreeData(stateObj, label, key, childKey) {
    var data = stateObj[key];
    var stateTree = {
      label: label,
      key: key,
      type: key,
      children: []
    };
    Object.keys(data).forEach(function (itemKey) {
      var _stateTree$children;
      var obj = {
        label: itemKey,
        key: itemKey,
        type: childKey
      };
      (_stateTree$children = stateTree.children) === null || _stateTree$children === void 0 ? void 0 : _stateTree$children.push(obj);
    });
    return stateTree;
  };
  _proto.getParentKeyByChildren = function getParentKeyByChildren(list) {
    return list.map(function (item) {
      return item.id;
    }).join('');
  };
  _proto.getEngineCodeList = function getEngineCodeList(children, componentKey) {
    var _this7 = this;
    var result = [];
    children.forEach(function (item) {
      var newKey = componentKey + "." + _utils.EngineRuleBeforeFlag + item.value;
      var obj = {
        key: newKey,
        label: item.label,
        type: _type.TreeType.engineRuleItem,
        children: item.stateList ? _this7.getEngineCodeList(item.stateList, newKey) : _this7.getEngineLastData(newKey)
      };
      result.push(obj);
    });
    return result;
  };
  _proto.getEngineLastData = function getEngineLastData(parentKey) {
    return [{
      key: parentKey + ".code",
      label: 'code',
      type: _type.TreeType.engineRuleItem
    }, {
      key: parentKey + ".msg",
      label: 'msg',
      type: _type.TreeType.engineRuleItem
    }, {
      key: parentKey + ".state",
      label: 'state',
      type: _type.TreeType.engineRuleItem
    }, {
      key: parentKey + ".data",
      label: 'data',
      type: _type.TreeType.engineRuleItem
    }];
  };
  return TreeDataRelationService;
}();