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

import { _ as _defineProperty } from '../../../_chunks/dep-eb20e23f.mjs';
import { i as isUndefined_1 } from '../../../_chunks/dep-30451c48.mjs';
import { i as isBoolean_1 } from '../../../_chunks/dep-0cfc2d3f.mjs';
import { p as pick_1 } from '../../../_chunks/dep-e9bfe96d.mjs';
import { o as omit_1 } from '../../../_chunks/dep-b2af48ba.mjs';
import log from '../log/log.mjs';
import '../../../_chunks/dep-b4041200.mjs';
import '../../../_chunks/dep-a0b4a044.mjs';
import '../../../_chunks/dep-c959a6fb.mjs';
import '../../../_chunks/dep-c31e1d9e.mjs';
import '../../../_chunks/dep-7acc11cb.mjs';
import '../../../_chunks/dep-e3b14d70.mjs';
import '../../../_chunks/dep-66d6733b.mjs';
import '../../../_chunks/dep-be27d184.mjs';
import '../../../_chunks/dep-0bfbf721.mjs';
import '../../../_chunks/dep-39cc276d.mjs';
import '../../../_chunks/dep-6b22dd27.mjs';
import '../../../_chunks/dep-0f0c89ff.mjs';
import '../../../_chunks/dep-8f777748.mjs';
import '../../../_chunks/dep-ae357f26.mjs';
import '../../../_chunks/dep-37ba476d.mjs';
import '../../../_chunks/dep-ea5448a5.mjs';
import '../../../_chunks/dep-352691c2.mjs';
import '../../../_chunks/dep-2cfd5b3a.mjs';
import '../../../_chunks/dep-c852a61a.mjs';
import '../../../_chunks/dep-05fa17c9.mjs';
import '../../../_chunks/dep-10085c61.mjs';
import '../../../_chunks/dep-774ac2d0.mjs';
import '../../../_chunks/dep-23a0f453.mjs';
import '../../../_chunks/dep-3afa94cf.mjs';
import '../../../_chunks/dep-bf74f1c2.mjs';
import '../../../_chunks/dep-79821ff2.mjs';
import '../../../_chunks/dep-a9c8a5d5.mjs';
import '../../../_chunks/dep-1b058de3.mjs';
import '../../../_chunks/dep-92e7085e.mjs';
import '../../../_chunks/dep-44f056cd.mjs';
import '../../../_chunks/dep-59c8420d.mjs';
import '../../../_chunks/dep-ba3c0b2e.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 getExposedProps(node) {
  var props = pick_1(node, ["value", "label", "data", "actived", "expanded", "checked", "indeterminate", "loading"]);
  return props;
}
function createNodeModel(node) {
  var props = getExposedProps(node);
  var model = _objectSpread(_objectSpread({}, props), {}, {
    getLevel: function getLevel() {
      return node.getLevel();
    },
    getIndex: function getIndex() {
      return node.getIndex();
    },
    isFirst: function isFirst() {
      return node.isFirst();
    },
    isLast: function isLast() {
      return node.isLast();
    },
    isLeaf: function isLeaf() {
      return node.isLeaf();
    },
    insertBefore: function insertBefore(newData) {
      return node.insertBefore(newData);
    },
    insertAfter: function insertAfter(newData) {
      return node.insertAfter(newData);
    },
    appendData: function appendData(data) {
      return node.append(data);
    },
    getPath: function getPath() {
      var nodes = node.getPath();
      return nodes.map(function (item) {
        return item.getModel();
      });
    },
    getParent: function getParent() {
      var _node$parent;
      return (_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.getModel();
    },
    getParents: function getParents() {
      var nodes = node.getParents();
      return nodes.map(function (item) {
        return item.getModel();
      });
    },
    getRoot: function getRoot() {
      var root = node.getRoot();
      return root === null || root === void 0 ? void 0 : root.getModel();
    },
    getSiblings: function getSiblings() {
      var nodes = node.getSiblings();
      return nodes.map(function (item) {
        return item.getModel();
      });
    },
    getChildren: function getChildren(deep) {
      var childrenModel = false;
      var children = node.children;
      if (Array.isArray(children)) {
        if (children.length > 0) {
          if (deep) {
            var nodes = node.walk();
            nodes.shift();
            childrenModel = nodes.map(function (item) {
              return item.getModel();
            });
          } else {
            childrenModel = children.map(function (item) {
              return item.getModel();
            });
          }
        } else {
          childrenModel = false;
        }
      } else if (isBoolean_1(children)) {
        childrenModel = children;
      }
      return childrenModel;
    },
    remove: function remove(value) {
      if (!value) {
        node.remove();
        return;
      }
      var tree = node.tree;
      var targetNode = tree.getNode(value);
      if (!targetNode) {
        log.warnOnce("Tree", "`".concat(value, "` is not exist"));
        return;
      }
      var parents = targetNode.getParents();
      var parentValues = parents.map(function (pnode) {
        return pnode.value;
      });
      if (!parentValues.includes(node.value)) {
        log.warnOnce("Tree", "`".concat(value, "` is not a childNode of current node"));
        return;
      }
      targetNode.remove();
    },
    setData: function setData(data) {
      var _data = omit_1(data, ["children", "value", "label"]);
      var keys = node.tree.config.keys;
      var dataValue = data[(keys === null || keys === void 0 ? void 0 : keys.value) || "value"];
      var dataLabel = data[(keys === null || keys === void 0 ? void 0 : keys.label) || "label"];
      if (!isUndefined_1(dataValue)) _data.value = dataValue;
      if (!isUndefined_1(dataLabel)) _data.label = dataLabel;
      Object.assign(node.data, _data);
      Object.assign(node, _data);
    }
  });
  return model;
}
function updateNodeModel(model, node) {
  var props = getExposedProps(node);
  Object.assign(model, props);
}

export { createNodeModel, updateNodeModel };
//# sourceMappingURL=tree-node-model.mjs.map
