import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";
import { watchEffect, ref, defineComponent } from 'vue';
import classNames from '../_util/classNames';
import VcTree, { TreeNode } from '../vc-tree';
import PropTypes from '../_util/vue-types';
import { filterEmpty } from '../_util/props-util';
import initDefaultProps from '../_util/props-util/initDefaultProps';
import { treeProps as vcTreeProps } from '../vc-tree/props';
import useConfigInject from '../_util/hooks/useConfigInject';
import renderSwitcherIcon from './utils/iconUtil';
import dropIndicatorRender from './utils/dropIndicator';
import devWarning from '../vc-util/devWarning';
import { warning } from '../vc-util/warning';
export var treeProps = function treeProps() {
  return _objectSpread(_objectSpread({}, vcTreeProps()), {}, {
    showLine: {
      type: Boolean,
      default: undefined
    },

    /** 是否支持多选 */
    multiple: {
      type: Boolean,
      default: undefined
    },

    /** 是否自动展开父节点 */
    autoExpandParent: {
      type: Boolean,
      default: undefined
    },

    /** checkable状态下节点选择完全受控（父子节点选中状态不再关联）*/
    checkStrictly: {
      type: Boolean,
      default: undefined
    },

    /** 是否支持选中 */
    checkable: {
      type: Boolean,
      default: undefined
    },

    /** 是否禁用树 */
    disabled: {
      type: Boolean,
      default: undefined
    },

    /** 默认展开所有树节点 */
    defaultExpandAll: {
      type: Boolean,
      default: undefined
    },

    /** 默认展开对应树节点 */
    defaultExpandParent: {
      type: Boolean,
      default: undefined
    },

    /** 默认展开指定的树节点 */
    defaultExpandedKeys: {
      type: Array
    },

    /** （受控）展开指定的树节点 */
    expandedKeys: {
      type: Array
    },

    /** （受控）选中复选框的树节点 */
    checkedKeys: {
      type: [Array, Object]
    },

    /** 默认选中复选框的树节点 */
    defaultCheckedKeys: {
      type: Array
    },

    /** （受控）设置选中的树节点 */
    selectedKeys: {
      type: Array
    },

    /** 默认选中的树节点 */
    defaultSelectedKeys: {
      type: Array
    },
    selectable: {
      type: Boolean,
      default: undefined
    },
    loadedKeys: {
      type: Array
    },
    draggable: {
      type: Boolean,
      default: undefined
    },
    showIcon: {
      type: Boolean,
      default: undefined
    },
    icon: {
      type: Function
    },
    switcherIcon: PropTypes.any,
    prefixCls: PropTypes.string,

    /**
     * @default{title,key,children}
     * deprecated, please use `fieldNames` instead
     * 替换treeNode中 title,key,children字段为treeData中对应的字段
     */
    replaceFields: {
      type: Object
    },
    blockNode: {
      type: Boolean,
      default: undefined
    },
    openAnimation: PropTypes.any
  });
};
export default defineComponent({
  name: 'ATree',
  inheritAttrs: false,
  props: initDefaultProps(treeProps(), {
    checkable: false,
    selectable: true,
    showIcon: false,
    blockNode: false
  }),
  slots: ['icon', 'title', 'switcherIcon', 'titleRender'],
  emits: ['update:selectedKeys', 'update:checkedKeys', 'update:expandedKeys', 'expand', 'select', 'check', 'doubleclick', 'dblclick'],
  TreeNode: TreeNode,
  setup: function setup(props, _ref) {
    var attrs = _ref.attrs,
        expose = _ref.expose,
        emit = _ref.emit,
        slots = _ref.slots;
    warning(!(props.treeData === undefined && slots.default), '`children` of Tree is deprecated. Please use `treeData` instead.');

    var _useConfigInject = useConfigInject('tree', props),
        prefixCls = _useConfigInject.prefixCls,
        direction = _useConfigInject.direction,
        virtual = _useConfigInject.virtual;

    var treeRef = ref();
    expose({
      treeRef: treeRef,
      onNodeExpand: function onNodeExpand() {
        var _treeRef$value;

        for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
          args[_key] = arguments[_key];
        }

        (_treeRef$value = treeRef.value) === null || _treeRef$value === void 0 ? void 0 : _treeRef$value.onNodeExpand.apply(_treeRef$value, args);
      }
    });
    watchEffect(function () {
      devWarning(props.replaceFields === undefined, 'Tree', '`replaceFields` is deprecated, please use fieldNames instead');
    });

    var handleCheck = function handleCheck(checkedObjOrKeys, eventObj) {
      emit('update:checkedKeys', checkedObjOrKeys);
      emit('check', checkedObjOrKeys, eventObj);
    };

    var handleExpand = function handleExpand(expandedKeys, eventObj) {
      emit('update:expandedKeys', expandedKeys);
      emit('expand', expandedKeys, eventObj);
    };

    var handleSelect = function handleSelect(selectedKeys, eventObj) {
      emit('update:selectedKeys', selectedKeys);
      emit('select', selectedKeys, eventObj);
    };

    return function () {
      var _classNames, _slots$default;

      var _ref2 = props,
          showIcon = _ref2.showIcon,
          showLine = _ref2.showLine,
          _ref2$switcherIcon = _ref2.switcherIcon,
          _switcherIcon = _ref2$switcherIcon === void 0 ? slots.switcherIcon : _ref2$switcherIcon,
          _ref2$icon = _ref2.icon,
          icon = _ref2$icon === void 0 ? slots.icon : _ref2$icon,
          blockNode = _ref2.blockNode,
          checkable = _ref2.checkable,
          selectable = _ref2.selectable,
          _ref2$fieldNames = _ref2.fieldNames,
          fieldNames = _ref2$fieldNames === void 0 ? props.replaceFields : _ref2$fieldNames,
          _ref2$motion = _ref2.motion,
          motion = _ref2$motion === void 0 ? props.openAnimation : _ref2$motion,
          _ref2$itemHeight = _ref2.itemHeight,
          itemHeight = _ref2$itemHeight === void 0 ? 28 : _ref2$itemHeight;

      var newProps = _objectSpread(_objectSpread(_objectSpread({}, attrs), props), {}, {
        showLine: Boolean(showLine),
        dropIndicatorRender: dropIndicatorRender,
        fieldNames: fieldNames,
        icon: icon,
        itemHeight: itemHeight
      });

      return _createVNode(VcTree, _objectSpread(_objectSpread({}, newProps), {}, {
        "virtual": virtual.value,
        "motion": motion,
        "ref": treeRef,
        "prefixCls": prefixCls.value,
        "class": classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls.value, "-icon-hide"), !showIcon), _defineProperty(_classNames, "".concat(prefixCls.value, "-block-node"), blockNode), _defineProperty(_classNames, "".concat(prefixCls.value, "-unselectable"), !selectable), _defineProperty(_classNames, "".concat(prefixCls.value, "-rtl"), direction.value === 'rtl'), _classNames), attrs.class),
        "direction": direction.value,
        "checkable": checkable,
        "selectable": selectable,
        "switcherIcon": function switcherIcon(nodeProps) {
          return renderSwitcherIcon(prefixCls.value, _switcherIcon, showLine, nodeProps);
        },
        "onCheck": handleCheck,
        "onExpand": handleExpand,
        "onSelect": handleSelect,
        "children": filterEmpty((_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots))
      }), _objectSpread(_objectSpread({}, slots), {}, {
        checkable: function checkable() {
          return _createVNode("span", {
            "class": "".concat(prefixCls.value, "-checkbox-inner")
          }, null);
        }
      }));
    };
  }
});