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

import pick from 'lodash/pick';
import { TreeStore } from '../../_common/js/tree/tree-store.js';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '@babel/runtime/helpers/defineProperty';
import 'lodash/isArray';
import 'lodash/isFunction';
import 'lodash/isNumber';
import 'lodash/isString';
import 'lodash/difference';
import 'lodash/camelCase';
import 'lodash/isPlainObject';
import 'mitt';
import '../../_common/js/tree/tree-node.js';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/helpers/toConsumableArray';
import '@babel/runtime/regenerator';
import 'lodash/isNull';
import 'lodash/uniqueId';
import 'lodash/isNil';
import 'lodash/get';
import '../../_common/js/log/log.js';
import '../../_common/js/tree/tree-node-model.js';
import 'lodash/isUndefined';
import 'lodash/isBoolean';
import 'lodash/omit';

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

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