/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { h as helper } from '../../chunks/helper.js';
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 '@ldesign/icons-vue';
import { Loading } from '../loading/index.js';
import mixins from '../../utils/mixins.js';
import getConfigReceiverMixins, { getGlobalIconMixins } from '../config-provider/config-receiver.js';
import { renderLNodeJSX } from '../../utils/render-node.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/fake-arrow.js';
import props from './props.js';
import '../../chunks/_commonjsHelpers.js';
import '../../utils/with-install.js';
import 'vue';
import 'lodash';
import '../../config.js';
import '../loading/loading.js';
import '../../utils/dom.js';
import 'raf';
import '../../chunks/index.js';
import '../../chunks/log.js';
import '../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../utils/easing.js';
import '../../utils/transfer-dom.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../../chunks/defineProperty-838e4192.js';
import '../../chunks/set-style.js';
import '../../chunks/helper2.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../loading/style/css.js';
import '../loading/type.js';
import '../loading/plugin.js';
import '../../utils/helper.js';
import 'lodash/camelCase';
import '../../utils/map-props.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../popup/props.js';
import '../popup/container.js';
import '../popup/style/css.js';
import '../popup/type.js';
import '../tag/tag.js';
import '../tag/props.js';
import '../tag/check-tag.js';
import '../tag/style/css.js';
import '../tag/type.js';
import '../tree/tree.js';
import 'lodash/upperFirst';
import 'lodash/pick';
import '../../chunks/tree-store.js';
import '../../chunks/get-5e1abd10.js';
import '../../chunks/isArray-cd7357b9.js';
import '../../chunks/isSymbol-a38a5184.js';
import '../../chunks/isObjectLike-c22c7e3b.js';
import '../../chunks/isFunction-91acade7.js';
import '../../chunks/tree-node.js';
import '../../chunks/_isIndex-52887d1e.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/style/css.js';
import '../checkbox/type.js';
import '../../utils/ripple.js';
import '../tree/utils.js';
import '../tree/props.js';
import '../tree/style/css.js';
import '../tree/type.js';
import '../input/input.js';
import 'lodash/kebabCase';
import '../input/props.js';
import '../input/group.js';
import '../input/style/css.js';
import '../input/type.js';

var _TreeSelect = mixins(getConfigReceiverMixins("treeSelect"), getGlobalIconMixins()).extend({
  name: "LTreeSelect",
  model: {
    prop: "value",
    event: "change"
  },
  components: {
    LTree: Tree,
    LLoading: Loading,
    LFakeArrow: FakeArrow,
    LPopup: Popup,
    LTag: Tag,
    LInput: Input
  },
  props,
  data() {
    return {
      formDisabled: void 0,
      visible: false,
      isHover: false,
      focusing: false,
      defaultProps: {
        trigger: "click",
        placement: "bottom-left",
        overlayClassName: "",
        overlayInnerStyle: trigger => ({
          width: `${trigger.offsetWidth}px`
        })
      },
      filterText: "",
      filterByText: null,
      actived: [],
      expanded: [],
      nodeInfo: null,
      treeKey: 0
    };
  },
  watch: {
    async value() {
      await this.changeNodeInfo();
      if (!this.multiple) this.actived = this.nodeInfo ? [this.nodeInfo.value] : [];
    },
    async data() {
      await this.changeNodeInfo();
      this.treeRerender();
    }
  },
  computed: {
    lDisabled() {
      return this.formDisabled || this.disabled;
    },
    classes() {
      return [`${this.classPrefix}-select`, `${this.classPrefix}-select-polyfill`, {
        [this.commonStatusClassName.disabled]: this.lDisabled,
        [this.commonStatusClassName.active]: this.visible,
        [this.commonSizeClassName[this.size]]: this.size,
        [`${this.classPrefix}-has-prefix`]: this.prefixIconSlot,
        [`${this.classPrefix}-select-selected`]: this.selectedSingle || !isEmpty(this.selectedMultiple)
      }];
    },
    popupClass() {
      const {
        popupObject
      } = this;
      return `${popupObject.overlayClassName} ${this.classPrefix}-select__dropdown narrow-scrollbar`;
    },
    dropdownInnerSize() {
      const sizeMap = {
        small: "small",
        medium: "medium",
        large: "large"
      };
      return sizeMap[this.size];
    },
    isObjectValue() {
      return this.valueType === "object";
    },
    checked() {
      if (this.multiple) {
        if (this.isObjectValue) return isArray(this.value) ? this.value.map(item => item.value) : [];
        return isArray(this.value) ? this.value : [];
      }
      return [];
    },
    showArrow() {
      return !this.clearable || !this.isHover || this.lDisabled || !this.multiple && !this.value && this.value !== 0 || this.multiple && isArray(this.value) && isEmpty(this.value);
    },
    showLoading() {
      return this.loading && !this.lDisabled;
    },
    showClose() {
      return this.clearable && this.isHover && !this.lDisabled && (!this.multiple && (!!this.value || this.value === 0) || this.multiple && !isEmpty(this.value));
    },
    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() {
      if (this.lDisabled) return false;
      if (!this.multiple && this.selectedSingle && (this.filterable || isFunction(this.filter))) return this.visible;
      return this.filterable || isFunction(this.filter);
    },
    showTree() {
      return !this.loading;
    },
    popupObject() {
      const propsObject = this.popupProps ? {
        ...this.defaultProps,
        ...this.popupProps
      } : this.defaultProps;
      return propsObject;
    },
    selectedSingle() {
      if (!this.multiple && (isString(this.value) || isNumber(this.value) || isObject(this.value))) {
        if (this.nodeInfo) return this.nodeInfo.label;
        return `${this.value}`;
      }
      return "";
    },
    selectedMultiple() {
      if (this.multiple && isArray(this.value) && !isEmpty(this.value)) return this.value;
      return [];
    },
    multiLimitDisabled() {
      if (this.multiple && this.max && isArray(this.value) && this.max <= this.value.length) return true;
      return false;
    },
    filterPlaceholder() {
      if (this.multiple && isArray(this.value) && !isEmpty(this.value)) return "";
      const single = Array.isArray(this.selectedSingle) ? this.selectedSingle[0] : this.selectedSingle;
      if (!this.multiple && single) return String(single);
      return this.placeholder;
    },
    loadingTextSlot() {
      const h = this.$createElement;
      const useLocale = !this.loadingText && !this.$scopedSlots.loadingText;
      return useLocale ? h("div", {
        "class": `${this.classPrefix}-select__empty`
      }, [this.t(this.global.loadingText)]) : renderLNodeJSX(this, "loadingText");
    },
    emptySlot() {
      const h = this.$createElement;
      const useLocale = !this.empty && !this.$scopedSlots.empty;
      return useLocale ? h("div", {
        "class": `${this.classPrefix}-select__empty`
      }, [this.t(this.global.empty)]) : renderLNodeJSX(this, "empty");
    },
    prefixIconSlot() {
      return renderLNodeJSX(this, "prefixIcon");
    },
    realLabel() {
      const {
        treeProps
      } = this;
      if (!isEmpty(treeProps) && !isEmpty(treeProps.keys)) return treeProps.keys.label || "label";
      return "label";
    },
    realValue() {
      const {
        treeProps
      } = this;
      if (!isEmpty(treeProps) && !isEmpty(treeProps.keys)) return treeProps.keys.value || "value";
      return "value";
    },
    realChildren() {
      var _treeProps$keys;
      const {
        treeProps
      } = this;
      return (treeProps === null || treeProps === void 0 ? void 0 : (_treeProps$keys = treeProps.keys) === null || _treeProps$keys === void 0 ? void 0 : _treeProps$keys.children) || "children";
    },
    tagList() {
      if (this.nodeInfo && isArray(this.nodeInfo)) return this.nodeInfo.map(node => node.label);
      return this.selectedMultiple;
    }
  },
  async mounted() {
    if (!this.value && this.defaultValue) await this.change(this.defaultValue, null);
    if (this.isObjectValue) {
      this.actived = isArray(this.value) ? this.value.map(item => item.value) : [this.value.value];
    } else {
      this.actived = isArray(this.value) ? this.value : [this.value];
    }
    this.changeNodeInfo();
  },
  methods: {
    async popupVisibleChange(visible) {
      await (this.visible = visible);
      this.filterText = "";
      this.filterByText = null;
      if (this.showFilter && this.visible) {
        const searchInput = this.$refs.input;
        searchInput === null || searchInput === void 0 ? void 0 : searchInput.focus();
        this.focusing = true;
      }
    },
    removeTag(index, data, e) {
      if (this.lDisabled) return;
      this.remove({
        value: this.value[index],
        data,
        e
      });
      isArray(this.value) && this.value.splice(index, 1);
      this.change(this.value, null);
    },
    change(value, node) {
      emitEvent(this, "change", value, {
        node
      });
      this.changeNodeInfo();
    },
    clear(e) {
      const defaultValue = this.multiple ? [] : "";
      this.change(defaultValue, null);
      this.actived = [];
      this.filterText = "";
      this.filterByText = null;
      emitEvent(this, "clear", {
        e
      });
    },
    focus(ctx) {
      this.focusing = true;
      emitEvent(this, "focus", {
        value: this.value,
        ...ctx
      });
    },
    blur(ctx) {
      this.focusing = false;
      emitEvent(this, "blur", {
        value: this.value,
        ...ctx
      });
    },
    remove(options) {
      emitEvent(this, "remove", options);
    },
    search(filterWords) {
      emitEvent(this, "search", filterWords);
    },
    treeNodeChange(value, context) {
      let current = value;
      if (this.isObjectValue) current = value.map(nodeValue => this.getTreeNode(this.data, nodeValue));
      this.change(current, context.node);
      this.actived = value;
    },
    treeNodeActive(value, context) {
      if (this.multiple) return;
      const triggerValue = this.isObjectValue ? context.node.data : context.node.data[this.realValue];
      this.change(triggerValue, context.node);
      this.actived = [triggerValue];
      this.visible = false;
    },
    treeNodeExpand(value) {
      this.expanded = value;
    },
    onInputChange() {
      if (!this.filterText) {
        this.filterByText = null;
        return null;
      }
      this.filterByText = node => {
        if (isFunction(this.filter)) {
          const filter = this.filter(this.filterText, node);
          if (isBoolean(filter)) return filter;
        }
        return node.data[this.realLabel].includes(this.filterText);
      };
      this.search(this.filterText);
    },
    getTreeData() {
      var _this$$refs$tree;
      return (((_this$$refs$tree = this.$refs.tree) === null || _this$$refs$tree === void 0 ? void 0 : _this$$refs$tree.getItems()) || []).map(item => ({
        [this.realLabel]: item.data[this.realLabel],
        [this.realValue]: item.data[this.realValue]
      }));
    },
    async changeNodeInfo() {
      await this.value;
      if (!this.multiple && (this.value || this.value === 0)) this.changeSingleNodeInfo();else if (this.multiple && isArray(this.value)) this.changeMultipleNodeInfo();else this.nodeInfo = null;
    },
    changeSingleNodeInfo() {
      var _this$treeProps;
      const {
        tree
      } = this.$refs;
      const nodeValue = this.isObjectValue ? this.value.value : this.value;
      if (tree && (_this$treeProps = this.treeProps) !== null && _this$treeProps !== void 0 && _this$treeProps.load) {
        if (!isEmpty(this.data)) {
          const node = tree.getItem(nodeValue);
          if (!node) return;
          this.nodeInfo = {
            label: node.data[this.realLabel],
            value: node.data[this.realValue]
          };
        } else {
          this.nodeInfo = {
            label: nodeValue,
            value: nodeValue
          };
        }
      } else {
        const node = this.getTreeNode(this.data, nodeValue);
        if (!node) this.nodeInfo = {
          label: nodeValue,
          value: nodeValue
        };else this.nodeInfo = node;
      }
    },
    changeMultipleNodeInfo() {
      const {
        tree
      } = this.$refs;
      this.nodeInfo = this.value.map(value => {
        var _this$treeProps2;
        const nodeValue = this.isObjectValue ? value.value : value;
        if (tree && (_this$treeProps2 = this.treeProps) !== null && _this$treeProps2 !== void 0 && _this$treeProps2.load) {
          if (!isEmpty(this.data)) {
            const node2 = tree.getItem(nodeValue);
            if (!node2) return false;
            return {
              label: node2.data[this.realLabel],
              value: node2.data[this.realValue]
            };
          }
          return {
            label: nodeValue,
            value: nodeValue
          };
        }
        const node = this.getTreeNode(this.data, nodeValue);
        if (!node) return {
          label: nodeValue,
          value: nodeValue
        };
        return node;
      });
    },
    getTreeNode(data, targetValue) {
      for (let i = 0, len = data.length; i < len; i++) {
        if (data[i][this.realValue] === targetValue) {
          return {
            label: data[i][this.realLabel],
            value: data[i][this.realValue]
          };
        }
        const childrenData = data[i][this.realChildren];
        if (childrenData) {
          const data2 = Array.isArray(childrenData) ? childrenData : this.getTreeData();
          const result = this.getTreeNode(data2, targetValue);
          if (!isNil(result)) return result;
        }
      }
      return null;
    },
    treeRerender() {
      this.treeKey += 1;
    }
  },
  render() {
    const h = arguments[0];
    const {
      treeProps,
      popupObject,
      classes,
      popupClass,
      treeKey
    } = this;
    const iconStyle = {
      "font-size": this.size
    };
    const {
      CloseCircleFilledIcon: CloseCircleFilledIcon$1
    } = this.useGlobalIcon({
      CloseCircleFilledIcon: CloseCircleFilledIcon
    });
    const treeItem = h("l-tree", {
      "ref": "tree",
      "directives": [{
        name: "show",
        value: this.showTree
      }],
      "key": treeKey,
      "attrs": {
        "value": this.checked,
        "hover": true,
        "data": this.data,
        "activable": !this.multiple,
        "checkable": this.multiple,
        "disabled": this.lDisabled || this.multiLimitDisabled,
        "empty": this.empty,
        "size": this.size,
        "filter": this.filterByText,
        "actived": this.actived,
        "expanded": this.expanded,
        "activeMultiple": this.multiple,
        "expandOnClickNode": true,
        "checkStrictly": false
      },
      "on": {
        "change": this.treeNodeChange,
        "active": this.treeNodeActive,
        "expand": this.treeNodeExpand
      },
      "props": {
        ...treeProps
      }
    }, [h("template", {
      "slot": "empty"
    }, [this.emptySlot])]);
    const searchInput = h("l-input", {
      "ref": "input",
      "directives": [{
        name: "show",
        value: this.showFilter
      }],
      "class": `${this.classPrefix}-select__input`,
      "attrs": {
        "size": this.size,
        "disabled": this.lDisabled,
        "placeholder": this.filterPlaceholder
      },
      "on": {
        "change": this.onInputChange,
        "blur": (value, context) => this.blur(context),
        "focus": (value, context) => this.focus(context)
      },
      "model": {
        value: this.filterText,
        callback: $$v => {
          this.filterText = $$v;
        }
      }
    });
    const tagItem = !isEmpty(this.tagList) && (this.valueDisplay || this.$scopedSlots.valueDisplay) ? renderLNodeJSX(this, "valueDisplay", {
      params: {
        value: this.nodeInfo,
        onClose: index => this.removeTag(index, null)
      }
    }) : this.tagList.map((label, index) => h("l-tag", {
      "directives": [{
        name: "show",
        value: this.minCollapsedNum <= 0 || index < this.minCollapsedNum
      }],
      "key": index,
      "attrs": {
        "size": this.size,
        "closable": !this.lDisabled,
        "disabled": this.lDisabled,
        "maxWidth": 300,
        "title": label
      },
      "on": {
        "close": e => this.removeTag(index, null, e)
      }
    }, [label]));
    const selectedSingle = this.valueDisplay || this.$scopedSlots.valueDisplay ? renderLNodeJSX(this, "valueDisplay", {
      params: {
        value: this.nodeInfo || {
          [this.realLabel]: "",
          [this.realValue]: ""
        }
      }
    }) : h("span", {
      "attrs": {
        "title": this.selectedSingle
      },
      "class": [`${this.classPrefix}-select__single`, `${this.classPrefix}-tree-select`, `${this.classPrefix}-single-suffix`]
    }, [this.selectedSingle]);
    const collapsedItem = (this.collapsedItems || this.$scopedSlots.collapsedItems) && this.minCollapsedNum > 0 && this.tagList.length > this.minCollapsedNum ? renderLNodeJSX(this, "collapsedItems", {
      params: {
        count: this.tagList.length - this.minCollapsedNum,
        value: this.selectedMultiple,
        collapsedSelectedItems: this.selectedMultiple.slice(this.minCollapsedNum)
      }
    }) : h("l-tag", {
      "directives": [{
        name: "show",
        value: this.minCollapsedNum > 0 && this.tagList.length > this.minCollapsedNum
      }],
      "attrs": {
        "size": this.size
      }
    }, [`+${this.tagList.length - this.minCollapsedNum}`]);
    return h("div", {
      "ref": "treeSelect",
      "class": `${this.classPrefix}-select__wrap`
    }, [h("l-popup", helper([{
      "ref": "popup",
      "class": `${this.classPrefix}-select__popup-reference`,
      "attrs": {
        "visible": this.visible,
        "disabled": this.lDisabled,
        "placement": popupObject.placement,
        "trigger": popupObject.trigger,
        "overlayInnerStyle": popupObject.overlayInnerStyle,
        "overlayClassName": popupClass
      }
    }, {
      "on": {
        "visible-change": this.popupVisibleChange
      }
    }, {
      "attrs": {
        "expandAnimation": true
      }
    }]), [h("div", {
      "class": classes,
      "on": {
        "mouseenter": () => this.isHover = true,
        "mouseleave": () => this.isHover = false
      }
    }, [this.prefixIconSlot && h("span", {
      "class": `${this.classPrefix}-select__left-icon`
    }, [this.prefixIconSlot[0]]), h("span", {
      "directives": [{
        name: "show",
        value: this.showPlaceholder
      }],
      "class": `${this.classPrefix}-select__placeholder`
    }, [this.placeholder || this.global.placeholder]), h("span", {
      "class": `${this.classPrefix}-tree-select ${this.classPrefix}-tag-prefix`
    }, [tagItem]), collapsedItem, !this.multiple && !this.showPlaceholder && !this.showFilter && selectedSingle, searchInput, this.showArrow && !this.showLoading && h("l-fake-arrow", {
      "attrs": {
        "overlayClassName": `${this.classPrefix}-select__right-icon ${this.classPrefix}-select__right-icon-polyfill`,
        "overlayStyle": iconStyle,
        "isActive": this.visible && !this.lDisabled
      }
    }), h(CloseCircleFilledIcon$1, {
      "directives": [{
        name: "show",
        value: this.showClose && !this.showLoading
      }],
      "class": [`${this.classPrefix}-select__right-icon`, `${this.classPrefix}-select__right-icon-polyfill`, `${this.classPrefix}-select__right-icon-clear`],
      "attrs": {
        "size": this.size
      },
      "nativeOn": {
        "click": this.clear
      }
    }), h("l-loading", {
      "directives": [{
        name: "show",
        value: this.showLoading
      }],
      "class": `${this.classPrefix}-select__right-icon ${this.classPrefix}-select__right-icon-polyfill ${this.classPrefix}-select__active-icon`,
      "attrs": {
        "size": "small"
      }
    })]), h("div", {
      "slot": "content",
      "class": [`${this.classPrefix}-select__dropdown-inner`, `${this.classPrefix}-select__dropdown-inner--size-${this.dropdownInnerSize}`]
    }, [h("p", {
      "directives": [{
        name: "show",
        value: this.showLoading
      }],
      "class": `${this.classPrefix}-select__loading-tips ${this.classPrefix}-select__right-icon-polyfill`
    }, [this.loadingTextSlot]), treeItem])])]);
  }
});

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