/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var isNumber = require('lodash/isNumber');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var isNumber__default = /*#__PURE__*/_interopDefaultLegacy(isNumber);

function getPanels(treeNodes) {
  var panels = [];
  treeNodes.forEach(function (node) {
    if (panels[node.level]) {
      panels[node.level].push(node);
    } else {
      panels[node.level] = [node];
    }
  });
  return panels;
}
function expendClickEffect(propsTrigger, trigger, node, cascaderContext) {
  var checkStrictly = cascaderContext.checkStrictly,
      filterActive = cascaderContext.filterActive,
      multiple = cascaderContext.multiple,
      treeStore = cascaderContext.treeStore,
      setFilterActive = cascaderContext.setFilterActive,
      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.isChecked());

    var _checked = _slicedToArray__default["default"](checked, 1),
        value2 = _checked[0];

    if (filterActive) {
      setFilterActive(false);
    }

    if (!filterActive && !checkStrictly) {
      setVisible(false);
    }

    setValue(valueType === "single" ? value2 : node.getPath().map(function (item) {
      return item.value;
    }), "checked", node.getModel());
  }
}
function valueChangeEffect(node, cascaderContext) {
  var disabled = cascaderContext.disabled,
      max = cascaderContext.max,
      multiple = cascaderContext.multiple,
      setVisible = cascaderContext.setVisible,
      setValue = cascaderContext.setValue,
      filterActive = cascaderContext.filterActive,
      setFilterActive = cascaderContext.setFilterActive,
      treeNodes = cascaderContext.treeNodes,
      treeStore = cascaderContext.treeStore,
      valueType = cascaderContext.valueType;

  if (!node || disabled || node.disabled) {
    return;
  }

  var checked = node.setChecked(!node.isChecked());

  if (isNumber__default["default"](max) && max < 0) {
    console.warn("TDesign Warn:", "max should > 0");
  }

  if (checked.length > max && isNumber__default["default"](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.indexOf(item.value) > -1;
  });

  if (filterActive && isSelectAll) {
    setVisible(false);
    setFilterActive(false);
  }

  var resValue = valueType === "single" ? checked : checked.map(function (val) {
    return treeStore.getNode(val).getPath().map(function (item) {
      return item.value;
    });
  });
  setValue(resValue, "checked", node.getModel());
}

exports.expendClickEffect = expendClickEffect;
exports.getPanels = getPanels;
exports.valueChangeEffect = valueChangeEffect;
//# sourceMappingURL=panel.js.map
