/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

import pick from 'lodash/pick';
import { T as TreeStore } from '../../../chunks/tree-store.js';
import '../../../chunks/defineProperty-19da3d4c.js';
import '../../../chunks/get-fb5b05c8.js';
import '../../../chunks/isArray-6b6e7094.js';
import '../../../chunks/isSymbol-0ea4cedf.js';
import '../../../chunks/isObjectLike-51896b77.js';
import '../../../chunks/_commonjsHelpers-fe796db6.js';
import '../../../chunks/isFunction-cb08cfdf.js';
import '../../../chunks/tree-node.js';
import '../../../chunks/log.js';
import '../../../chunks/_isIndex-8b06cfbb.js';

function useTreeStore(props, context) {
  const {
    actived,
    value,
    valueMode,
    filter,
    keys
  } = props;
  const store = new TreeStore({
    valueMode,
    filter
  });
  const updateStoreConfig = () => {
    const storeProps = pick(props, ["expandAll", "expandLevel", "expandMutex", "expandParent", "activable", "activeMultiple", "disabled", "checkable", "draggable", "checkStrictly", "load", "lazy", "valueMode", "filter"]);
    store.setConfig(storeProps);
  };
  const updateExpanded = () => {
    const {
      expanded,
      expandParent
    } = props;
    if (!Array.isArray(expanded)) return;
    const expandedMap = /* @__PURE__ */new Map();
    expanded.forEach(val => {
      expandedMap.set(val, true);
      if (expandParent) {
        const node = store.getNode(val);
        if (node) {
          node.getModel().getParents().forEach(tn => {
            expandedMap.set(tn.value, true);
          });
        }
      }
    });
    const expandedArr = Array.from(expandedMap.keys());
    store.setExpanded(expandedArr);
  };
  const handleLoad = info => {
    const {
      node
    } = info;
    const evtCtx = {
      node: node.getModel()
    };
    const {
      value: value2,
      expanded,
      actived: actived2
    } = props;
    if (value2 && value2.length > 0) store.replaceChecked(value2);
    if (expanded && expanded.length > 0) store.replaceExpanded(expanded);
    if (actived2 && actived2.length > 0) store.replaceActived(actived2);
    if (props?.onLoad) props?.onLoad(evtCtx);
    context.emit("load", evtCtx);
  };
  const rebuild = list => {
    const {
      value: value2,
      actived: actived2
    } = props;
    store.reload(list || []);
    if (Array.isArray(value2)) store.setChecked(value2);
    updateExpanded();
    if (Array.isArray(actived2)) store.setActived(actived2);
    store.refreshState();
  };
  store.setConfig({
    keys
  });
  updateStoreConfig();
  store.append(props.data || []);
  store.refreshNodes();
  if (Array.isArray(value)) store.setChecked(value);
  updateExpanded();
  if (Array.isArray(actived)) store.setActived(actived);
  store.emitter.on("load", handleLoad);
  return {
    store,
    rebuild,
    updateStoreConfig,
    updateExpanded
  };
}

export { useTreeStore as default };
//# sourceMappingURL=useTreeStore.js.map
