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

'use strict';

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

var pick = require('lodash/pick');
var treeStore = require('../../../chunks/tree-store.js');
require('../../../chunks/defineProperty-19da3d4c.js');
require('../../../chunks/get-fb5b05c8.js');
require('../../../chunks/isArray-6b6e7094.js');
require('../../../chunks/isSymbol-0ea4cedf.js');
require('../../../chunks/isObjectLike-51896b77.js');
require('../../../chunks/_commonjsHelpers-fe796db6.js');
require('../../../chunks/isFunction-cb08cfdf.js');
require('../../../chunks/tree-node.js');
require('../../../chunks/log.js');
require('../../../chunks/_isIndex-8b06cfbb.js');

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

var pick__default = /*#__PURE__*/_interopDefaultLegacy(pick);

function useTreeStore(props, context) {
  const {
    actived,
    value,
    valueMode,
    filter,
    keys
  } = props;
  const store = new treeStore.TreeStore({
    valueMode,
    filter
  });
  const updateStoreConfig = () => {
    const storeProps = pick__default["default"](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
  };
}

exports["default"] = useTreeStore;
//# sourceMappingURL=useTreeStore.js.map
