/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import isFunction from 'lodash/isFunction';
import { CaretRightSmallIcon } from 'tdesign-icons-vue';
import mixins from '../utils/mixins.js';
import getConfigReceiverMixins from '../config-provider/config-receiver.js';
import { Checkbox } from '../checkbox/index.js';
import { Loading } from '../loading/index.js';
import { TreeNode } from '../_common/js/tree/tree-node.js';
import { getTNode } from './util.js';
import { TREE_NODE_NAME, CLASS_NAMES } from './constants.js';
import Ripple from '../utils/ripple.js';
import 'vue';
import '../config-provider/zh_CN_config.js';
import '../config-provider/type.js';
import '../checkbox/group.js';
import '@babel/runtime/helpers/toConsumableArray';
import '../_chunks/dep-42d5c87e.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/intersection';
import '../config.js';
import '../checkbox/checkbox.js';
import '../utils/render-tnode.js';
import '../utils/classnames.js';
import '../checkbox/props.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import '../utils/event.js';
import '../checkbox/checkbox-group-props.js';
import '../utils/map-props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/css.js';
import '../checkbox/type.js';
import '../loading/loading.js';
import '../loading/icon/gradient.js';
import '../utils/set-style.js';
import '../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';
import '../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/type.js';
import '../loading/plugin.js';
import '@babel/runtime/helpers/asyncToGenerator';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '@babel/runtime/regenerator';
import 'lodash/uniqueId';
import 'lodash/get';
import '../_common/js/tree/tree-node-model.js';
import 'lodash/pick';

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; }
var TreeItemProps = {
  node: {
    type: TreeNode
  },
  treeScope: {
    type: Object
  }
};
var TreeItem = mixins(getConfigReceiverMixins("tree")).extend({
  name: TREE_NODE_NAME,
  props: TreeItemProps,
  directives: {
    ripple: Ripple
  },
  data: function data() {
    return {
      data: null
    };
  },
  methods: {
    getStyles: function getStyles() {
      var level = this.node.level;
      var styles = "--level: ".concat(level, ";");
      return styles;
    },
    getClassList: function getClassList() {
      var _list$push;

      var node = this.node;
      var list = [];
      list.push(CLASS_NAMES.treeNode);
      list.push((_list$push = {}, _defineProperty(_list$push, CLASS_NAMES.treeNodeOpen, node.expanded), _defineProperty(_list$push, CLASS_NAMES.actived, node.isActivable() ? node.actived : false), _defineProperty(_list$push, CLASS_NAMES.disabled, node.isDisabled()), _list$push));

      if (node.visible) {
        list.push(CLASS_NAMES.treeNodeVisible);
      } else {
        list.push(CLASS_NAMES.treeNodeHidden);
      }

      return list;
    },
    renderLine: function renderLine(createElement) {
      var h = this.$createElement;
      var node = this.node,
          treeScope = this.treeScope;
      var line = treeScope.line,
          scopedSlots = treeScope.scopedSlots;
      var iconVisible = !!treeScope.icon;
      var lineNode = null;

      if (line === true) {
        if (scopedSlots !== null && scopedSlots !== void 0 && scopedSlots.line) {
          lineNode = scopedSlots.line({
            node: node === null || node === void 0 ? void 0 : node.getModel()
          });
        } else if (node.parent && node.tree) {
          var vmIsLeaf = node.vmIsLeaf,
              vmIsFirst = node.vmIsFirst,
              level = node.level;
          var lineClasses = [];
          lineClasses.push(CLASS_NAMES.line);

          if (vmIsLeaf || !iconVisible) {
            lineClasses.push(CLASS_NAMES.lineIsLeaf);
          }

          if (vmIsFirst && iconVisible) {
            lineClasses.push(CLASS_NAMES.lineIsFirst);
          }

          var shadowStyles = [];
          var parents = node.getParents();
          parents.pop();
          parents.forEach(function (pnode, index) {
            if (!pnode.vmIsLast) {
              shadowStyles.push("calc(-".concat(index + 1, " * var(--space)) 0 var(--color)"));
            }
          });
          var styles = {
            "--level": level,
            "box-shadow": shadowStyles.join(",")
          };
          lineNode = h("span", {
            "class": lineClasses,
            "style": styles
          });
        }
      } else {
        lineNode = getTNode(line, {
          createElement: createElement,
          node: node
        });
      }

      return lineNode;
    },
    getFolderIcon: function getFolderIcon() {
      var h = this.$createElement;

      if (isFunction(this.global.folderIcon)) {
        return this.global.folderIcon(this.$createElement);
      }

      return h(CaretRightSmallIcon);
    },
    renderIcon: function renderIcon(createElement) {
      var h = this.$createElement;
      var node = this.node,
          treeScope = this.treeScope;
      var icon = treeScope.icon,
          scopedSlots = treeScope.scopedSlots;
      var isDefaultIcon = false;
      var iconNode = null;

      if (icon === true) {
        if (scopedSlots !== null && scopedSlots !== void 0 && scopedSlots.icon) {
          iconNode = scopedSlots.icon({
            node: node === null || node === void 0 ? void 0 : node.getModel()
          });
        } else if (!node.vmIsLeaf) {
          isDefaultIcon = true;
          iconNode = this.getFolderIcon();

          if (node.loading && node.expanded) {
            iconNode = h(Loading);
          }
        } else {
          iconNode = "";
        }
      } else {
        iconNode = getTNode(icon, {
          createElement: createElement,
          node: node
        });
      }

      iconNode = h("span", {
        "class": [CLASS_NAMES.treeIcon, CLASS_NAMES.folderIcon, isDefaultIcon ? CLASS_NAMES.treeIconDefault : ""],
        "attrs": {
          "trigger": "expand",
          "ignore": "active"
        }
      }, [iconNode]);
      return iconNode;
    },
    renderLabel: function renderLabel(createElement) {
      var _this = this;

      var h = this.$createElement;
      var node = this.node,
          treeScope = this.treeScope;
      var label = treeScope.label,
          scopedSlots = treeScope.scopedSlots,
          disableCheck = treeScope.disableCheck;
      var checkProps = treeScope.checkProps || {};
      var labelNode = null;

      if (label === true) {
        if (scopedSlots !== null && scopedSlots !== void 0 && scopedSlots.label) {
          labelNode = scopedSlots.label({
            node: node === null || node === void 0 ? void 0 : node.getModel()
          });
        } else {
          labelNode = node.label || "";
        }
      } else {
        labelNode = getTNode(label, {
          createElement: createElement,
          node: node
        });
      }

      var labelClasses = [CLASS_NAMES.treeLabel, CLASS_NAMES.treeLabelStrictly, _defineProperty({}, CLASS_NAMES.actived, node.isActivable() ? node.actived : false)];

      if (node.vmCheckable) {
        var checkboxDisabled = false;

        if (typeof disableCheck === "function") {
          checkboxDisabled = disableCheck(node);
        } else {
          checkboxDisabled = !!disableCheck;
        }

        if (node.isDisabled()) {
          checkboxDisabled = true;
        }

        var itemCheckProps = _objectSpread(_objectSpread({}, checkProps), {}, {
          disabled: checkboxDisabled
        });

        labelNode = h(Checkbox, {
          "directives": [{
            name: "ripple",
            value: true
          }],
          "class": labelClasses,
          "attrs": {
            "checked": node.checked,
            "indeterminate": node.indeterminate,
            "disabled": node.isDisabled(),
            "name": String(node.value),
            "ignore": "expand,active"
          },
          "on": {
            "change": function change() {
              return _this.handleChange();
            }
          },
          "props": _objectSpread({}, itemCheckProps)
        }, [labelNode]);
      } else {
        var inner = h("span", {
          "style": "position: relative"
        }, [labelNode]);
        labelNode = node.isActivable() ? h("span", {
          "key": "1",
          "directives": [{
            name: "ripple",
            value: true
          }],
          "class": labelClasses
        }, [inner]) : h("span", {
          "key": "2",
          "class": labelClasses
        }, [inner]);
      }

      return labelNode;
    },
    renderOperations: function renderOperations(createElement) {
      var h = this.$createElement;
      var node = this.node,
          treeScope = this.treeScope;
      var operations = treeScope.operations,
          scopedSlots = treeScope.scopedSlots;
      var opNode = null;

      if (scopedSlots !== null && scopedSlots !== void 0 && scopedSlots.operations) {
        opNode = scopedSlots.operations({
          node: node === null || node === void 0 ? void 0 : node.getModel()
        });
      } else {
        opNode = getTNode(operations, {
          createElement: createElement,
          node: node
        });
      }

      if (opNode) {
        opNode = h("span", {
          "class": CLASS_NAMES.treeOperations,
          "attrs": {
            "ignore": "active,expand"
          }
        }, [opNode]);
      }

      return opNode;
    },
    renderItem: function renderItem(createElement) {
      var itemNodes = [];
      var iconNode = this.renderIcon(createElement);
      var lineNode = this.renderLine(createElement);

      if (lineNode) {
        itemNodes.push(lineNode);
      }

      if (iconNode) {
        itemNodes.push(iconNode);
      }

      var labelNode = this.renderLabel(createElement);

      if (labelNode) {
        itemNodes.push(labelNode);
      }

      var opNode = this.renderOperations(createElement);

      if (opNode) {
        itemNodes.push(opNode);
      }

      return itemNodes;
    },
    handleClick: function handleClick(evt) {
      var node = this.node;
      var state = {
        mouseEvent: evt,
        event: evt,
        node: node,
        path: node.getPath()
      };
      this.$emit("click", state);
    },
    handleChange: function handleChange() {
      var node = this.node;
      var event = new Event("change");
      var state = {
        event: event,
        node: node
      };
      this.$emit("change", state);
    }
  },
  created: function created() {
    if (this.node) {
      this.data = this.node.data;
    }
  },
  render: function render(createElement) {
    var _this2 = this;

    var h = arguments[0];
    var node = this.node;
    var tree = node.tree,
        level = node.level,
        value = node.value;

    if (!tree || !tree.nodeMap.get(value)) {
      this.$destroy();
    }

    var styles = this.getStyles();
    var classList = this.getClassList();
    return h("div", {
      "class": classList,
      "attrs": {
        "data-value": node.value,
        "data-level": level
      },
      "style": styles,
      "on": {
        "click": function click(evt) {
          return _this2.handleClick(evt);
        }
      }
    }, [this.renderItem(createElement)]);
  }
});

export { TreeItemProps, TreeItem as default };
//# sourceMappingURL=tree-item.js.map
