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

import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import isNumber from 'lodash/isNumber';
import isFunction from 'lodash/isFunction';
import isArray from 'lodash/isArray';
import cloneDeep from 'lodash/cloneDeep';
import { getFullPathLabel, getTreeValue } from './helper.js';
import 'lodash/isObject';
import 'lodash/isEmpty';

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(max) && max < 0) console.warn("TDesign Warn:", "max should > 0");
  if (checked.length > max && isNumber(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(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(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(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(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.js.map
