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; }
import { FlowEvent, FlowEventSelectType, OriginConfigType } from "./type";
import { t } from "../../../../src/locale";
import { BehaviorType } from "../../../../src/utils/CLowCodeService/type";
import { SchemaService } from "../../../_utils/schemaService";

// 获取树的深度
export 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 
 */
export function isActionAndCondition(menus) {
  var menuList = menus.filter(function (item) {
    return item.key === FlowEvent.action || item.key === FlowEvent.condition;
  });
  return menuList.length === 2;
}
export function getComMenus() {
  return [{
    key: FlowEvent.rule,
    label: t('setter.flowMenusRule'),
    nodeType: FlowEvent.rule,
    nodeName: t('setter.flowMenusRule')
  }, {
    key: FlowEvent.action,
    label: t('setter.flowMenusAction'),
    nodeType: FlowEvent.action,
    nodeName: t('setter.flowMenusAction')
  }, {
    key: FlowEvent.condition,
    label: t('setter.flowMenusCondition'),
    nodeType: FlowEvent.condition,
    nodeName: t('setter.flowMenusCondition')
  }, {
    key: FlowEvent.conditionElse,
    label: t('setter.flowMenusConditionElse'),
    nodeType: FlowEvent.conditionElse,
    nodeName: t('setter.flowMenusConditionElse')
  }, {
    key: FlowEvent.eventFlowNode,
    label: t('setter.flowMenusEventFlow'),
    nodeType: FlowEvent.eventFlowNode,
    nodeName: t('setter.flowMenusEventFlow')
  }, {
    key: FlowEvent.end,
    label: t('setter.flowMenusEnd'),
    nodeType: FlowEvent.end,
    nodeName: t('setter.flowMenusEnd')
  }, {
    key: FlowEvent.abnormal,
    label: t('setter.flowMenusAbnormal'),
    nodeType: FlowEvent.abnormal,
    nodeName: t('setter.flowMenusAbnormal')
  }];
}
export function getInterposition() {
  return [{
    key: FlowEvent.ruleInsert,
    label: t('setter.flowMenusRuleInsert'),
    nodeType: FlowEvent.rule,
    nodeName: t('setter.flowMenusRule')
  }, {
    key: FlowEvent.actionInsert,
    label: t('setter.flowMenusActionInsert'),
    nodeType: FlowEvent.action,
    nodeName: t('setter.flowMenusAction')
  }, {
    key: FlowEvent.conditionInsert,
    label: t('setter.flowMenusConditionInsert'),
    nodeType: FlowEvent.condition,
    nodeName: t('setter.flowMenusCondition')
  }];
}
export function getDeleteMenus() {
  return [{
    key: FlowEvent.deleteContent,
    label: t('setter.deleteContent')
  }, {
    key: FlowEvent.deleteAfter,
    label: t('setter.deleteAfter')
  }];
}
export function getActionRequestMenus() {
  return [{
    key: FlowEvent.success,
    label: t('setter.flowMenusSuccess'),
    nodeType: FlowEvent.event,
    nodeName: FlowEvent.success
  }, {
    key: FlowEvent.error,
    label: t('setter.flowMenusError'),
    nodeType: FlowEvent.event,
    nodeName: FlowEvent.error
  }, {
    key: FlowEvent["finally"],
    label: t('setter.flowMenusEvent'),
    nodeType: FlowEvent.event,
    nodeName: FlowEvent["finally"]
  }];
}

/**
 * 检测是否含有多个规则节点
 * @param node 
 * @returns 
 */
export 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 === 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
// }

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

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