/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { defineComponent, ref, toRefs, watch, computed, onMounted, createVNode, mergeProps, withDirectives, vShow } from 'vue';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import isArray from 'lodash/isArray';
import isEmpty from 'lodash/isEmpty';
import isBoolean from 'lodash/isBoolean';
import isFunction from 'lodash/isFunction';
import isNil from 'lodash/isNil';
import { Tree } from '../tree/index.js';
import { SelectInput } from '../select-input/index.js';
import FakeArrow from '../common-components/fake-arrow.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { useFormDisabled } from '../form/hooks.js';
import { useTNodeJSX, useTNodeDefault } from '../hooks/tnode.js';
import useVModel from '../hooks/useVModel.js';
import useDefaultValue from '../hooks/useDefaultValue.js';
import props from './props.js';
import { useConfig } from '../config-provider/useConfig.js';
import '../utils/withInstall.js';
import '../tree/td-tree.js';
import '../tree/props.js';
import '../tree/constants.js';
import '../tree/useTree.js';
import '../_common/js/tree/tree-store.js';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import 'lodash/isNumber';
import 'lodash/isString';
import 'lodash/difference';
import 'lodash/camelCase';
import 'lodash/isPlainObject';
import 'mitt';
import '../_common/js/tree/tree-node.js';
import 'lodash/isNull';
import 'lodash/uniqueId';
import 'lodash/get';
import '../_common/js/log/log.js';
import '../_common/js/tree/tree-node-model.js';
import 'lodash/isUndefined';
import 'lodash/pick';
import 'lodash/omit';
import '../tree/tree-item.js';
import '@ldesign/icons-vue-next';
import '../checkbox/index.js';
import '../checkbox/checkbox.js';
import '../hooks/useRipple.js';
import '../utils/set-style.js';
import '../hooks/useKeepAnimation.js';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-c9c789a8.js';
import 'dayjs';
import '../checkbox/props.js';
import '../checkbox/constants.js';
import 'lodash/kebabCase';
import 'lodash/cloneDeep';
import '../utils/render-tnode.js';
import 'lodash/isObject';
import '../checkbox/group.js';
import 'lodash/intersection';
import '../hooks/slot.js';
import '../checkbox/checkbox-group-props.js';
import '../loading/index.js';
import '../loading/directive.js';
import '../loading/plugin.js';
import '../utils/dom.js';
import '../utils/easing.js';
import '../loading/loading.js';
import '../hooks/useTeleport.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../_common/js/loading/circle-adapter.js';
import '../_common/js/utils/set-style.js';
import '../_common/js/utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '../hooks/useGlobalIcon.js';
import '../tree/util.js';
import '../tree/hooks/useDraggable.js';
import 'lodash/throttle';
import '../tree/hooks/useOnDrag.js';
import '../tree/hooks/useExposeFunc.js';
import 'lodash/upperFirst';
import '../select-input/select-input.js';
import '../popup/index.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../popup/container.js';
import '../hooks/useResizeObserver.js';
import '../popup/props.js';
import '../select-input/props.js';
import '../select-input/useSingle.js';
import '../input/index.js';
import '../input/input.js';
import '../input/props.js';
import '../input/useInput.js';
import '../form/const.js';
import '../input/useLengthLimit.js';
import '../input/useInputEventHandler.js';
import '../input/useInputWidth.js';
import '../input/input-group.js';
import '../input/input-group-props.js';
import '../select-input/useMultiple.js';
import '../tag-input/index.js';
import '../tag-input/tag-input.js';
import '../tag-input/props.js';
import '../tag-input/hooks/useTagScroll.js';
import '../tag-input/useTagList.js';
import '../tag/index.js';
import '../tag/tag.js';
import '../tag/props.js';
import '../tag/check-tag.js';
import '../tag/check-tag-props.js';
import '../tag-input/hooks/useHover.js';
import '../tag-input/hooks/useDragSorter.js';
import '../select-input/useOverlayInnerStyle.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 = defineComponent({
  name: "TTreeSelect",
  props: props,
  setup: function setup(props2, _ref) {
    var slots = _ref.slots;
    var renderTNodeJSX = useTNodeJSX();
    var renderDefaultTNode = useTNodeDefault();
    var classPrefix = usePrefixClass();
    var _useConfig = useConfig("treeSelect"),
      globalConfig = _useConfig.globalConfig;
    var formDisabled = useFormDisabled();
    var treeRef = ref(null);
    var filterByText = ref(null);
    var actived = ref([]);
    var expanded = ref([]);
    var nodeInfo = ref(null);
    var treeKey = ref(0);
    var _toRefs = toRefs(props2),
      value = _toRefs.value,
      modelValue = _toRefs.modelValue,
      popupVisible = _toRefs.popupVisible,
      inputValue = _toRefs.inputValue;
    var _useVModel = useVModel(value, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      treeSelectValue = _useVModel2[0],
      setTreeSelectValue = _useVModel2[1];
    var _useDefaultValue = useDefaultValue(popupVisible, false, props2.onPopupVisibleChange, "popupVisible"),
      _useDefaultValue2 = _slicedToArray(_useDefaultValue, 2),
      innerVisible = _useDefaultValue2[0],
      setInnerVisible = _useDefaultValue2[1];
    var _useDefaultValue3 = useDefaultValue(inputValue, props2.defaultInputValue, props2.onInputChange, "inputValue"),
      _useDefaultValue4 = _slicedToArray(_useDefaultValue3, 2),
      innerInputValue = _useDefaultValue4[0],
      setInnerInputValue = _useDefaultValue4[1];
    watch(treeSelectValue, /*#__PURE__*/_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 changeNodeInfo();
          case 2:
            if (!props2.multiple) actived.value = nodeInfo.value ? [nodeInfo.value.value] : [];
          case 3:
          case "end":
            return _context.stop();
        }
      }, _callee);
    })));
    watch(function () {
      return props2.data;
    }, /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return changeNodeInfo();
          case 2:
            treeRerender();
          case 3:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    })), {
      deep: true
    });
    var tDisabled = computed(function () {
      return formDisabled.value || props2.disabled;
    });
    var inputPlaceholder = computed(function () {
      var _nodeInfo$value;
      return innerVisible.value && ((_nodeInfo$value = nodeInfo.value) === null || _nodeInfo$value === void 0 ? void 0 : _nodeInfo$value.label) || props2.placeholder || globalConfig.value.placeholder;
    });
    var popupClass = computed(function () {
      return ["".concat(classPrefix.value, "-select__dropdown"), "narrow-scrollbar"];
    });
    var dropdownInnerSize = computed(function () {
      return {
        small: "s",
        medium: "m",
        large: "l"
      }[props2.size];
    });
    var isObjectValue = computed(function () {
      return props2.valueType === "object";
    });
    var checked = computed(function () {
      if (props2.multiple) {
        if (isObjectValue.value) {
          return isArray(treeSelectValue.value) ? treeSelectValue.value.map(function (item) {
            return item.value;
          }) : [];
        }
        return isArray(treeSelectValue.value) ? treeSelectValue.value.map(function (item) {
          return item;
        }) : [];
      }
      return [];
    });
    var multiLimitDisabled = computed(function () {
      return props2.multiple && !!props2.max && isArray(treeSelectValue.value) && props2.max <= treeSelectValue.value.length;
    });
    var realLabel = computed(function () {
      if (!isEmpty(props2.treeProps) && !isEmpty(props2.treeProps.keys)) return props2.treeProps.keys.label || "label";
      return "label";
    });
    var realValue = computed(function () {
      if (!isEmpty(props2.treeProps) && !isEmpty(props2.treeProps.keys)) return props2.treeProps.keys.value || "value";
      return "value";
    });
    var realChildren = computed(function () {
      if (!isEmpty(props2.treeProps) && !isEmpty(props2.treeProps.keys)) return props2.treeProps.keys.children || "children";
      return "children";
    });
    onMounted( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
      return _regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            if (!(!treeSelectValue.value && props2.defaultValue)) {
              _context3.next = 3;
              break;
            }
            _context3.next = 3;
            return change(props2.defaultValue, null, "uncheck");
          case 3:
            if (isObjectValue.value) {
              actived.value = isArray(treeSelectValue.value) ? treeSelectValue.value.map(function (item) {
                return item.value;
              }) : [treeSelectValue.value.value];
            } else {
              actived.value = isArray(treeSelectValue.value) ? treeSelectValue.value : [treeSelectValue.value];
            }
            changeNodeInfo();
          case 5:
          case "end":
            return _context3.stop();
        }
      }, _callee3);
    })));
    var change = function change(valueParam, node, trigger) {
      setTreeSelectValue(valueParam, {
        node: node,
        trigger: trigger
      });
      changeNodeInfo();
    };
    var clear = function clear(content) {
      var _props2$onClear;
      var defaultValue = props2.multiple ? [] : "";
      actived.value = [];
      change(defaultValue, null, "clear");
      (_props2$onClear = props2.onClear) === null || _props2$onClear === void 0 ? void 0 : _props2$onClear.call(props2, {
        e: content.e
      });
    };
    var treeNodeChange = function treeNodeChange(valueParam, context) {
      var current = valueParam;
      if (isObjectValue.value) current = valueParam.map(function (nodeValue) {
        return getTreeNode(props2.data, nodeValue);
      });
      change(current, context.node, "check");
    };
    var treeNodeActive = function treeNodeActive(valueParam, context) {
      if (!props2.multiple) setInnerVisible(false, context);
      if (props2.multiple) return;
      if (treeSelectValue.value === context.node.data[realValue.value]) return;
      var current = valueParam;
      if (isObjectValue.value) {
        var nodeValue = isEmpty(valueParam) ? "" : valueParam[0];
        current = getTreeNode(props2.data, nodeValue);
      } else {
        current = isEmpty(valueParam) ? "" : valueParam[0];
      }
      change(current, context.node, "check");
      actived.value = valueParam;
    };
    var treeNodeExpand = function treeNodeExpand(valueParam) {
      expanded.value = valueParam;
    };
    var treeNodeLoad = function treeNodeLoad() {
      changeNodeInfo();
    };
    var inputChange = function inputChange(value2) {
      var _props2$onSearch2;
      if (!innerVisible.value) {
        var _props2$onSearch;
        (_props2$onSearch = props2.onSearch) === null || _props2$onSearch === void 0 ? void 0 : _props2$onSearch.call(props2, String(value2));
        return;
      }
      setInnerInputValue(value2);
      if (!value2) {
        filterByText.value = null;
        return null;
      }
      filterByText.value = function (node) {
        if (isFunction(props2.filter)) {
          var filter = props2.filter(String(value2), node);
          if (isBoolean(filter)) return filter;
        }
        return node.data[realLabel.value].includes(value2);
      };
      (_props2$onSearch2 = props2.onSearch) === null || _props2$onSearch2 === void 0 ? void 0 : _props2$onSearch2.call(props2, String(value2));
    };
    var tagChange = function tagChange(value2, context) {
      var _props2$onRemove;
      var trigger = context.trigger,
        index = context.index;
      if (["tag-remove", "backspace"].includes(trigger)) isArray(treeSelectValue.value) && treeSelectValue.value.splice(index, 1);
      (_props2$onRemove = props2.onRemove) === null || _props2$onRemove === void 0 ? void 0 : _props2$onRemove.call(props2, {
        value: value2,
        data: null,
        e: context && context.e
      });
      change(treeSelectValue.value, null, trigger);
    };
    var changeNodeInfo = /*#__PURE__*/function () {
      var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
        return _regeneratorRuntime.wrap(function _callee4$(_context4) {
          while (1) switch (_context4.prev = _context4.next) {
            case 0:
              _context4.next = 2;
              return treeSelectValue.value;
            case 2:
              if (!props2.multiple) {
                if (treeSelectValue.value || treeSelectValue.value === 0) nodeInfo.value = getSingleNodeInfo();else nodeInfo.value = "";
              } else if (props2.multiple) {
                if (isArray(treeSelectValue.value)) nodeInfo.value = getMultipleNodeInfo();else nodeInfo.value = [];
              } else {
                nodeInfo.value = null;
              }
            case 3:
            case "end":
              return _context4.stop();
          }
        }, _callee4);
      }));
      return function changeNodeInfo() {
        return _ref5.apply(this, arguments);
      };
    }();
    var getSingleNodeInfo = function getSingleNodeInfo() {
      var _props2$treeProps;
      var nodeValue = isObjectValue.value ? treeSelectValue.value.value : treeSelectValue.value;
      if (treeRef.value && (_props2$treeProps = props2.treeProps) !== null && _props2$treeProps !== void 0 && _props2$treeProps.load) {
        if (!isEmpty(props2.data)) {
          var node2 = treeRef.value.getItem(nodeValue);
          if (node2) return {
            label: node2.data[realLabel.value],
            value: node2.data[realValue.value]
          };
        }
        return {
          label: nodeValue,
          value: nodeValue
        };
      }
      var node = getTreeNode(props2.data, nodeValue);
      if (!node) return {
        label: nodeValue,
        value: nodeValue
      };
      return node;
    };
    var getMultipleNodeInfo = function getMultipleNodeInfo() {
      return treeSelectValue.value.map(function (value2) {
        var _props2$treeProps2;
        var nodeValue = isObjectValue.value ? value2.value : value2;
        if (treeRef.value && (_props2$treeProps2 = props2.treeProps) !== null && _props2$treeProps2 !== void 0 && _props2$treeProps2.load) {
          if (!isEmpty(props2.data)) {
            var node2 = treeRef.value.getItem(nodeValue);
            if (node2) return {
              label: node2.data[realLabel.value],
              value: node2.data[realValue.value]
            };
          }
          return {
            label: nodeValue,
            value: nodeValue
          };
        }
        var node = getTreeNode(props2.data, nodeValue);
        if (!node) return {
          label: nodeValue,
          value: nodeValue
        };
        return node;
      });
    };
    var getTreeNode = function getTreeNode(data, targetValue) {
      for (var i = 0, len = data.length; i < len; i++) {
        var _data$i;
        if (data[i][realValue.value] === targetValue) return {
          label: data[i][realLabel.value],
          value: data[i][realValue.value]
        };
        if ((_data$i = data[i]) !== null && _data$i !== void 0 && _data$i[realChildren.value]) {
          var _data$i2;
          var result = getTreeNode((_data$i2 = data[i]) === null || _data$i2 === void 0 ? void 0 : _data$i2[realChildren.value], targetValue);
          if (!isNil(result)) return result;
        }
      }
      return null;
    };
    var treeRerender = function treeRerender() {
      treeKey.value += 1;
    };
    var renderTree = function renderTree() {
      return withDirectives(createVNode(Tree, mergeProps({
        "ref": treeRef,
        "key": treeKey.value,
        "value": _toConsumableArray(checked.value),
        "hover": true,
        "data": props2.data,
        "activable": !props2.multiple,
        "checkable": props2.multiple,
        "disabled": tDisabled.value || multiLimitDisabled.value,
        "size": props2.size,
        "filter": filterByText.value,
        "icon": !filterByText.value,
        "actived": actived.value,
        "expanded": expanded.value,
        "activeMultiple": props2.multiple,
        "onChange": treeNodeChange,
        "onActive": treeNodeActive,
        "onExpand": treeNodeExpand,
        "onLoad": treeNodeLoad,
        "expandOnClickNode": true
      }, props2.treeProps), {
        empty: function empty() {
          return renderDefaultTNode("empty", {
            defaultNode: createVNode("div", {
              "class": "".concat(classPrefix.value, "-select__empty")
            }, [globalConfig.value.empty])
          });
        }
      }), [[vShow, !props2.loading]]);
    };
    var renderSuffixIcon = function renderSuffixIcon() {
      var _ref6;
      return createVNode(FakeArrow, {
        "isActive": innerVisible.value,
        "disabled": props2.disabled,
        "overlayClassName": (_ref6 = {}, _defineProperty(_ref6, "".concat(classPrefix.value, "-fake-arrow--highlight"), innerVisible.value), _defineProperty(_ref6, "".concat(classPrefix.value, "-fake-arrow--disable"), props2.disabled), _ref6)
      }, null);
    };
    return function () {
      return createVNode(SelectInput, mergeProps({
        "class": "".concat(classPrefix.value, "-tree-select"),
        "value": nodeInfo.value,
        "inputValue": innerVisible.value ? innerInputValue.value : "",
        "popupVisible": innerVisible.value,
        "disabled": tDisabled.value,
        "multiple": props2.multiple,
        "loading": props2.loading,
        "clearable": props2.clearable,
        "autoWidth": props2.autoWidth,
        "borderless": props2.borderless,
        "readonly": props2.readonly,
        "placeholder": inputPlaceholder.value,
        "allowInput": props2.filterable || isFunction(props2.filter),
        "minCollapsedNum": props2.minCollapsedNum,
        "collapsed-items": props2.collapsedItems,
        "popupProps": _objectSpread({
          overlayClassName: popupClass.value
        }, props2.popupProps),
        "inputProps": _objectSpread({
          size: props2.size
        }, props2.inputProps),
        "tagInputProps": {
          size: props2.size
        },
        "tagProps": _objectSpread({
          maxWidth: 300
        }, props2.tagProps),
        "label": function label() {
          return renderTNodeJSX("prefixIcon");
        },
        "suffixIcon": function suffixIcon() {
          return renderSuffixIcon();
        },
        "onClear": clear,
        "onBlur": function onBlur(_, context) {
          var _props2$onBlur;
          (_props2$onBlur = props2.onBlur) === null || _props2$onBlur === void 0 ? void 0 : _props2$onBlur.call(props2, {
            value: treeSelectValue.value,
            e: context.e
          });
        },
        "onFocus": function onFocus(_, context) {
          var _props2$onFocus;
          (_props2$onFocus = props2.onFocus) === null || _props2$onFocus === void 0 ? void 0 : _props2$onFocus.call(props2, {
            value: treeSelectValue.value,
            e: context.e
          });
        },
        "valueDisplay": function valueDisplay() {
          var _ref7;
          return renderTNodeJSX("valueDisplay", {
            params: props2.multiple ? {
              value: nodeInfo.value,
              onClose: function onClose(value2, context) {
                tagChange(value2, context);
              }
            } : {
              value: nodeInfo.value || (_ref7 = {}, _defineProperty(_ref7, realLabel.value, ""), _defineProperty(_ref7, realValue.value, void 0), _ref7)
            }
          });
        },
        "onInputChange": inputChange,
        "onTagChange": tagChange,
        "onPopupVisibleChange": function onPopupVisibleChange(state, context) {
          return setInnerVisible(state, context);
        }
      }, props2.selectInputProps), {
        panel: function panel() {
          return createVNode("div", {
            "class": ["".concat(classPrefix.value, "-select__dropdown-inner"), "".concat(classPrefix.value, "-select__dropdown-inner--size-").concat(dropdownInnerSize.value)]
          }, [withDirectives(createVNode("p", {
            "class": ["".concat(classPrefix.value, "-select-loading-tips"), "".concat(classPrefix.value, "-select__right-icon-polyfill")]
          }, [renderDefaultTNode("loadingText", {
            defaultNode: createVNode("div", {
              "class": "".concat(classPrefix.value, "-select__empty")
            }, [globalConfig.value.loadingText])
          })]), [[vShow, props2.loading && !tDisabled.value]]), renderTree()]);
        },
        collapsedItems: slots.collapsedItems
      });
    };
  }
});

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