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

import { _ as _slicedToArray } from '../../_chunks/dep-84acf3c2.mjs';
import { i as isNumber_1 } from '../../_chunks/dep-3f21269a.mjs';
import { i as isFunction_1 } from '../../_chunks/dep-5717e263.mjs';
import { i as isArray_1 } from '../../_chunks/dep-b57180d0.mjs';
import { c as cloneDeep_1 } from '../../_chunks/dep-095107f7.mjs';
import { getFullPathLabel, getTreeValue } from './helper.mjs';
import '../../_chunks/dep-b8d7992c.mjs';
import '../../_chunks/dep-eba0ea6e.mjs';
import '../../_chunks/dep-469b4ae0.mjs';
import '../../_chunks/dep-255303ba.mjs';
import '../../_chunks/dep-3da99438.mjs';
import '../../_chunks/dep-a8bcacea.mjs';
import '../../_chunks/dep-c989dff3.mjs';
import '../../_chunks/dep-9cf86cf1.mjs';
import '../../_chunks/dep-bc1d737a.mjs';
import '../../_chunks/dep-4d7a675b.mjs';
import '../../_chunks/dep-2e7f59b1.mjs';
import '../../_chunks/dep-855db8bc.mjs';
import '../../_chunks/dep-a87f3e4c.mjs';
import '../../_chunks/dep-1e566bb5.mjs';
import '../../_chunks/dep-1434aa15.mjs';
import '../../_chunks/dep-c8d7b6b4.mjs';
import '../../_chunks/dep-e5eb9f23.mjs';
import '../../_chunks/dep-9fe2ef12.mjs';
import '../../_chunks/dep-672e7d1a.mjs';
import '../../_chunks/dep-42a19209.mjs';
import '../../_chunks/dep-9262ee2f.mjs';
import '../../_chunks/dep-334a4f6b.mjs';
import '../../_chunks/dep-8d3e9aa3.mjs';
import '../../_chunks/dep-888e80c4.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
