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

import { _ as _defineProperty } from '../_chunks/dep-2db7b3ec.mjs';
import { _ as _slicedToArray } from '../_chunks/dep-84acf3c2.mjs';
import { ref, toRefs, createVNode, nextTick, watch } from 'vue';
import { i as isArray_1 } from '../_chunks/dep-b57180d0.mjs';
import { TreeStore } from '../_common/js/tree/tree-store.mjs';
import useDefaultValue from '../hooks/useDefaultValue.mjs';
import useVModel from '../hooks/useVModel.mjs';
import TreeItem from './tree-item.mjs';
import useOnDrag from './hooks/useOnDrag.mjs';
import { getMark, getNode, getStoreConfig } from './util.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-ccbc47f4.mjs';
import '../_chunks/dep-5717e263.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-a8bcacea.mjs';
import '../_chunks/dep-3f21269a.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../_chunks/dep-876fb3ec.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-c42916c7.mjs';
import '../_chunks/dep-bc1d737a.mjs';
import '../_chunks/dep-4d7a675b.mjs';
import '../_chunks/dep-2e7f59b1.mjs';
import '../_chunks/dep-855db8bc.mjs';
import '../_chunks/dep-42cc3015.mjs';
import '../_chunks/dep-2bc37d52.mjs';
import '../_chunks/dep-672e7d1a.mjs';
import '../_chunks/dep-6884627f.mjs';
import '../_chunks/dep-8d3e9aa3.mjs';
import '../_chunks/dep-1434aa15.mjs';
import '../_chunks/dep-0fc02184.mjs';
import '../_chunks/dep-1e566bb5.mjs';
import '../_chunks/dep-dcc83161.mjs';
import '../_chunks/dep-9262ee2f.mjs';
import '../_chunks/dep-9fe2ef12.mjs';
import '../_chunks/dep-406cd044.mjs';
import '../_chunks/dep-327559e8.mjs';
import '../_chunks/dep-b2235f26.mjs';
import '../_chunks/dep-8b989474.mjs';
import '../_chunks/dep-1372b2d3.mjs';
import '../_chunks/dep-f1994936.mjs';
import '../_chunks/dep-9cf86cf1.mjs';
import '../_chunks/dep-a87f3e4c.mjs';
import '../_chunks/dep-e5eb9f23.mjs';
import '../_chunks/dep-42a19209.mjs';
import 'mitt';
import '../_common/js/tree/tree-node.mjs';
import '../_chunks/dep-38ee6a8f.mjs';
import '../_chunks/dep-bf1e33c8.mjs';
import '../_chunks/dep-ed5ebb35.mjs';
import '../_chunks/dep-01e3f446.mjs';
import '../_chunks/dep-c81c0ee4.mjs';
import '../_chunks/dep-0d3dd4b5.mjs';
import '../_common/js/tree/tree-node-model.mjs';
import '../_chunks/dep-78847b2e.mjs';
import '../_chunks/dep-35e3b1aa.mjs';
import '../_chunks/dep-8932db97.mjs';
import '../_chunks/dep-550c24c1.mjs';
import '../_chunks/dep-350764bc.mjs';
import '../_chunks/dep-c989dff3.mjs';
import '../_chunks/dep-334a4f6b.mjs';
import '../_common/js/log/log.mjs';
import '../_common/js/log/index.mjs';
import '../_chunks/dep-9a85dac1.mjs';
import '@ldesign/icons-vue-next';
import '../checkbox/index.mjs';
import '../utils/withInstall.mjs';
import '../checkbox/checkbox.mjs';
import '../form/hooks.mjs';
import '../hooks/useRipple.mjs';
import '../utils/set-style.mjs';
import '../hooks/useKeepAnimation.mjs';
import '../_chunks/dep-40d69a9c.mjs';
import '../_chunks/dep-345e1be3.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-9888611c.mjs';
import '../_chunks/dep-697a1365.mjs';
import '../hooks/useConfig.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-095107f7.mjs';
import '../config-provider/type.mjs';
import '../hooks/tnode.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-888e80c4.mjs';
import '../checkbox/props.mjs';
import '../checkbox/constants.mjs';
import '../checkbox/group.mjs';
import '../_chunks/dep-85ed244c.mjs';
import '../hooks/slot.mjs';
import '../checkbox/checkbox-group-props.mjs';
import '../checkbox/style/css.mjs';
import '../checkbox/type.mjs';
import '../loading/index.mjs';
import '../loading/directive.mjs';
import '../loading/plugin.mjs';
import '../utils/dom.mjs';
import '../utils/easing.mjs';
import '../loading/loading.mjs';
import '../hooks/useTeleport.mjs';
import '../loading/props.mjs';
import '../loading/icon/gradient.mjs';
import '../_common/js/loading/circle-adapter.mjs';
import '../_common/js/utils/set-style.mjs';
import '../_common/js/utils/helper.mjs';
import '../_chunks/dep-60fae668.mjs';
import '../loading/style/css.mjs';
import '../loading/type.mjs';
import '../hooks/useGlobalIcon.mjs';
import './constants.mjs';
import './hooks/useDraggable.mjs';
import '../_chunks/dep-4f3ad3f8.mjs';
import '../_chunks/dep-9b6cf76d.mjs';
import '../_chunks/dep-f709f709.mjs';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function useTree(props) {
  var treeStore = ref();
  var cacheMap = /* @__PURE__ */new Map();
  var treeNodeViews = ref([]);
  var _toRefs = toRefs(props),
    expanded = _toRefs.expanded,
    actived = _toRefs.actived,
    value = _toRefs.value,
    modelValue = _toRefs.modelValue;
  var _useVModel = useVModel(value, modelValue, props.defaultValue, props.onChange),
    _useVModel2 = _slicedToArray(_useVModel, 2),
    innerChecked = _useVModel2[0],
    setInnerChecked = _useVModel2[1];
  var _useDefaultValue = useDefaultValue(actived, props.defaultActived, props.onActive, "actived"),
    _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
    innerActived = _useDefaultValue2[0],
    setInnerActived = _useDefaultValue2[1];
  var _useDefaultValue3 = useDefaultValue(expanded, props.defaultExpanded, props.onExpand, "expanded"),
    _useDefaultValue4 = _slicedToArray(_useDefaultValue3, 2),
    innerExpanded = _useDefaultValue4[0],
    setInnerExpanded = _useDefaultValue4[1];
  var handleLoad = function handleLoad(info) {
    var _props$onLoad;
    var node = info.node;
    var ctx = {
      node: node.getModel()
    };
    if (innerChecked.value && innerChecked.value.length > 0) treeStore.value.replaceChecked(innerChecked.value);
    if (innerExpanded.value && innerExpanded.value.length > 0) treeStore.value.replaceExpanded(innerExpanded.value);
    if (innerActived.value && innerActived.value.length > 0) treeStore.value.replaceActived(innerActived.value);
    (_props$onLoad = props.onLoad) === null || _props$onLoad === void 0 ? void 0 : _props$onLoad.call(props, ctx);
  };
  var handleClick = function handleClick(state) {
    var _props$onClick;
    var expandOnClickNode = props.expandOnClickNode;
    var mouseEvent = state.mouseEvent,
      event = state.event,
      node = state.node;
    if (!node) return;
    var shouldExpand = expandOnClickNode;
    var shouldActive = !props.disabled && !node.disabled;
    ["trigger", "ignore"].forEach(function (markName) {
      var mark = getMark(markName, event.target, event.currentTarget);
      var markValue = (mark === null || mark === void 0 ? void 0 : mark.value) || "";
      if (markValue.includes("expand")) {
        if (markName === "trigger") {
          shouldExpand = true;
        } else if (markName === "ignore") {}
      }
      if (markValue.includes("active")) {
        if (markName === "ignore") shouldActive = false;
      }
    });
    var ctx = {
      node: node.getModel(),
      e: mouseEvent
    };
    if (shouldExpand) {
      var tnode = getNode(treeStore.value, node);
      var expanded2 = node.setExpanded(!tnode.isExpanded());
      setInnerExpanded(expanded2, ctx);
    }
    if (shouldActive) {
      var _tnode = getNode(treeStore.value, node);
      var actived2 = node.setActived(!_tnode.isActived());
      setInnerActived(actived2, ctx);
    }
    (_props$onClick = props.onClick) === null || _props$onClick === void 0 ? void 0 : _props$onClick.call(props, ctx);
  };
  var handleChange = function handleChange(state) {
    var disabled = props.disabled;
    var node = state.node,
      mouseEvent = state.mouseEvent;
    if (!node || disabled || node.disabled) return;
    var tnode = getNode(treeStore.value, node);
    var checked = node.setChecked(!tnode.isChecked());
    setInnerChecked(checked, {
      node: node.getModel(),
      e: mouseEvent
    });
  };
  var renderTreeNodeViews = function renderTreeNodeViews() {
    var nodes = treeStore.value.getNodes();
    treeNodeViews.value = nodes.filter(function (node) {
      return node.visible;
    }).map(function (node) {
      var nodeView = cacheMap.get(node.value);
      if (!nodeView) {
        nodeView = createVNode(TreeItem, {
          "key": node.value,
          "node": node,
          "onChange": handleChange,
          "onClick": handleClick,
          "expandOnClickNode": props.expandOnClickNode
        }, null);
        cacheMap.set(node.value, nodeView);
      }
      return nodeView;
    });
    nextTick(function () {
      cacheMap.forEach(function (view, value2) {
        if (!treeStore.value.getNode(value2)) cacheMap["delete"](value2);
      });
    });
  };
  useOnDrag(treeStore);
  var updateExpanded = function updateExpanded() {
    var expandParent = props.expandParent;
    if (isArray_1(innerExpanded.value)) {
      var expandedMap = /* @__PURE__ */new Map();
      innerExpanded.value.forEach(function (val) {
        expandedMap.set(val, true);
        if (expandParent) {
          var node = treeStore.value.getNode(val);
          node.getParents().forEach(function (tn) {
            expandedMap.set(tn.value, true);
          });
        }
      });
      var expandedArr = Array.from(expandedMap.keys());
      treeStore.value.setExpanded(expandedArr);
    }
  };
  var init = function init() {
    var options = props.data;
    var store = new TreeStore(_objectSpread(_objectSpread({}, getStoreConfig(props)), {}, {
      onLoad: function onLoad(info) {
        handleLoad(info);
      },
      onUpdate: function onUpdate() {
        renderTreeNodeViews();
      }
    }));
    treeStore.value = store;
    if (!isArray_1(options)) options = [];
    store.append(options);
    store.refreshNodes();
    if (isArray_1(innerChecked.value)) store.setChecked(innerChecked.value);
    updateExpanded();
    if (isArray_1(innerActived.value)) store.setActived(innerActived.value);
    renderTreeNodeViews();
  };
  watch(function () {
    return props.data;
  }, function (list) {
    list = props.data;
    cacheMap.clear();
    treeStore.value.reload(list);
    treeStore.value.refreshNodes();
    if (!list.length) return;
    if (isArray_1(innerChecked.value)) treeStore.value.setChecked(innerChecked.value);
    updateExpanded();
    if (isArray_1(innerActived.value)) treeStore.value.setActived(innerActived.value);
    treeStore.value.refreshState();
  });
  watch(function () {
    return getStoreConfig(props);
  }, function () {
    if (!treeStore.value) return;
    treeStore.value.setConfig(getStoreConfig(props));
  });
  watch(innerChecked, function (nVal) {
    treeStore.value.replaceChecked(nVal);
  });
  watch(innerExpanded, function (nVal) {
    treeStore.value.replaceExpanded(nVal);
  });
  watch(innerActived, function (nVal) {
    treeStore.value.replaceActived(nVal);
  });
  init();
  return {
    treeStore: treeStore,
    treeNodeViews: treeNodeViews
  };
}

export { useTree as default };
//# sourceMappingURL=useTree.mjs.map
