/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import { _ as _slicedToArray } from '../../_chunks/dep-55846619.mjs';
import { i as isNumber_1 } from '../../_chunks/dep-6eea369f.mjs';
import { i as isFunction_1 } from '../../_chunks/dep-6b22dd27.mjs';
import { i as isArray_1 } from '../../_chunks/dep-e3b14d70.mjs';
import { c as cloneDeep_1 } from '../../_chunks/dep-44417261.mjs';
import { getFullPathLabel, getTreeValue } from './helper.mjs';
import '../../_chunks/dep-cfcedf47.mjs';
import '../../_chunks/dep-a0b4a044.mjs';
import '../../_chunks/dep-c959a6fb.mjs';
import '../../_chunks/dep-b4041200.mjs';
import '../../_chunks/dep-c31e1d9e.mjs';
import '../../_chunks/dep-0f0c89ff.mjs';
import '../../_chunks/dep-bf74f1c2.mjs';
import '../../_chunks/dep-79821ff2.mjs';
import '../../_chunks/dep-0bfbf721.mjs';
import '../../_chunks/dep-39cc276d.mjs';
import '../../_chunks/dep-8f777748.mjs';
import '../../_chunks/dep-ae357f26.mjs';
import '../../_chunks/dep-352691c2.mjs';
import '../../_chunks/dep-2cfd5b3a.mjs';
import '../../_chunks/dep-05fa17c9.mjs';
import '../../_chunks/dep-66d6733b.mjs';
import '../../_chunks/dep-a9c8a5d5.mjs';
import '../../_chunks/dep-10085c61.mjs';
import '../../_chunks/dep-1b058de3.mjs';
import '../../_chunks/dep-c852a61a.mjs';
import '../../_chunks/dep-92e7085e.mjs';
import '../../_chunks/dep-44f056cd.mjs';
import '../../_chunks/dep-23a0f453.mjs';
import '../../_chunks/dep-bd03960e.mjs';

function expendClickEffect(propsTrigger, trigger, node, cascaderContext) {
  var checkStrictly = cascaderContext.checkStrictly,
    multiple = cascaderContext.multiple,
    treeStore = cascaderContext.treeStore,
    setVisible = cascaderContext.setVisible,
    setValue = cascaderContext.setValue,
    setTreeNodes = cascaderContext.setTreeNodes,
    setExpend = cascaderContext.setExpend,
    value = cascaderContext.value,
    max = cascaderContext.max,
    valueType = cascaderContext.valueType;
  var isDisabled = node.disabled || multiple && value.length >= max && max !== 0;
  if (isDisabled) return;
  if (propsTrigger === trigger && !node.isLeaf()) {
    var expanded = node.setExpanded(true);
    treeStore.refreshNodes();
    treeStore.replaceExpanded(expanded);
    var nodes = treeStore.getNodes().filter(function (node2) {
      return node2.visible;
    });
    setTreeNodes(nodes);
    if (multiple) setExpend(expanded);
  }
  if (!multiple && (node.isLeaf() || checkStrictly) && trigger === "click") {
    treeStore.resetChecked();
    var checked = node.setChecked(!node.checked);
    var _checked = _slicedToArray(checked, 1),
      value2 = _checked[0];
    setValue(valueType === "single" ? value2 : node.getPath().map(function (item) {
      return item.value;
    }), "check", node.getModel());
    if (!checkStrictly) setVisible(false, {});
  }
}
function valueChangeEffect(node, cascaderContext) {
  var disabled = cascaderContext.disabled,
    max = cascaderContext.max,
    inputVal = cascaderContext.inputVal,
    multiple = cascaderContext.multiple,
    setVisible = cascaderContext.setVisible,
    setValue = cascaderContext.setValue,
    treeNodes = cascaderContext.treeNodes,
    treeStore = cascaderContext.treeStore,
    valueType = cascaderContext.valueType;
  if (!node || disabled || node.disabled) return;
  var checked = node.setChecked(!node.isChecked());
  if (isNumber_1(max) && max < 0) console.warn("TDesign Warn:", "max should > 0");
  if (checked.length > max && isNumber_1(max) && max > 0) return;
  if (checked.length === 0) {
    var expanded = treeStore.getExpanded();
    setTimeout(function () {
      treeStore.replaceExpanded(expanded);
      treeStore.refreshNodes();
    }, 0);
  }
  if (!multiple) setVisible(false, {});
  var isSelectAll = treeNodes.every(function (item) {
    return checked.includes(item.value);
  });
  if (inputVal && isSelectAll) setVisible(false, {});
  var resValue = valueType === "single" ? checked : checked.map(function (val) {
    return treeStore.getNode(val).getPath().map(function (item) {
      return item.value;
    });
  });
  setValue(resValue, "check", node.getModel());
}
function closeIconClickEffect(cascaderContext) {
  var setVisible = cascaderContext.setVisible,
    multiple = cascaderContext.multiple,
    setExpend = cascaderContext.setExpend,
    setValue = cascaderContext.setValue;
  setVisible(false, {});
  if (multiple) setExpend([]);
  setValue(multiple ? [] : "", "clear");
}
function handleRemoveTagEffect(cascaderContext, index, onRemove) {
  var disabled = cascaderContext.disabled,
    setValue = cascaderContext.setValue,
    value = cascaderContext.value,
    valueType = cascaderContext.valueType,
    treeStore = cascaderContext.treeStore;
  if (disabled) return;
  var newValue = cloneDeep_1(value);
  var res = newValue.splice(index, 1);
  var node = treeStore.getNodes(res[0])[0];
  setValue(newValue, "uncheck", node.getModel());
  var checked = node.setChecked(!node.isChecked());
  var resValue = valueType === "single" ? checked : checked.map(function (val) {
    return treeStore.getNode(val).getPath().map(function (item) {
      return item.value;
    });
  });
  setValue(resValue, "uncheck", node.getModel());
  if (isFunction_1(onRemove)) onRemove({
    value: checked,
    node: node
  });
}
var treeNodesEffect = function treeNodesEffect(inputVal, treeStore, setTreeNodes, filter) {
  if (!treeStore) return;
  var nodes = [];
  if (inputVal) {
    var filterMethods = function filterMethods(node) {
      if (!node.isLeaf()) return;
      if (isFunction_1(filter)) return filter("".concat(inputVal), node);
      var fullPathLabel = getFullPathLabel(node, "");
      return fullPathLabel.includes("".concat(inputVal));
    };
    nodes = treeStore.nodes.filter(filterMethods);
  } else {
    nodes = treeStore.getNodes().filter(function (node) {
      return node.visible;
    });
  }
  setTreeNodes(nodes);
};
var treeStoreExpendEffect = function treeStoreExpendEffect(treeStore, value, expend) {
  var treeValue = getTreeValue(value);
  if (!treeStore) return;
  if (isArray_1(treeValue) && expend.length === 0) {
    var expandedMap = /* @__PURE__ */new Map();
    var _treeValue = _slicedToArray(treeValue, 1),
      val = _treeValue[0];
    if (val) {
      expandedMap.set(val, true);
      var node = treeStore.getNode(val);
      if (!node) {
        treeStore.refreshNodes();
        return;
      }
      node.getParents().forEach(function (tn) {
        expandedMap.set(tn.value, true);
      });
      var expandedArr = Array.from(expandedMap.keys());
      treeStore.replaceExpanded(expandedArr);
    } else {
      treeStore.resetExpanded();
    }
  }
  if (treeStore.getExpanded() && expend.length) treeStore.replaceExpanded(expend);
  treeStore.refreshNodes();
};

export { closeIconClickEffect, expendClickEffect, handleRemoveTagEffect, treeNodesEffect, treeStoreExpendEffect, valueChangeEffect };
//# sourceMappingURL=effect.mjs.map
