import { EntrySchemaService } from "../../../cTreeDataRelation-setter/comFn/EntrySchemaService";
import { FlowEvent } from "../type";
import { getLangType } from "../../../../_utils/utils";
import { cloneDeep, isArray } from "lodash";
export var RulePanelService = /*#__PURE__*/function () {
  function RulePanelService() {
    this.entrySchemaService = void 0;
    this.entrySchemaService = new EntrySchemaService();
  }
  var _proto = RulePanelService.prototype;
  _proto.getViewKeyOptionsByViewList = function getViewKeyOptionsByViewList(viewList) {
    var lang = getLangType();
    var result = [];
    if (viewList && viewList.length) {
      viewList.forEach(function (item) {
        result.push({
          value: item.key,
          // 后期这里可以扩展成多语言的读取
          label: item.name.label[lang]
        });
      });
    }
    return result;
  };
  _proto.getPageKeyOptionsByViewList = function getPageKeyOptionsByViewList(viewList, viewKey) {
    var result = [];
    var lang = getLangType();
    var list = this.getViewListByViewKey(viewList, viewKey);
    list.forEach(function (item) {
      var _item$pageList;
      if ((_item$pageList = item.pageList) !== null && _item$pageList !== void 0 && _item$pageList.length) {
        item.pageList.forEach(function (pageItem) {
          if (pageItem.name.visible) {
            result.push({
              value: pageItem.key,
              // 后期这里可以扩展成多语言的读取
              label: pageItem.name.label[lang]
            });
          }
        });
      }
    });
    return result;
  };
  _proto.getParamsOptionsByViewList = function getParamsOptionsByViewList(viewList, viewKey, pageKey) {
    var result = [];
    if (viewList && viewList.length) {
      var _list$;
      var editArgs = [];
      var protocolEditArgs;
      var list = this.getViewListByViewKey(viewList, viewKey);
      if (list.length && (_list$ = list[0]) !== null && _list$ !== void 0 && _list$.protocolEditArgs) {
        protocolEditArgs = list[0].protocolEditArgs;
      }
      if (protocolEditArgs) {
        if (isArray(protocolEditArgs)) {
          editArgs = protocolEditArgs;
        } else if (protocolEditArgs[pageKey]) {
          editArgs = protocolEditArgs[pageKey];
        }
      }
      result = this.entrySchemaService.load(editArgs);
    }
    return result;
  }

  /**
   * 从流里面过滤出多组规则，用于编辑态给CPanelSetter显示
   * @param flow 
   */;
  _proto.getRuleNodeOptions = function getRuleNodeOptions(flow) {
    var _flow$children,
      _this = this;
    var result = [];
    if (flow.config && flow.type === FlowEvent.rule && flow.config.pbiId !== undefined && flow.config.ruleId !== undefined && flow.config.pageKey !== undefined) {
      var obj = {
        value: this.getRuleNodeConfigValue(flow.config),
        label: flow.label,
        config: this.getFlowNodeConfig(flow.config),
        eventFlowId: flow.id
      };
      result.push(obj);
    } else if ((_flow$children = flow.children) !== null && _flow$children !== void 0 && _flow$children.length) {
      flow.children.forEach(function (item) {
        result = result.concat(_this.getRuleNodeOptions(item));
      });
    }
    return result;
  };
  _proto.getViewListByViewKey = function getViewListByViewKey(viewList, viewKey) {
    var result = [];
    if (viewList && viewList.length) {
      result = viewList.filter(function (item) {
        return item.key === viewKey;
      });
    }
    return result;
  };
  _proto.getFlowNodeConfig = function getFlowNodeConfig(config) {
    var data = cloneDeep(config);
    delete data.apiParams;
    return data;
  };
  _proto.getRuleNodeConfigValue = function getRuleNodeConfigValue(config) {
    return config.pbiId + "-" + config.ruleId + "-" + config.pageKey;
  };
  return RulePanelService;
}();