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 { computed, ref, watchEffect, defineComponent } from 'vue';
import VcTreeSelect, { TreeNode, SHOW_ALL, SHOW_PARENT, SHOW_CHILD, treeSelectProps as vcTreeSelectProps } from '../vc-tree-select';
import classNames from '../_util/classNames';
import initDefaultProps from '../_util/props-util/initDefaultProps';
import omit from '../_util/omit';
import PropTypes from '../_util/vue-types';
import useConfigInject from '../_util/hooks/useConfigInject';
import devWarning from '../vc-util/devWarning';
import getIcons from '../select/utils/iconUtil';
import renderSwitcherIcon from '../tree/utils/iconUtil';
import { warning } from '../vc-util/warning';
import { flattenChildren } from '../_util/props-util';
import { useInjectFormItemContext } from '../form/FormItemContext';

var getTransitionName = function getTransitionName(rootPrefixCls, motion, transitionName) {
  if (transitionName !== undefined) {
    return transitionName;
  }

  return "".concat(rootPrefixCls, "-").concat(motion);
};

export var treeSelectProps = _objectSpread(_objectSpread({}, omit(vcTreeSelectProps(), ['showTreeIcon', 'treeMotion', 'inputIcon'])), {}, {
  suffixIcon: PropTypes.any,
  size: {
    type: String
  },
  bordered: {
    type: Boolean,
    default: undefined
  },
  replaceFields: {
    type: Object
  }
});
var TreeSelect = defineComponent({
  TreeNode: TreeNode,
  SHOW_ALL: SHOW_ALL,
  SHOW_PARENT: SHOW_PARENT,
  SHOW_CHILD: SHOW_CHILD,
  name: 'ATreeSelect',
  inheritAttrs: false,
  props: initDefaultProps(treeSelectProps, {
    transitionName: 'slide-up',
    choiceTransitionName: '',
    listHeight: 256,
    treeIcon: false,
    listItemHeight: 26,
    bordered: true
  }),
  slots: ['title', 'titleRender', 'placeholder', 'maxTagPlaceholder', 'treeIcon', 'switcherIcon', 'notFoundContent'],
  setup: function setup(props, _ref) {
    var attrs = _ref.attrs,
        slots = _ref.slots,
        expose = _ref.expose,
        emit = _ref.emit;
    warning(!(props.treeData === undefined && slots.default), '`children` of TreeSelect is deprecated. Please use `treeData` instead.');
    watchEffect(function () {
      devWarning(props.multiple !== false || !props.treeCheckable, 'TreeSelect', '`multiple` will alway be `true` when `treeCheckable` is true');
      devWarning(props.replaceFields === undefined, 'TreeSelect', '`replaceFields` is deprecated, please use fieldNames instead');
    });
    var formItemContext = useInjectFormItemContext();

    var _useConfigInject = useConfigInject('select', props),
        configProvider = _useConfigInject.configProvider,
        prefixCls = _useConfigInject.prefixCls,
        renderEmpty = _useConfigInject.renderEmpty,
        direction = _useConfigInject.direction,
        virtual = _useConfigInject.virtual,
        dropdownMatchSelectWidth = _useConfigInject.dropdownMatchSelectWidth,
        size = _useConfigInject.size,
        getPopupContainer = _useConfigInject.getPopupContainer;

    var treePrefixCls = computed(function () {
      return configProvider.getPrefixCls('select-tree', props.prefixCls);
    });
    var treeSelectPrefixCls = computed(function () {
      return configProvider.getPrefixCls('tree-select', props.prefixCls);
    });
    var mergedDropdownClassName = computed(function () {
      return classNames(props.dropdownClassName, "".concat(treeSelectPrefixCls.value, "-dropdown"), _defineProperty({}, "".concat(treeSelectPrefixCls.value, "-dropdown-rtl"), direction.value === 'rtl'));
    });
    var isMultiple = computed(function () {
      return !!(props.treeCheckable || props.multiple);
    });
    var treeSelectRef = ref();
    expose({
      focus: function focus() {
        var _treeSelectRef$value$, _treeSelectRef$value;

        (_treeSelectRef$value$ = (_treeSelectRef$value = treeSelectRef.value).focus) === null || _treeSelectRef$value$ === void 0 ? void 0 : _treeSelectRef$value$.call(_treeSelectRef$value);
      },
      blur: function blur() {
        var _treeSelectRef$value$2, _treeSelectRef$value2;

        (_treeSelectRef$value$2 = (_treeSelectRef$value2 = treeSelectRef.value).blur) === null || _treeSelectRef$value$2 === void 0 ? void 0 : _treeSelectRef$value$2.call(_treeSelectRef$value2);
      }
    });

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

      emit('update:value', args[0]);
      emit.apply(void 0, ['change'].concat(args));
      formItemContext.onFieldChange();
    };

    var handleTreeExpand = function handleTreeExpand() {
      for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
        args[_key2] = arguments[_key2];
      }

      emit('update:treeExpandedKeys', args[0]);
      emit.apply(void 0, ['treeExpand'].concat(args));
    };

    var handleSearch = function handleSearch() {
      for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
        args[_key3] = arguments[_key3];
      }

      emit('update:searchValue', args[0]);
      emit.apply(void 0, ['search'].concat(args));
    };

    var handleBlur = function handleBlur() {
      emit('blur');
      formItemContext.onFieldBlur();
    };

    return function () {
      var _slots$notFoundConten, _slots$switcherIcon, _classNames2;

      var _props$notFoundConten = props.notFoundContent,
          notFoundContent = _props$notFoundConten === void 0 ? (_slots$notFoundConten = slots.notFoundContent) === null || _slots$notFoundConten === void 0 ? void 0 : _slots$notFoundConten.call(slots) : _props$notFoundConten,
          customizePrefixCls = props.prefixCls,
          bordered = props.bordered,
          listHeight = props.listHeight,
          listItemHeight = props.listItemHeight,
          multiple = props.multiple,
          treeIcon = props.treeIcon,
          transitionName = props.transitionName,
          choiceTransitionName = props.choiceTransitionName,
          treeLine = props.treeLine,
          _props$switcherIcon = props.switcherIcon,
          _switcherIcon = _props$switcherIcon === void 0 ? (_slots$switcherIcon = slots.switcherIcon) === null || _slots$switcherIcon === void 0 ? void 0 : _slots$switcherIcon.call(slots) : _props$switcherIcon,
          _props$fieldNames = props.fieldNames,
          fieldNames = _props$fieldNames === void 0 ? props.replaceFields : _props$fieldNames,
          _props$id = props.id,
          id = _props$id === void 0 ? formItemContext.id.value : _props$id; // ===================== Icons =====================


      var _getIcons = getIcons(_objectSpread(_objectSpread({}, props), {}, {
        multiple: isMultiple.value,
        prefixCls: prefixCls.value
      }), slots),
          suffixIcon = _getIcons.suffixIcon,
          removeIcon = _getIcons.removeIcon,
          clearIcon = _getIcons.clearIcon; // ===================== Empty =====================


      var mergedNotFound;

      if (notFoundContent !== undefined) {
        mergedNotFound = notFoundContent;
      } else {
        mergedNotFound = renderEmpty.value('Select');
      } // ==================== Render =====================


      var selectProps = omit(props, ['suffixIcon', 'itemIcon', 'removeIcon', 'clearIcon', 'switcherIcon']);
      var mergedClassName = classNames(!customizePrefixCls && treeSelectPrefixCls.value, (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls.value, "-lg"), size.value === 'large'), _defineProperty(_classNames2, "".concat(prefixCls.value, "-sm"), size.value === 'small'), _defineProperty(_classNames2, "".concat(prefixCls.value, "-rtl"), direction.value === 'rtl'), _defineProperty(_classNames2, "".concat(prefixCls.value, "-borderless"), !bordered), _classNames2), attrs.class);
      var rootPrefixCls = configProvider.getPrefixCls();
      var otherProps = {};

      if (props.treeData === undefined && slots.default) {
        otherProps.children = flattenChildren(slots.default());
      }

      return _createVNode(VcTreeSelect, _objectSpread(_objectSpread(_objectSpread({}, attrs), {}, {
        "virtual": virtual.value,
        "dropdownMatchSelectWidth": dropdownMatchSelectWidth.value
      }, selectProps), {}, {
        "id": id,
        "fieldNames": fieldNames,
        "ref": treeSelectRef,
        "prefixCls": prefixCls.value,
        "class": mergedClassName,
        "listHeight": listHeight,
        "listItemHeight": listItemHeight,
        "inputIcon": suffixIcon,
        "multiple": multiple,
        "removeIcon": removeIcon,
        "clearIcon": clearIcon,
        "switcherIcon": function switcherIcon(nodeProps) {
          return renderSwitcherIcon(treePrefixCls.value, _switcherIcon, treeLine, nodeProps);
        },
        "showTreeIcon": treeIcon,
        "notFoundContent": mergedNotFound,
        "getPopupContainer": getPopupContainer.value,
        "treeMotion": null,
        "dropdownClassName": mergedDropdownClassName.value,
        "choiceTransitionName": getTransitionName(rootPrefixCls, '', choiceTransitionName),
        "transitionName": getTransitionName(rootPrefixCls, 'slide-up', transitionName),
        "onChange": handleChange,
        "onBlur": handleBlur,
        "onSearch": handleSearch,
        "onTreeExpand": handleTreeExpand
      }, otherProps), _objectSpread(_objectSpread({}, slots), {}, {
        treeCheckable: function treeCheckable() {
          return _createVNode("span", {
            "class": "".concat(prefixCls.value, "-tree-checkbox-inner")
          }, null);
        }
      }));
    };
  }
});
/* istanbul ignore next */

TreeSelect.install = function (app) {
  app.component(TreeSelect.name, TreeSelect);
  app.component(TreeSelect.TreeNode.displayName, TreeSelect.TreeNode);
  return app;
};

export var TreeSelectNode = TreeSelect.TreeNode;
export default TreeSelect;