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

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var helper = require('../_chunks/dep-44e6bdf6.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var isArray = require('lodash/isArray');
var isEmpty = require('lodash/isEmpty');
var isNumber = require('lodash/isNumber');
var isString = require('lodash/isString');
var isBoolean = require('lodash/isBoolean');
var isObject = require('lodash/isObject');
var isFunction = require('lodash/isFunction');
var isNil = require('lodash/isNil');
var tdesignIconsVue = require('tdesign-icons-vue');
var loading_index = require('../loading/index.js');
var utils_mixins = require('../utils/mixins.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var utils_event = require('../utils/event.js');
var popup_index = require('../popup/index.js');
var tag_index = require('../tag/index.js');
var tree_index = require('../tree/index.js');
var input_index = require('../input/index.js');
var commonComponents_fakeArrow = require('../common-components/fake-arrow.js');
var utils_classnames = require('../utils/classnames.js');
var treeSelect_props = require('./props.js');
var config = require('../config.js');
require('../loading/loading.js');
require('vue');
require('../loading/icon/gradient.js');
require('../utils/set-style.js');
require('../utils/dom.js');
require('raf');
require('../utils/easing.js');
require('../utils/transfer-dom.js');
require('../loading/props.js');
require('@babel/runtime/helpers/typeof');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../loading/plugin.js');
require('../config-provider/zh_CN_config.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');
require('../popup/popup.js');
require('@popperjs/core');
require('../_chunks/dep-939cdc39.js');
require('../popup/props.js');
require('../utils/map-props.js');
require('../tag/tag.js');
require('../tag/props.js');
require('../tag/check-tag.js');
require('../tag/check-tag-props.js');
require('../tree/td-tree.js');
require('lodash/upperFirst');
require('lodash/pick');
require('../_common/js/tree/tree-store.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('lodash/difference');
require('lodash/isPlainObject');
require('../_common/js/tree/tree-node.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/uniqueId');
require('lodash/get');
require('../_common/js/tree/tree-node-model.js');
require('../tree/tree-item.js');
require('../checkbox/index.js');
require('../checkbox/group.js');
require('lodash/intersection');
require('../checkbox/checkbox.js');
require('../checkbox/props.js');
require('../checkbox/checkbox-group-props.js');
require('../tree/util.js');
require('../tree/constants.js');
require('../utils/ripple.js');
require('../tree/props.js');
require('../input/addon.js');
require('../input/input.js');
require('../input/props.js');
require('../input/input-group.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
var isEmpty__default = /*#__PURE__*/_interopDefaultLegacy(isEmpty);
var isNumber__default = /*#__PURE__*/_interopDefaultLegacy(isNumber);
var isString__default = /*#__PURE__*/_interopDefaultLegacy(isString);
var isBoolean__default = /*#__PURE__*/_interopDefaultLegacy(isBoolean);
var isObject__default = /*#__PURE__*/_interopDefaultLegacy(isObject);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var isNil__default = /*#__PURE__*/_interopDefaultLegacy(isNil);

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__default["default"](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 = utils_mixins["default"](configProvider_configReceiver["default"]("treeSelect")).extend({
  name: "TTreeSelect",
  model: {
    prop: "value",
    event: "change"
  },
  props: treeSelect_props["default"],
  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__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
        return _regeneratorRuntime__default["default"].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(config.prefix, "-select"), (_ref = {}, _defineProperty__default["default"](_ref, utils_classnames["default"].STATUS.disabled, this.disabled), _defineProperty__default["default"](_ref, utils_classnames["default"].STATUS.active, this.visible), _defineProperty__default["default"](_ref, utils_classnames["default"].SIZE[this.size], this.size), _defineProperty__default["default"](_ref, "".concat(config.prefix, "-has-prefix"), this.prefixIconSlot), _defineProperty__default["default"](_ref, "".concat(config.prefix, "-select-selected"), this.selectedSingle || !isEmpty__default["default"](this.selectedMultiple)), _ref)];
    },
    popupClass: function popupClass() {
      var popupObject = this.popupObject;
      return "".concat(popupObject.overlayClassName, " ").concat(config.prefix, "-select__dropdown narrow-scrollbar");
    },
    isObjectValue: function isObjectValue() {
      return this.valueType === "object";
    },
    checked: function checked() {
      if (this.multiple) {
        if (this.isObjectValue) {
          return isArray__default["default"](this.value) ? this.value.map(function (item) {
            return item.value;
          }) : [];
        }

        return isArray__default["default"](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__default["default"](this.value) && isEmpty__default["default"](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__default["default"](this.value));
    },
    showPlaceholder: function showPlaceholder() {
      if (!this.showFilter && (isString__default["default"](this.value) && this.value === "" && !this.selectedSingle || isArray__default["default"](this.value) && isEmpty__default["default"](this.value) || isNil__default["default"](this.value))) {
        return true;
      }

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

      if (!this.multiple && this.selectedSingle && (this.filterable || isFunction__default["default"](this.filter))) {
        return this.visible;
      }

      return this.filterable || isFunction__default["default"](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__default["default"](this.value) || isNumber__default["default"](this.value) || isObject__default["default"](this.value))) {
        if (this.nodeInfo) {
          return this.nodeInfo.label;
        }

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

      return "";
    },
    selectedMultiple: function selectedMultiple() {
      if (this.multiple && isArray__default["default"](this.value) && !isEmpty__default["default"](this.value)) {
        return this.value;
      }

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

      return false;
    },
    filterPlaceholder: function filterPlaceholder() {
      if (this.multiple && isArray__default["default"](this.value) && !isEmpty__default["default"](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(config.prefix, "-select__empty")
      }, [this.t(this.global.loadingText)]) : utils_renderTnode.renderTNodeJSX(this, "loadingText");
    },
    emptySlot: function emptySlot() {
      var h = this.$createElement;
      var useLocale = !this.empty && !this.$scopedSlots.empty;
      return useLocale ? h("div", {
        "class": "".concat(config.prefix, "-select__empty")
      }, [this.t(this.global.empty)]) : utils_renderTnode.renderTNodeJSX(this, "empty");
    },
    prefixIconSlot: function prefixIconSlot() {
      return utils_renderTnode.renderTNodeJSX(this, "prefixIcon");
    },
    realLabel: function realLabel() {
      var treeProps = this.treeProps;

      if (!isEmpty__default["default"](treeProps) && !isEmpty__default["default"](treeProps.keys)) {
        return treeProps.keys.label || "label";
      }

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

      if (!isEmpty__default["default"](treeProps) && !isEmpty__default["default"](treeProps.keys)) {
        return treeProps.keys.value || "value";
      }

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

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

    return _asyncToGenerator__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
      return _regeneratorRuntime__default["default"].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__default["default"](_this2.value) ? _this2.value.map(function (item) {
                  return item.value;
                }) : [_this2.value.value];
              } else {
                _this2.actived = isArray__default["default"](_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__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
        var searchInput;
        return _regeneratorRuntime__default["default"].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__default["default"](this.value) && this.value.splice(index, 1);
      this.change(this.value, null);
    },
    change: function change(value, node) {
      utils_event.emitEvent(this, "change", value, {
        node: node
      });
      this.changeNodeInfo();
    },
    clear: function clear(e) {
      var defaultValue = this.multiple ? [] : "";
      this.change(defaultValue, null);
      this.actived = [];
      this.filterText = "";
      utils_event.emitEvent(this, "clear", {
        e: e
      });
    },
    focus: function focus(ctx) {
      this.focusing = true;
      utils_event.emitEvent(this, "focus", _objectSpread({
        value: this.value
      }, ctx));
    },
    blur: function blur(ctx) {
      this.focusing = false;
      this.filterText = "";
      utils_event.emitEvent(this, "blur", _objectSpread({
        value: this.value
      }, ctx));
    },
    remove: function remove(options) {
      utils_event.emitEvent(this, "remove", options);
    },
    search: function search(filterWords) {
      utils_event.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__default["default"](value) ? "" : value[0];
        var node = tree.getItem(nodeValue);
        current = {
          label: node.data[this.realLabel],
          value: node.data[this.realValue]
        };
      } else {
        current = isEmpty__default["default"](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__default["default"](_this5.filter)) {
          var filter = _this5.filter(_this5.filterText, node);

          if (isBoolean__default["default"](filter)) {
            return filter;
          }
        }

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

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

      return _asyncToGenerator__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee4() {
        var tree, nodeValue, node;
        return _regeneratorRuntime__default["default"].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__default["default"](_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__default["default"](_this6.value)) {
                  _this6.nodeInfo = _this6.value.map(function (value) {
                    var nodeValue = _this6.isObjectValue ? value.value : value;

                    if (!isEmpty__default["default"](_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_index.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_index.Input, {
      "ref": "input",
      "directives": [{
        name: "show",
        value: this.showFilter
      }],
      "class": "".concat(config.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_index.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 ? utils_renderTnode.renderTNodeJSX(this, "collapsedItems", {
      params: {
        count: this.tagList.length - this.minCollapsedNum,
        value: this.selectedMultiple,
        collapsedSelectedItems: this.selectedMultiple.slice(this.minCollapsedNum)
      }
    }) : h(tag_index.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_index.Popup, helper.helper([{
      "ref": "popup",
      "class": "".concat(config.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(config.prefix, "-select__left-icon")
    }, [this.prefixIconSlot[0]]), h("span", {
      "directives": [{
        name: "show",
        value: this.showPlaceholder
      }],
      "class": "".concat(config.prefix, "-select__placeholder")
    }, [this.placeholder]), tagItem, collapsedItem, !this.multiple && !this.showPlaceholder && !this.showFilter && h("span", {
      "attrs": {
        "title": this.selectedSingle
      },
      "class": "".concat(config.prefix, "-select__single")
    }, [this.selectedSingle]), searchInput, this.showArrow && !this.showLoading && h(commonComponents_fakeArrow["default"], {
      "attrs": {
        "overlayClassName": "".concat(config.prefix, "-select__right-icon"),
        "overlayStyle": iconStyle,
        "isActive": this.visible && !this.disabled
      }
    }), h(tdesignIconsVue.CloseCircleFilledIcon, {
      "directives": [{
        name: "show",
        value: this.showClose && !this.showLoading
      }],
      "class": ["".concat(config.prefix, "-select__right-icon"), "".concat(config.prefix, "-select__right-icon-clear")],
      "attrs": {
        "size": this.size
      },
      "nativeOn": {
        "click": this.clear
      }
    }), h(loading_index.Loading, {
      "directives": [{
        name: "show",
        value: this.showLoading
      }],
      "class": "".concat(config.prefix, "-select__right-icon ").concat(config.prefix, "-select__active-icon"),
      "attrs": {
        "size": "small"
      }
    })]), h("div", {
      "slot": "content"
    }, [h("p", {
      "directives": [{
        name: "show",
        value: this.showLoading
      }],
      "class": "".concat(config.prefix, "-select__loading-tips")
    }, [this.loadingTextSlot]), treeItem])])]);
  }
});

exports["default"] = _TreeSelect;
//# sourceMappingURL=tree-select.js.map
