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

'use strict';

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

var pick = require('lodash/pick');
var _common_js_tree_treeStore = require('../../_common/js/tree/tree-store.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('@babel/runtime/helpers/defineProperty');
require('lodash/isArray');
require('lodash/isFunction');
require('lodash/isNumber');
require('lodash/isString');
require('lodash/difference');
require('lodash/camelCase');
require('lodash/isPlainObject');
require('mitt');
require('../../_common/js/tree/tree-node.js');
require('@babel/runtime/helpers/asyncToGenerator');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/regenerator');
require('lodash/isNull');
require('lodash/uniqueId');
require('lodash/isNil');
require('lodash/get');
require('../../_common/js/log/log.js');
require('../../_common/js/tree/tree-node-model.js');
require('lodash/isUndefined');
require('lodash/isBoolean');
require('lodash/omit');

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

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

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

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