/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import isNumber from 'lodash/isNumber';
import isFunction from 'lodash/isFunction';
import cloneDeep from 'lodash/cloneDeep';
import { getTreeValue, getFullPathLabel } from './helper.js';
import 'lodash/isEmpty';

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