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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import isUndefined from 'lodash/isUndefined';
import isBoolean from 'lodash/isBoolean';
import pick from 'lodash/pick';
import omit from 'lodash/omit';
import log from '../log/log.js';

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(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(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(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(dataValue)) _data.value = dataValue;
      if (!isUndefined(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.js.map
