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

import { _ as _defineProperty } from '../_chunks/dep-eb20e23f.mjs';
import { _ as _slicedToArray } from '../_chunks/dep-55846619.mjs';
import { ref, toRefs, createVNode, nextTick, watch } from 'vue';
import { i as isArray_1 } from '../_chunks/dep-e3b14d70.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-b4041200.mjs';
import '../_chunks/dep-cfcedf47.mjs';
import '../_chunks/dep-a0b4a044.mjs';
import '../_chunks/dep-b343e307.mjs';
import '../_chunks/dep-6b22dd27.mjs';
import '../_chunks/dep-c959a6fb.mjs';
import '../_chunks/dep-0f0c89ff.mjs';
import '../_chunks/dep-6eea369f.mjs';
import '../_chunks/dep-c31e1d9e.mjs';
import '../_chunks/dep-1cf2b129.mjs';
import '../_chunks/dep-66d6733b.mjs';
import '../_chunks/dep-5d966cee.mjs';
import '../_chunks/dep-0bfbf721.mjs';
import '../_chunks/dep-39cc276d.mjs';
import '../_chunks/dep-8f777748.mjs';
import '../_chunks/dep-ae357f26.mjs';
import '../_chunks/dep-37ba476d.mjs';
import '../_chunks/dep-be27d184.mjs';
import '../_chunks/dep-1b058de3.mjs';
import '../_chunks/dep-774ac2d0.mjs';
import '../_chunks/dep-23a0f453.mjs';
import '../_chunks/dep-05fa17c9.mjs';
import '../_chunks/dep-3afa94cf.mjs';
import '../_chunks/dep-2cfd5b3a.mjs';
import '../_chunks/dep-c7e9ce16.mjs';
import '../_chunks/dep-92e7085e.mjs';
import '../_chunks/dep-10085c61.mjs';
import '../_chunks/dep-f6649a46.mjs';
import '../_chunks/dep-1fe0ad42.mjs';
import '../_chunks/dep-d54f2690.mjs';
import '../_chunks/dep-59c8420d.mjs';
import '../_chunks/dep-74848b92.mjs';
import '../_chunks/dep-ba3c0b2e.mjs';
import '../_chunks/dep-79821ff2.mjs';
import '../_chunks/dep-352691c2.mjs';
import '../_chunks/dep-a9c8a5d5.mjs';
import '../_chunks/dep-c852a61a.mjs';
import 'mitt';
import '../_common/js/tree/tree-node.mjs';
import '../_chunks/dep-beaa677e.mjs';
import '../_chunks/dep-e091cfd9.mjs';
import '../_chunks/dep-6f89b77e.mjs';
import '../_chunks/dep-51aacdc9.mjs';
import '../_chunks/dep-4c3e8dd5.mjs';
import '../_chunks/dep-7acc11cb.mjs';
import '../_common/js/log/index.mjs';
import '../_common/js/log/log.mjs';
import '../_common/js/tree/tree-node-model.mjs';
import '../_chunks/dep-30451c48.mjs';
import '../_chunks/dep-0cfc2d3f.mjs';
import '../_chunks/dep-e9bfe96d.mjs';
import '../_chunks/dep-ea5448a5.mjs';
import '../_chunks/dep-b2af48ba.mjs';
import '../_chunks/dep-bf74f1c2.mjs';
import '../_chunks/dep-44f056cd.mjs';
import '../_chunks/dep-c4e1a112.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-99920870.mjs';
import '../_chunks/dep-ee57d235.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-6b4aa943.mjs';
import '../_chunks/dep-d52291a9.mjs';
import '../hooks/useConfig.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-44417261.mjs';
import '../config-provider/type.mjs';
import '../hooks/tnode.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-bd03960e.mjs';
import '../checkbox/props.mjs';
import '../checkbox/constants.mjs';
import '../checkbox/group.mjs';
import '../_chunks/dep-0492e83e.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-b6a73f93.mjs';
import '../loading/style/css.mjs';
import '../loading/type.mjs';
import '../hooks/useGlobalIcon.mjs';
import './constants.mjs';
import './hooks/useDraggable.mjs';
import '../_chunks/dep-b069d97f.mjs';
import '../_chunks/dep-96bb54d5.mjs';
import '../_chunks/dep-6205fc47.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
