"use strict";

exports.__esModule = true;
exports.checkMultiRuleNode = checkMultiRuleNode;
exports.getActionDataSource = getActionDataSource;
exports.getActionRequestMenus = getActionRequestMenus;
exports.getComMenus = getComMenus;
exports.getDeleteMenus = getDeleteMenus;
exports.getFlowEventOptions = getFlowEventOptions;
exports.getFlowEventOptionsBySchema = getFlowEventOptionsBySchema;
exports.getInterposition = getInterposition;
exports.getOriginOptions = getOriginOptions;
exports.getTreeDepth = getTreeDepth;
exports.isActionAndCondition = isActionAndCondition;
exports.isEffectivePluginId = isEffectivePluginId;
var _type = require("./type");
var _locale = require("../../../../src/locale");
var _type2 = require("../../../../src/utils/CLowCodeService/type");
var _schemaService = require("../../../_utils/schemaService");
function _createForOfIteratorHelperLoose(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (t) return (t = t.call(r)).next.bind(t); if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var o = 0; return function () { return o >= r.length ? { done: !0 } : { done: !1, value: r[o++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
// 获取树的深度
function getTreeDepth(node) {
  if (!node) {
    return 0; // 如果节点为空，深度为0
  }
  var maxChildDepth = 0; // 当前节点的子节点的最大深度

  // 遍历当前节点的所有子节点，递归调用 getTreeDepth 函数获取子节点的深度
  for (var _iterator = _createForOfIteratorHelperLoose(node.children || []), _step; !(_step = _iterator()).done;) {
    var child = _step.value;
    var childDepth = getTreeDepth(child);
    if (childDepth > maxChildDepth) {
      maxChildDepth = childDepth;
    }
  }
  return maxChildDepth + 1; // 当前节点的深度为子节点的最大深度加1
}

/**
 * 动作和条件只能添加一个
 * @param menus 
 * @returns 
 */
function isActionAndCondition(menus) {
  var menuList = menus.filter(function (item) {
    return item.key === _type.FlowEvent.action || item.key === _type.FlowEvent.condition;
  });
  return menuList.length === 2;
}
function getComMenus() {
  return [{
    key: _type.FlowEvent.rule,
    label: (0, _locale.t)('setter.flowMenusRule'),
    nodeType: _type.FlowEvent.rule,
    nodeName: (0, _locale.t)('setter.flowMenusRule')
  }, {
    key: _type.FlowEvent.action,
    label: (0, _locale.t)('setter.flowMenusAction'),
    nodeType: _type.FlowEvent.action,
    nodeName: (0, _locale.t)('setter.flowMenusAction')
  }, {
    key: _type.FlowEvent.condition,
    label: (0, _locale.t)('setter.flowMenusCondition'),
    nodeType: _type.FlowEvent.condition,
    nodeName: (0, _locale.t)('setter.flowMenusCondition')
  }, {
    key: _type.FlowEvent.conditionElse,
    label: (0, _locale.t)('setter.flowMenusConditionElse'),
    nodeType: _type.FlowEvent.conditionElse,
    nodeName: (0, _locale.t)('setter.flowMenusConditionElse')
  }, {
    key: _type.FlowEvent.eventFlowNode,
    label: (0, _locale.t)('setter.flowMenusEventFlow'),
    nodeType: _type.FlowEvent.eventFlowNode,
    nodeName: (0, _locale.t)('setter.flowMenusEventFlow')
  }, {
    key: _type.FlowEvent.end,
    label: (0, _locale.t)('setter.flowMenusEnd'),
    nodeType: _type.FlowEvent.end,
    nodeName: (0, _locale.t)('setter.flowMenusEnd')
  }, {
    key: _type.FlowEvent.abnormal,
    label: (0, _locale.t)('setter.flowMenusAbnormal'),
    nodeType: _type.FlowEvent.abnormal,
    nodeName: (0, _locale.t)('setter.flowMenusAbnormal')
  }];
}
function getInterposition() {
  return [{
    key: _type.FlowEvent.ruleInsert,
    label: (0, _locale.t)('setter.flowMenusRuleInsert'),
    nodeType: _type.FlowEvent.rule,
    nodeName: (0, _locale.t)('setter.flowMenusRule')
  }, {
    key: _type.FlowEvent.actionInsert,
    label: (0, _locale.t)('setter.flowMenusActionInsert'),
    nodeType: _type.FlowEvent.action,
    nodeName: (0, _locale.t)('setter.flowMenusAction')
  }, {
    key: _type.FlowEvent.conditionInsert,
    label: (0, _locale.t)('setter.flowMenusConditionInsert'),
    nodeType: _type.FlowEvent.condition,
    nodeName: (0, _locale.t)('setter.flowMenusCondition')
  }];
}
function getDeleteMenus() {
  return [{
    key: _type.FlowEvent.deleteContent,
    label: (0, _locale.t)('setter.deleteContent')
  }, {
    key: _type.FlowEvent.deleteAfter,
    label: (0, _locale.t)('setter.deleteAfter')
  }];
}
function getActionRequestMenus() {
  return [{
    key: _type.FlowEvent.success,
    label: (0, _locale.t)('setter.flowMenusSuccess'),
    nodeType: _type.FlowEvent.event,
    nodeName: _type.FlowEvent.success
  }, {
    key: _type.FlowEvent.error,
    label: (0, _locale.t)('setter.flowMenusError'),
    nodeType: _type.FlowEvent.event,
    nodeName: _type.FlowEvent.error
  }, {
    key: _type.FlowEvent["finally"],
    label: (0, _locale.t)('setter.flowMenusEvent'),
    nodeType: _type.FlowEvent.event,
    nodeName: _type.FlowEvent["finally"]
  }];
}

/**
 * 检测是否含有多个规则节点
 * @param node 
 * @returns 
 */
function checkMultiRuleNode(node) {
  var result = false;
  if (node.children) {
    var ruleNodes = checkRuleChildrenNode(node.children);
    result = ruleNodes.length > 1;
  }
  return result;
}
function checkRuleChildrenNode(list) {
  var result = [];
  for (var i = 0; i < list.length; i++) {
    var _item$children;
    var item = list[i];
    if (item.type === _type.FlowEvent.rule) {
      result.push(item);
    }
    if ((_item$children = item.children) !== null && _item$children !== void 0 && _item$children.length) {
      result = result.concat(checkRuleChildrenNode(item.children));
    }
  }
  return result;
}

/**
 * 校验节点至少得有一个结束节点
 * @param node 
 */
// export function checkNode(node: FlowNode) {
//   let result = false

//   if (node.children) {
//     result = checkChildrenNode(node.children)
//   }

//   return result

// }

// function checkChildrenNode(list: FlowNode[]): boolean {
//   let result = false
//   for (let i = 0; i < list.length; i++) {
//     let item = list[i]

//     if (item.type === FlowEvent.end) {
//       result = true
//       break
//     }
//     if (item.children?.length) {
//       result = checkChildrenNode(item.children)
//     }
//   }
//   return result
// }

function isEffectivePluginId(pluginId) {
  return pluginId !== undefined && pluginId > 0;
}
function getActionDataSource() {
  return [{
    label: (0, _locale.t)('setter.closeProBehavior'),
    value: _type2.BehaviorType.closeProBehavior
  }, {
    label: (0, _locale.t)('setter.alterStateBehavior'),
    value: _type2.BehaviorType.alterStateBehavior
  }, {
    label: (0, _locale.t)('setter.requestBehavior'),
    value: _type2.BehaviorType.requestBehavior
  }, {
    label: (0, _locale.t)('setter.openBehavior'),
    value: _type2.BehaviorType.openBehavior
  }, {
    label: (0, _locale.t)('setter.refreshBehavior'),
    value: _type2.BehaviorType.refreshBehavior
  }, {
    label: (0, _locale.t)('setter.copyToClipboard'),
    value: _type2.BehaviorType.copyToClipboard
  }, {
    label: (0, _locale.t)('setter.awaitBehavior'),
    value: _type2.BehaviorType.awaitBehavior
  }, {
    label: (0, _locale.t)('setter.storageDataBehavior'),
    value: _type2.BehaviorType.storageDataBehavior
  }, {
    label: (0, _locale.t)('setter.recoveryCustomStateBehavior'),
    value: _type2.BehaviorType.recoveryCustomStateBehavior
  }, {
    label: (0, _locale.t)('setter.changeWalletEnvBehavior'),
    value: _type2.BehaviorType.changeWalletEnv
  }, {
    label: (0, _locale.t)('setter.robotCheckBehavior'),
    value: _type2.BehaviorType.robotCheckBehavior
  }, {
    label: (0, _locale.t)('setter.closeBehavior'),
    value: _type2.BehaviorType.closeBehavior
  }];
}
function getOriginOptions() {
  return [{
    label: (0, _locale.t)('setter.originPrivate'),
    value: _type.OriginConfigType["private"]
  }, {
    label: (0, _locale.t)('setter.originCommon'),
    value: _type.OriginConfigType.common
  }];
}
function getFlowEventOptions() {
  return [{
    label: (0, _locale.t)('setter.currentFlowEvent'),
    value: _type.FlowEventSelectType.current
  }, {
    label: (0, _locale.t)('setter.parentFlowEvent'),
    value: _type.FlowEventSelectType.parent
  }];
}

/**
 * 获取全部按钮上的事件流过滤自己
 * @returns 
 */
function getFlowEventOptionsBySchema(componentKey) {
  var result = [];
  var schemaService = new _schemaService.SchemaService();
  result = schemaService.getFlowEventOptionsBySchema(componentKey);
  return result;
}