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

import { _ as _defineProperty } from '../../../_chunks/dep-2db7b3ec.mjs';
import { i as isUndefined_1 } from '../../../_chunks/dep-78847b2e.mjs';
import { i as isBoolean_1 } from '../../../_chunks/dep-35e3b1aa.mjs';
import { p as pick_1 } from '../../../_chunks/dep-8932db97.mjs';
import { o as omit_1 } from '../../../_chunks/dep-350764bc.mjs';
import log from '../log/log.mjs';
import '../../../_chunks/dep-255303ba.mjs';
import '../../../_chunks/dep-eba0ea6e.mjs';
import '../../../_chunks/dep-469b4ae0.mjs';
import '../../../_chunks/dep-3da99438.mjs';
import '../../../_chunks/dep-0d3dd4b5.mjs';
import '../../../_chunks/dep-b57180d0.mjs';
import '../../../_chunks/dep-c8d7b6b4.mjs';
import '../../../_chunks/dep-2bc37d52.mjs';
import '../../../_chunks/dep-bc1d737a.mjs';
import '../../../_chunks/dep-4d7a675b.mjs';
import '../../../_chunks/dep-5717e263.mjs';
import '../../../_chunks/dep-a8bcacea.mjs';
import '../../../_chunks/dep-2e7f59b1.mjs';
import '../../../_chunks/dep-855db8bc.mjs';
import '../../../_chunks/dep-42cc3015.mjs';
import '../../../_chunks/dep-550c24c1.mjs';
import '../../../_chunks/dep-a87f3e4c.mjs';
import '../../../_chunks/dep-1e566bb5.mjs';
import '../../../_chunks/dep-42a19209.mjs';
import '../../../_chunks/dep-1434aa15.mjs';
import '../../../_chunks/dep-9fe2ef12.mjs';
import '../../../_chunks/dep-6884627f.mjs';
import '../../../_chunks/dep-8d3e9aa3.mjs';
import '../../../_chunks/dep-0fc02184.mjs';
import '../../../_chunks/dep-c989dff3.mjs';
import '../../../_chunks/dep-9cf86cf1.mjs';
import '../../../_chunks/dep-e5eb9f23.mjs';
import '../../../_chunks/dep-672e7d1a.mjs';
import '../../../_chunks/dep-9262ee2f.mjs';
import '../../../_chunks/dep-334a4f6b.mjs';
import '../../../_chunks/dep-8b989474.mjs';
import '../../../_chunks/dep-f1994936.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.indexOf(node.value) < 0) {
        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
