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

import { h as helper } from '../_chunks/dep-42d5c87e.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import isArray from 'lodash/isArray';
import isEmpty from 'lodash/isEmpty';
import isNumber from 'lodash/isNumber';
import isString from 'lodash/isString';
import isBoolean from 'lodash/isBoolean';
import isObject from 'lodash/isObject';
import isFunction from 'lodash/isFunction';
import isNil from 'lodash/isNil';
import { CloseCircleFilledIcon } from 'tdesign-icons-vue';
import { Loading } from '../loading/index.js';
import mixins from '../utils/mixins.js';
import getConfigReceiverMixins from '../config-provider/config-receiver.js';
import { renderTNodeJSX } from '../utils/render-tnode.js';
import { emitEvent } from '../utils/event.js';
import { Popup } from '../popup/index.js';
import { Tag } from '../tag/index.js';
import { Tree } from '../tree/index.js';
import { Input } from '../input/index.js';
import FakeArrow from '../common-components/fake-arrow.js';
import ClASSNAMES from '../utils/classnames.js';
import props from './props.js';
import { prefix } from '../config.js';
import '../loading/loading.js';
import 'vue';
import '../loading/icon/gradient.js';
import '../utils/set-style.js';
import '../utils/dom.js';
import 'raf';
import '../utils/easing.js';
import '../utils/transfer-dom.js';
import '../loading/props.js';
import '@babel/runtime/helpers/typeof';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/css.js';
import '../loading/type.js';
import '../loading/plugin.js';
import '../config-provider/zh_CN_config.js';
import '../config-provider/type.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/camelCase';
import '../popup/popup.js';
import '@popperjs/core';
import '../_chunks/dep-1db5c91c.js';
import '../popup/props.js';
import '../utils/map-props.js';
import '../popup/type.js';
import '../tag/tag.js';
import '../tag/props.js';
import '../tag/check-tag.js';
import '../tag/check-tag-props.js';
import '../tag/type.js';
import '../tree/td-tree.js';
import 'lodash/upperFirst';
import 'lodash/pick';
import '../_common/js/tree/tree-store.js';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import 'lodash/difference';
import 'lodash/isPlainObject';
import '../_common/js/tree/tree-node.js';
import '@babel/runtime/helpers/toConsumableArray';
import 'lodash/uniqueId';
import 'lodash/get';
import '../_common/js/tree/tree-node-model.js';
import '../tree/tree-item.js';
import '../checkbox/index.js';
import '../checkbox/group.js';
import 'lodash/intersection';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../checkbox/checkbox-group-props.js';
import '../checkbox/type.js';
import '../tree/util.js';
import '../tree/constants.js';
import '../utils/ripple.js';
import '../tree/props.js';
import '../tree/interface.js';
import '../tree/type.js';
import '../input/addon.js';
import '../input/input.js';
import '../input/props.js';
import '../input/input-group.js';
import '../input/type.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; }
var _TreeSelect = mixins(getConfigReceiverMixins("treeSelect")).extend({
  name: "TTreeSelect",
  model: {
    prop: "value",
    event: "change"
  },
  props: props,
  data: function data() {
    return {
      visible: false,
      isHover: false,
      focusing: false,
      defaultProps: {
        trigger: "click",
        placement: "bottom-left",
        overlayClassName: "",
        overlayStyle: function overlayStyle(trigger) {
          return {
            width: "".concat(trigger.offsetWidth, "px")
          };
        }
      },
      filterText: "",
      filterByText: null,
      actived: [],
      expanded: [],
      nodeInfo: null
    };
  },
  watch: {
    value: function value() {
      var _this = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return _this.changeNodeInfo();

              case 2:
                if (!_this.multiple) {
                  _this.actived = _this.nodeInfo ? [_this.nodeInfo.value] : [];
                }

              case 3:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))();
    }
  },
  computed: {
    classes: function classes() {
      var _ref;

      return ["".concat(prefix, "-select"), (_ref = {}, _defineProperty(_ref, ClASSNAMES.STATUS.disabled, this.disabled), _defineProperty(_ref, ClASSNAMES.STATUS.active, this.visible), _defineProperty(_ref, ClASSNAMES.SIZE[this.size], this.size), _defineProperty(_ref, "".concat(prefix, "-has-prefix"), this.prefixIconSlot), _defineProperty(_ref, "".concat(prefix, "-select-selected"), this.selectedSingle || !isEmpty(this.selectedMultiple)), _ref)];
    },
    popupClass: function popupClass() {
      var popupObject = this.popupObject;
      return "".concat(popupObject.overlayClassName, " ").concat(prefix, "-select__dropdown narrow-scrollbar");
    },
    isObjectValue: function isObjectValue() {
      return this.valueType === "object";
    },
    checked: function checked() {
      if (this.multiple) {
        if (this.isObjectValue) {
          return isArray(this.value) ? this.value.map(function (item) {
            return item.value;
          }) : [];
        }

        return isArray(this.value) ? this.value : [];
      }

      return [];
    },
    showArrow: function showArrow() {
      return !this.clearable || !this.isHover || this.disabled || !this.multiple && !this.value && this.value !== 0 || this.multiple && isArray(this.value) && isEmpty(this.value);
    },
    showLoading: function showLoading() {
      return this.loading && !this.disabled;
    },
    showClose: function showClose() {
      return this.clearable && this.isHover && !this.disabled && (!this.multiple && (!!this.value || this.value === 0) || this.multiple && !isEmpty(this.value));
    },
    showPlaceholder: function showPlaceholder() {
      if (!this.showFilter && (isString(this.value) && this.value === "" && !this.selectedSingle || isArray(this.value) && isEmpty(this.value) || isNil(this.value))) {
        return true;
      }

      return false;
    },
    showFilter: function showFilter() {
      if (this.disabled) {
        return false;
      }

      if (!this.multiple && this.selectedSingle && (this.filterable || isFunction(this.filter))) {
        return this.visible;
      }

      return this.filterable || isFunction(this.filter);
    },
    showTree: function showTree() {
      return !this.loading;
    },
    popupObject: function popupObject() {
      var propsObject = this.popupProps ? _objectSpread(_objectSpread({}, this.defaultProps), this.popupProps) : this.defaultProps;
      return propsObject;
    },
    selectedSingle: function selectedSingle() {
      if (!this.multiple && (isString(this.value) || isNumber(this.value) || isObject(this.value))) {
        if (this.nodeInfo) {
          return this.nodeInfo.label;
        }

        return "".concat(this.value);
      }

      return "";
    },
    selectedMultiple: function selectedMultiple() {
      if (this.multiple && isArray(this.value) && !isEmpty(this.value)) {
        return this.value;
      }

      return [];
    },
    multiLimitDisabled: function multiLimitDisabled() {
      if (this.multiple && this.max && isArray(this.value) && this.max <= this.value.length) {
        return true;
      }

      return false;
    },
    filterPlaceholder: function filterPlaceholder() {
      if (this.multiple && isArray(this.value) && !isEmpty(this.value)) {
        return "";
      }

      if (!this.multiple && this.selectedSingle) {
        return this.selectedSingle;
      }

      return this.placeholder;
    },
    loadingTextSlot: function loadingTextSlot() {
      var h = this.$createElement;
      var useLocale = !this.loadingText && !this.$scopedSlots.loadingText;
      return useLocale ? h("div", {
        "class": "".concat(prefix, "-select__empty")
      }, [this.t(this.global.loadingText)]) : renderTNodeJSX(this, "loadingText");
    },
    emptySlot: function emptySlot() {
      var h = this.$createElement;
      var useLocale = !this.empty && !this.$scopedSlots.empty;
      return useLocale ? h("div", {
        "class": "".concat(prefix, "-select__empty")
      }, [this.t(this.global.empty)]) : renderTNodeJSX(this, "empty");
    },
    prefixIconSlot: function prefixIconSlot() {
      return renderTNodeJSX(this, "prefixIcon");
    },
    realLabel: function realLabel() {
      var treeProps = this.treeProps;

      if (!isEmpty(treeProps) && !isEmpty(treeProps.keys)) {
        return treeProps.keys.label || "label";
      }

      return "label";
    },
    realValue: function realValue() {
      var treeProps = this.treeProps;

      if (!isEmpty(treeProps) && !isEmpty(treeProps.keys)) {
        return treeProps.keys.value || "value";
      }

      return "value";
    },
    tagList: function tagList() {
      if (this.nodeInfo && isArray(this.nodeInfo)) {
        return this.nodeInfo.map(function (node) {
          return node.label;
        });
      }

      return this.selectedMultiple;
    }
  },
  mounted: function mounted() {
    var _this2 = this;

    return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              if (!(!_this2.value && _this2.defaultValue)) {
                _context2.next = 3;
                break;
              }

              _context2.next = 3;
              return _this2.change(_this2.defaultValue, null);

            case 3:
              if (_this2.isObjectValue) {
                _this2.actived = isArray(_this2.value) ? _this2.value.map(function (item) {
                  return item.value;
                }) : [_this2.value.value];
              } else {
                _this2.actived = isArray(_this2.value) ? _this2.value : [_this2.value];
              }

              _this2.changeNodeInfo();

            case 5:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }))();
  },
  methods: {
    popupVisibleChange: function popupVisibleChange(visible) {
      var _this3 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
        var searchInput;
        return _regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                if (!(_this3.focusing && !visible)) {
                  _context3.next = 3;
                  break;
                }

                _this3.visible = true;
                return _context3.abrupt("return");

              case 3:
                _context3.next = 5;
                return _this3.visible = visible;

              case 5:
                if (_this3.showFilter && _this3.visible) {
                  searchInput = _this3.$refs.input;
                  searchInput === null || searchInput === void 0 ? void 0 : searchInput.focus();
                  _this3.focusing = true;
                }

              case 6:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3);
      }))();
    },
    removeTag: function removeTag(index, data, e) {
      if (this.disabled) {
        return;
      }

      this.remove({
        value: this.value[index],
        data: data,
        e: e
      });
      isArray(this.value) && this.value.splice(index, 1);
      this.change(this.value, null);
    },
    change: function change(value, node) {
      emitEvent(this, "change", value, {
        node: node
      });
      this.changeNodeInfo();
    },
    clear: function clear(e) {
      var defaultValue = this.multiple ? [] : "";
      this.change(defaultValue, null);
      this.actived = [];
      this.filterText = "";
      emitEvent(this, "clear", {
        e: e
      });
    },
    focus: function focus(ctx) {
      this.focusing = true;
      emitEvent(this, "focus", _objectSpread({
        value: this.value
      }, ctx));
    },
    blur: function blur(ctx) {
      this.focusing = false;
      this.filterText = "";
      emitEvent(this, "blur", _objectSpread({
        value: this.value
      }, ctx));
    },
    remove: function remove(options) {
      emitEvent(this, "remove", options);
    },
    search: function search(filterWords) {
      emitEvent(this, "search", filterWords);
    },
    treeNodeChange: function treeNodeChange(value, context) {
      var _this4 = this;

      var current = value;

      if (this.isObjectValue) {
        var tree = this.$refs.tree;
        current = value.map(function (nodeValue) {
          var node = tree.getItem(nodeValue);
          return {
            label: node.data[_this4.realLabel],
            value: node.data[_this4.realValue]
          };
        });
      }

      this.change(current, context.node);
      this.actived = value;
    },
    treeNodeActive: function treeNodeActive(value, context) {
      if (this.multiple) {
        return;
      }

      var current = value;

      if (this.isObjectValue) {
        var tree = this.$refs.tree;
        var nodeValue = isEmpty(value) ? "" : value[0];
        var node = tree.getItem(nodeValue);
        current = {
          label: node.data[this.realLabel],
          value: node.data[this.realValue]
        };
      } else {
        current = isEmpty(value) ? "" : value[0];
      }

      this.change(current, context.node);
      this.actived = value;
      this.visible = false;
    },
    treeNodeExpand: function treeNodeExpand(value) {
      this.expanded = value;
    },
    onInputChange: function onInputChange() {
      var _this5 = this;

      this.filterByText = function (node) {
        if (isFunction(_this5.filter)) {
          var filter = _this5.filter(_this5.filterText, node);

          if (isBoolean(filter)) {
            return filter;
          }
        }

        return node.data[_this5.realLabel].indexOf(_this5.filterText) >= 0;
      };

      this.search(this.filterText);
    },
    changeNodeInfo: function changeNodeInfo() {
      var _this6 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
        var tree, nodeValue, node;
        return _regeneratorRuntime.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                tree = _this6.$refs.tree;
                _context4.next = 3;
                return _this6.value;

              case 3:
                if (tree && !_this6.multiple && _this6.value) {
                  nodeValue = _this6.isObjectValue ? _this6.value.value : _this6.value;

                  if (!isEmpty(_this6.data)) {
                    node = tree.getItem(nodeValue);
                    _this6.nodeInfo = {
                      label: node.data[_this6.realLabel],
                      value: node.data[_this6.realValue]
                    };
                  } else {
                    _this6.nodeInfo = {
                      label: nodeValue,
                      value: nodeValue
                    };
                  }
                } else if (tree && _this6.multiple && isArray(_this6.value)) {
                  _this6.nodeInfo = _this6.value.map(function (value) {
                    var nodeValue = _this6.isObjectValue ? value.value : value;

                    if (!isEmpty(_this6.data)) {
                      var _node = tree.getItem(nodeValue);

                      return {
                        label: _node.data[_this6.realLabel],
                        value: _node.data[_this6.realValue]
                      };
                    }

                    return {
                      label: nodeValue,
                      value: nodeValue
                    };
                  });
                } else {
                  _this6.nodeInfo = null;
                }

              case 4:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4);
      }))();
    }
  },
  render: function render() {
    var _this7 = this;

    var h = arguments[0];
    var treeProps = this.treeProps,
        popupObject = this.popupObject,
        classes = this.classes,
        popupClass = this.popupClass;
    var iconStyle = {
      "font-size": this.size
    };
    var treeItem = h(Tree, {
      "ref": "tree",
      "directives": [{
        name: "show",
        value: this.showTree
      }],
      "attrs": {
        "value": this.checked,
        "hover": true,
        "data": this.data,
        "activable": !this.multiple,
        "checkable": this.multiple,
        "disabled": this.disabled || this.multiLimitDisabled,
        "empty": this.empty,
        "size": this.size,
        "filter": this.filterByText,
        "actived": this.actived,
        "expanded": this.expanded,
        "activeMultiple": this.multiple
      },
      "on": {
        "change": this.treeNodeChange,
        "active": this.treeNodeActive,
        "expand": this.treeNodeExpand
      },
      "props": _objectSpread({}, treeProps)
    }, [h("template", {
      "slot": "empty"
    }, [this.emptySlot])]);
    var searchInput = h(Input, {
      "ref": "input",
      "directives": [{
        name: "show",
        value: this.showFilter
      }],
      "class": "".concat(prefix, "-select__input"),
      "attrs": {
        "size": this.size,
        "disabled": this.disabled,
        "placeholder": this.filterPlaceholder
      },
      "on": {
        "change": this.onInputChange,
        "blur": function blur(value, context) {
          return _this7.blur(context);
        },
        "focus": function focus(value, context) {
          return _this7.focus(context);
        }
      },
      "model": {
        value: _this7.filterText,
        callback: function callback($$v) {
          _this7.filterText = $$v;
        }
      }
    });
    var tagItem = this.tagList.map(function (label, index) {
      return h(Tag, {
        "directives": [{
          name: "show",
          value: _this7.minCollapsedNum <= 0 || index < _this7.minCollapsedNum
        }],
        "key": index,
        "attrs": {
          "size": _this7.size,
          "closable": !_this7.disabled,
          "disabled": _this7.disabled
        },
        "on": {
          "close": function close(e) {
            return _this7.removeTag(index, null, e);
          }
        }
      }, [label]);
    });
    var collapsedItem = (this.collapsedItems || this.$scopedSlots.collapsedItems) && this.minCollapsedNum > 0 && this.tagList.length > this.minCollapsedNum ? renderTNodeJSX(this, "collapsedItems", {
      params: {
        count: this.tagList.length - this.minCollapsedNum,
        value: this.selectedMultiple,
        collapsedSelectedItems: this.selectedMultiple.slice(this.minCollapsedNum)
      }
    }) : h(Tag, {
      "directives": [{
        name: "show",
        value: this.minCollapsedNum > 0 && this.tagList.length > this.minCollapsedNum
      }],
      "attrs": {
        "size": this.size
      }
    }, ["+".concat(this.tagList.length - this.minCollapsedNum)]);
    return h("div", {
      "ref": "treeSelect"
    }, [h(Popup, helper([{
      "ref": "popup",
      "class": "".concat(prefix, "-select__popup-reference"),
      "attrs": {
        "visible": this.visible,
        "disabled": this.disabled,
        "placement": popupObject.placement,
        "trigger": popupObject.trigger,
        "overlayStyle": popupObject.overlayStyle,
        "overlayClassName": popupClass
      }
    }, {
      "on": {
        "visible-change": this.popupVisibleChange
      }
    }, {
      "attrs": {
        "expandAnimation": true
      }
    }]), [h("div", {
      "class": classes,
      "on": {
        "mouseenter": function mouseenter() {
          return _this7.isHover = true;
        },
        "mouseleave": function mouseleave() {
          return _this7.isHover = false;
        }
      }
    }, [this.prefixIconSlot && h("span", {
      "class": "".concat(prefix, "-select__left-icon")
    }, [this.prefixIconSlot[0]]), h("span", {
      "directives": [{
        name: "show",
        value: this.showPlaceholder
      }],
      "class": "".concat(prefix, "-select__placeholder")
    }, [this.placeholder]), tagItem, collapsedItem, !this.multiple && !this.showPlaceholder && !this.showFilter && h("span", {
      "attrs": {
        "title": this.selectedSingle
      },
      "class": "".concat(prefix, "-select__single")
    }, [this.selectedSingle]), searchInput, this.showArrow && !this.showLoading && h(FakeArrow, {
      "attrs": {
        "overlayClassName": "".concat(prefix, "-select__right-icon"),
        "overlayStyle": iconStyle,
        "isActive": this.visible && !this.disabled
      }
    }), h(CloseCircleFilledIcon, {
      "directives": [{
        name: "show",
        value: this.showClose && !this.showLoading
      }],
      "class": ["".concat(prefix, "-select__right-icon"), "".concat(prefix, "-select__right-icon-clear")],
      "attrs": {
        "size": this.size
      },
      "nativeOn": {
        "click": this.clear
      }
    }), h(Loading, {
      "directives": [{
        name: "show",
        value: this.showLoading
      }],
      "class": "".concat(prefix, "-select__right-icon ").concat(prefix, "-select__active-icon"),
      "attrs": {
        "size": "small"
      }
    })]), h("div", {
      "slot": "content"
    }, [h("p", {
      "directives": [{
        name: "show",
        value: this.showLoading
      }],
      "class": "".concat(prefix, "-select__loading-tips")
    }, [this.loadingTextSlot]), treeItem])])]);
  }
});

export { _TreeSelect as default };
//# sourceMappingURL=tree-select.js.map
