/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

'use strict';

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

var helper = require('../../chunks/helper.js');
var VueCompositionAPI = require('@vue/composition-api');
var isArray = require('lodash/isArray');
var isEmpty = require('lodash/isEmpty');
var isBoolean = require('lodash/isBoolean');
var isFunction = require('lodash/isFunction');
var isNil = require('lodash/isNil');
var components_tree_index = require('../tree/index.js');
var components_selectInput_index = require('../select-input/index.js');
var components_common_fakeArrow = require('../common/fake-arrow.js');
var hooks_useConfig = require('../../hooks/useConfig.js');
var hooks_useFormDisabled = require('../../hooks/useFormDisabled.js');
var hooks_lnode = require('../../hooks/lnode.js');
var hooks_useVModel = require('../../hooks/useVModel.js');
var hooks_useDefaultValue = require('../../hooks/useDefaultValue.js');
var components_treeSelect_props = require('./props.js');
var components_configProvider_useConfig = require('../config-provider/useConfig.js');
require('../../utils/map-props.js');
require('vue');
require('../../utils/withInstall.js');
require('lodash/capitalize');
require('../../config.js');
require('../tree/tree.js');
require('lodash/upperFirst');
require('../../utils/render-node.js');
require('lodash');
require('../tree/props.js');
require('../tree/hooks/useTreeStore.js');
require('lodash/pick');
require('../../chunks/tree-store.js');
require('../../chunks/defineProperty-19da3d4c.js');
require('../../chunks/get-fb5b05c8.js');
require('../../chunks/isArray-6b6e7094.js');
require('../../chunks/isSymbol-0ea4cedf.js');
require('../../chunks/isObjectLike-51896b77.js');
require('../../chunks/_commonjsHelpers-fe796db6.js');
require('../../chunks/isFunction-cb08cfdf.js');
require('../../chunks/tree-node.js');
require('../../chunks/log.js');
require('../../chunks/_isIndex-8b06cfbb.js');
require('../tree/hooks/useCache.js');
require('../tree/hooks/useTreeAction.js');
require('../tree/utils.js');
require('lodash/camelCase');
require('../tree/hooks/useTreeNodes.js');
require('../tree/tree-item.js');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../../chunks/zh_CN.js');
require('../../utils/ripple.js');
require('../../chunks/set-style.js');
require('../tree/hooks/useTreeItem.js');
require('../tree/hooks/useItemEvents.js');
require('../tree/hooks/useRenderIcon.js');
require('@ldesign/icons-vue');
require('../../hooks/useGlobalIcon.js');
require('../loading/index.js');
require('../loading/loading.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../utils/transfer-dom.js');
require('../../utils/mixins.js');
require('../loading/props.js');
require('../loading/icon/gradient.js');
require('../../chunks/helper2.js');
require('../loading/plugin.js');
require('../tree/hooks/useRenderLabel.js');
require('../checkbox/index.js');
require('../checkbox/group.js');
require('lodash/intersection');
require('../../utils/event.js');
require('../../utils/helper.js');
require('../checkbox/checkbox.js');
require('../checkbox/props.js');
require('../tree/hooks/useRenderLine.js');
require('../tree/hooks/useRenderOperations.js');
require('../tree/hooks/useDraggable.js');
require('lodash/throttle');
require('../tree/hooks/useTreeEvents.js');
require('../tree/hooks/useDragHandle.js');
require('../select-input/select-input.js');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../popup/props.js');
require('../popup/container.js');
require('../select-input/props.js');
require('../select-input/hooks/use-single.js');
require('lodash/isObject');
require('../input/index.js');
require('../input/input.js');
require('lodash/kebabCase');
require('../input/props.js');
require('../input/group.js');
require('../select-input/hooks/use-multiple.js');
require('../tag-input/index.js');
require('../tag-input/tag-input.js');
require('../tag-input/props.js');
require('../tag-input/hooks/use-tag-scroll.js');
require('../tag-input/hooks/use-tag-list.js');
require('../tag/index.js');
require('../tag/tag.js');
require('../tag/props.js');
require('../tag/check-tag.js');
require('../tag-input/hooks/use-hover.js');
require('../tag-input/hooks/use-drag-sorter.js');
require('../../hooks/render-lnode.js');
require('../select-input/hooks/use-overlay-innerstyle.js');

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

var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);
var isEmpty__default = /*#__PURE__*/_interopDefaultLegacy(isEmpty);
var isBoolean__default = /*#__PURE__*/_interopDefaultLegacy(isBoolean);
var isFunction__default = /*#__PURE__*/_interopDefaultLegacy(isFunction);
var isNil__default = /*#__PURE__*/_interopDefaultLegacy(isNil);

var _TreeSelect = VueCompositionAPI.defineComponent({
  name: "LTreeSelect",
  props: {
    ...components_treeSelect_props["default"]
  },
  setup(props2, _ref) {
    let {
      emit
    } = _ref;
    const classPrefix = hooks_useConfig.usePrefixClass();
    const {
      global
    } = components_configProvider_useConfig.useConfig("treeSelect");
    const {
      formDisabled
    } = hooks_useFormDisabled["default"]();
    const treeRef = VueCompositionAPI.ref(null);
    const filterByText = VueCompositionAPI.ref(null);
    const activated = VueCompositionAPI.ref([]);
    const expanded = VueCompositionAPI.ref([]);
    const nodeInfo = VueCompositionAPI.ref(null);
    const treeKey = VueCompositionAPI.ref(0);
    const {
      value,
      popupVisible,
      inputValue
    } = VueCompositionAPI.toRefs(props2);
    const [treeSelectValue, setTreeSelectValue] = hooks_useVModel["default"](value, props2.defaultValue, props2.onChange, "change");
    const [innerVisible, setInnerVisible] = hooks_useDefaultValue["default"](popupVisible, false, props2.onPopupVisibleChange, "popupVisible", "popup-visible-change");
    const [innerInputValue, setInnerInputValue] = hooks_useDefaultValue["default"](inputValue, props2.defaultInputValue, props2.onInputChange, "inputValue", "input-change");
    VueCompositionAPI.watch(treeSelectValue, async () => {
      await changeNodeInfo();
      if (!props2.multiple) activated.value = nodeInfo.value ? [nodeInfo.value.value] : [];
    });
    VueCompositionAPI.watch(() => props2.data, async () => {
      await changeNodeInfo();
      treeRerender();
    });
    const tDisabled = VueCompositionAPI.computed(() => formDisabled.value || props2.disabled);
    const inputPlaceholder = VueCompositionAPI.computed(() => innerVisible.value && nodeInfo.value?.label || props2.placeholder || global.value.placeholder);
    const popupClass = VueCompositionAPI.computed(() => [`${classPrefix.value}-select__dropdown`, "narrow-scrollbar"]);
    const dropdownInnerSize = VueCompositionAPI.computed(() => ({
      small: "s",
      medium: "m",
      large: "l"
    })[props2.size]);
    const isObjectValue = VueCompositionAPI.computed(() => props2.valueType === "object");
    const checked = VueCompositionAPI.computed(() => {
      if (props2.multiple) {
        if (isObjectValue.value) {
          return isArray__default["default"](treeSelectValue.value) ? treeSelectValue.value.map(item => item.value) : [];
        }
        return isArray__default["default"](treeSelectValue.value) ? treeSelectValue.value.map(item => item) : [];
      }
      return [];
    });
    const multiLimitDisabled = VueCompositionAPI.computed(() => props2.multiple && !!props2.max && isArray__default["default"](treeSelectValue.value) && props2.max <= treeSelectValue.value.length);
    const realLabel = VueCompositionAPI.computed(() => {
      if (!isEmpty__default["default"](props2.treeProps) && !isEmpty__default["default"](props2.treeProps.keys)) return props2.treeProps.keys.label || "label";
      return "label";
    });
    const realValue = VueCompositionAPI.computed(() => {
      if (!isEmpty__default["default"](props2.treeProps) && !isEmpty__default["default"](props2.treeProps.keys)) return props2.treeProps.keys.value || "value";
      return "value";
    });
    const realChildren = VueCompositionAPI.computed(() => {
      if (!isEmpty__default["default"](props2.treeProps) && !isEmpty__default["default"](props2.treeProps.keys)) return props2.treeProps.keys.children || "children";
      return "children";
    });
    VueCompositionAPI.onMounted(async () => {
      if (!treeSelectValue.value && props2.defaultValue) await change(props2.defaultValue, null, "uncheck");
      if (isObjectValue.value) {
        activated.value = isArray__default["default"](treeSelectValue.value) ? treeSelectValue.value.map(item => item.value) : [treeSelectValue.value.value];
      } else {
        activated.value = isArray__default["default"](treeSelectValue.value) ? treeSelectValue.value : [treeSelectValue.value];
      }
      changeNodeInfo();
    });
    const change = (valueParam, node, trigger) => {
      setTreeSelectValue(valueParam, {
        node,
        trigger
      });
      changeNodeInfo();
    };
    const clear = content => {
      const defaultValue = props2.multiple ? [] : "";
      activated.value = [];
      change(defaultValue, null, "clear");
      props2.onClear?.({
        e: content.e
      });
      emit("clear", {
        e: content.e
      });
    };
    const treeNodeChange = (valueParam, context) => {
      let current = valueParam;
      if (isObjectValue.value) current = valueParam.map(nodeValue => getTreeNode(props2.data, nodeValue));
      change(current, context.node, "check");
    };
    const treeNodeActive = (valueParam, context) => {
      if (props2.multiple) return;
      setInnerVisible(false, {});
      if (treeSelectValue.value === context.node.data[realValue.value]) return;
      let current = valueParam;
      if (isObjectValue.value) {
        const nodeValue = isEmpty__default["default"](valueParam) ? "" : valueParam[0];
        current = getTreeNode(props2.data, nodeValue);
      } else {
        current = isEmpty__default["default"](valueParam) ? "" : valueParam[0];
      }
      change(current, context.node, "check");
      activated.value = valueParam;
    };
    const treeNodeExpand = valueParam => {
      expanded.value = valueParam;
    };
    const treeNodeLoad = () => {
      changeNodeInfo();
    };
    const inputChange = value2 => {
      const searchValue = String(value2);
      if (!innerVisible.value) {
        props2.onSearch?.(searchValue);
        emit("search", searchValue);
        return;
      }
      setInnerInputValue(value2);
      if (!value2) {
        filterByText.value = null;
        return null;
      }
      filterByText.value = node => {
        if (isFunction__default["default"](props2.filter)) {
          const filter = props2.filter(searchValue, node);
          if (isBoolean__default["default"](filter)) return filter;
        }
        return node.data[realLabel.value].includes(value2);
      };
      props2.onSearch?.(searchValue);
      emit("search", searchValue);
    };
    const tagChange = (value2, context) => {
      const {
        trigger,
        index
      } = context;
      if (["tag-remove", "backspace"].includes(trigger)) isArray__default["default"](treeSelectValue.value) && treeSelectValue.value.splice(index, 1);
      props2.onRemove?.({
        value: value2,
        data: null,
        e: context && context.e
      });
      emit("remove", {
        value: value2,
        data: null,
        e: context && context.e
      });
      change(treeSelectValue.value, null, trigger);
    };
    const changeNodeInfo = async () => {
      await treeSelectValue.value;
      if (!props2.multiple) {
        if (treeSelectValue.value || treeSelectValue.value === 0) nodeInfo.value = getSingleNodeInfo();else nodeInfo.value = "";
      } else if (props2.multiple) {
        if (isArray__default["default"](treeSelectValue.value)) nodeInfo.value = getMultipleNodeInfo();else nodeInfo.value = [];
      } else {
        nodeInfo.value = null;
      }
    };
    const getSingleNodeInfo = () => {
      const nodeValue = isObjectValue.value ? treeSelectValue.value.value : treeSelectValue.value;
      if (treeRef.value && props2.treeProps?.load) {
        if (!isEmpty__default["default"](props2.data)) {
          const node2 = treeRef.value.getItem(nodeValue);
          if (node2) return {
            label: node2.data[realLabel.value],
            value: node2.data[realValue.value]
          };
        }
        return {
          label: nodeValue,
          value: nodeValue
        };
      }
      const node = getTreeNode(props2.data, nodeValue);
      if (!node) return {
        label: nodeValue,
        value: nodeValue
      };
      return node;
    };
    const getMultipleNodeInfo = () => treeSelectValue.value.map(value2 => {
      const nodeValue = isObjectValue.value ? value2.value : value2;
      if (treeRef.value && props2.treeProps?.load) {
        if (!isEmpty__default["default"](props2.data)) {
          const node2 = treeRef.value.getItem(nodeValue);
          if (node2) return {
            label: node2.data[realLabel.value],
            value: node2.data[realValue.value]
          };
        }
        return {
          label: nodeValue,
          value: nodeValue
        };
      }
      const node = getTreeNode(props2.data, nodeValue);
      if (!node) return {
        label: nodeValue,
        value: nodeValue
      };
      return node;
    });
    const getTreeNode = (data, targetValue) => {
      for (let i = 0, len = data.length; i < len; i++) {
        if (data[i][realValue.value] === targetValue) return {
          label: data[i][realLabel.value],
          value: data[i][realValue.value]
        };
        if (data[i]?.[realChildren.value]) {
          const result = getTreeNode(data[i]?.[realChildren.value], targetValue);
          if (!isNil__default["default"](result)) return result;
        }
      }
      return null;
    };
    const treeRerender = () => {
      treeKey.value += 1;
    };
    return {
      innerVisible,
      classPrefix,
      nodeInfo,
      inputPlaceholder,
      popupClass,
      clear,
      setInnerVisible,
      tagChange,
      treeKey,
      treeRef,
      checked,
      innerInputValue,
      tDisabled,
      filterByText,
      activated,
      realLabel,
      realValue,
      inputChange,
      dropdownInnerSize,
      global,
      treeNodeChange,
      treeNodeExpand,
      treeNodeActive,
      multiLimitDisabled,
      treeNodeLoad,
      expanded,
      emit
    };
  },
  methods: {
    renderSuffixIcon() {
      const h = this.$createElement;
      return h(components_common_fakeArrow["default"], {
        "attrs": {
          "isActive": this.innerVisible,
          "disabled": this.disabled,
          "overlayClassName": {
            [`${this.classPrefix}-fake-arrow--highlight`]: this.innerVisible,
            [`${this.classPrefix}-fake-arrow--disable`]: this.disabled
          }
        }
      });
    }
  },
  render() {
    const h = arguments[0];
    const renderTNodeJSX = hooks_lnode.useLNodeJSX();
    const renderDefaultTNode = hooks_lnode.useLNodeDefault();
    return h(components_selectInput_index.SelectInput, helper.helper([{}, {
      class: `${this.classPrefix}-tree-select`,
      props: {
        value: this.nodeInfo,
        inputValue: this.innerVisible ? this.innerInputValue : "",
        popupVisible: this.innerVisible,
        disabled: this.tDisabled,
        multiple: this.multiple,
        loading: this.loading,
        clearable: this.clearable,
        autoWidth: this.autoWidth,
        borderless: this.borderless,
        readonly: this.readonly,
        placeholder: this.inputPlaceholder,
        allowInput: this.filterable || isFunction__default["default"](this.filter),
        minCollapsedNum: this.minCollapsedNum,
        popupProps: {
          overlayClassName: this.popupClass,
          ...this.popupProps
        },
        inputProps: {
          size: this.size,
          ...this.inputProps
        },
        tagInputProps: {
          size: this.size
        },
        tagProps: {
          maxWidth: 300,
          ...this.tagProps
        },
        label: () => renderTNodeJSX("prefixIcon"),
        suffixIcon: this.renderSuffixIcon,
        onClear: this.clear,
        onBlur: (value, context) => {
          this.onBlur?.({
            value,
            e: context.e
          });
          this.emit("blur", {
            value,
            e: context.e
          });
        },
        onFocus: (value, context) => {
          this.onFocus?.({
            value,
            e: context.e
          });
          this.emit("focus", {
            value,
            e: context.e
          });
        },
        onInputChange: this.inputChange,
        onTagChange: this.tagChange,
        onPopupVisibleChange: (state, context) => this.setInnerVisible(state, context),
        valueDisplay: () => renderTNodeJSX("valueDisplay", {
          params: this.multiple ? {
            value: this.nodeInfo,
            onClose: (value, context) => {
              this.tagChange(value, context);
            }
          } : {
            value: this.nodeInfo || {
              [this.realLabel]: "",
              [this.realValue]: void 0
            }
          }
        }),
        panel: () => h("div", {
          "class": [`${this.classPrefix}-select__dropdown-inner`, `${this.classPrefix}-select__dropdown-inner--size-${this.dropdownInnerSize}`]
        }, [this.loading && !this.tDisabled ? h("p", {
          "class": [`${this.classPrefix}-select-loading-tips`, `${this.classPrefix}-select__right-icon-polyfill`]
        }, [renderDefaultTNode("loadingText", {
          defaultNode: h("div", {
            "class": `${this.classPrefix}-select__empty`
          }, [this.global.loadingText])
        })]) : null, !this.loading ? h(components_tree_index.Tree, {
          "props": {
            ...{
              ref: this.treeRef,
              key: this.treeKey,
              value: [...this.checked],
              hover: true,
              data: this.data,
              activable: !this.multiple,
              checkable: this.multiple,
              disabled: this.tDisabled || this.multiLimitDisabled,
              size: this.size,
              filter: this.filterByText,
              icon: !this.filterByText,
              actived: this.activated,
              expanded: this.expanded,
              activeMultiple: this.multiple,
              onChange: this.treeNodeChange,
              onActive: this.treeNodeActive,
              onExpand: this.treeNodeExpand,
              onLoad: this.treeNodeLoad,
              expandOnClickNode: true,
              empty: () => renderDefaultTNode("empty", {
                defaultNode: h("div", {
                  "class": `${this.classPrefix}-select__empty`
                }, [this.global.empty])
              }),
              ...this.treeProps
            }
          }
        }) : null]),
        collapsedItems: () => renderTNodeJSX("collapsedItems"),
        ...this.selectInputProps
      }
    }]));
  }
});

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