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

import { defineComponent, inject, ref, toRefs, reactive, computed, onMounted, onBeforeUnmount } from '@vue/composition-api';
import { renderContent } from '../../utils/render-node.js';
import Ripple from '../../utils/ripple.js';
import { getKeepAnimationMixins } from '../config-provider/config-receiver.js';
import { Checkbox } from '../checkbox/index.js';
import useLazyLoad from '../../hooks/useLazyLoad.js';
import { useConfig } from '../config-provider/useConfig.js';
import useCommonClassName from '../../hooks/useCommonClassName.js';
import useFormDisabled from '../../hooks/useFormDisabled.js';
import { getNewMultipleValue } from './utils.js';
import { SelectOptionProps } from './props.js';
import 'vue';
import 'lodash';
import '../../chunks/set-style.js';
import '../../config.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/map-props.js';
import '../../utils/withInstall.js';
import 'lodash/capitalize';
import '../checkbox/group.js';
import '../../chunks/helper.js';
import '../../chunks/_commonjsHelpers.js';
import 'lodash/intersection';
import '../../chunks/index.js';
import '../../chunks/log.js';
import '../../chunks/_commonjsHelpers-fe796db6.js';
import '../../utils/event.js';
import '../../utils/helper.js';
import 'lodash/camelCase';
import '../../utils/mixins.js';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../checkbox/style';
import '../checkbox/type.js';
import '../../chunks/observe.js';
import 'lodash/cloneDeep';

const keepAnimationMixins = getKeepAnimationMixins();
var _Option = defineComponent({
  name: "LOption",
  components: {
    TCheckbox: Checkbox
  },
  directives: {
    Ripple
  },
  mixins: [keepAnimationMixins],
  props: {
    ...SelectOptionProps,
    isCreatedOption: Boolean,
    multiple: Boolean,
    rowIndex: Number,
    trs: Map,
    scrollType: String,
    isVirtual: Boolean,
    bufferSize: Number,
    index: Number
  },
  setup(props2, context) {
    const selectProvider = inject("tSelect");
    const optionNode = ref(null);
    const {
      sizeClassNames,
      statusClassNames
    } = useCommonClassName();
    const {
      classPrefix
    } = useConfig("classPrefix");
    const {
      value,
      label,
      disabled,
      panelElement,
      scrollType,
      bufferSize,
      index,
      multiple,
      isCreatedOption
    } = toRefs(props2);
    const {
      hasLazyLoadHolder = null,
      tRowHeight = null
    } = useLazyLoad(panelElement, optionNode, reactive({
      type: scrollType,
      bufferSize,
      rowIndex: props2.rowIndex
    }));
    const isHover = ref(false);
    const {
      formDisabled
    } = useFormDisabled();
    const isSelected = computed(() => multiple.value ? selectProvider.selectValue.value?.includes(props2.value) : selectProvider.selectValue.value === props2.value);
    const isDisabled = computed(() => disabled.value || formDisabled.value || multiple.value && !isSelected.value && selectProvider.isReachMaxLimit.value);
    const mouseEvent = v => {
      isHover.value = v;
    };
    const labelText = computed(() => label.value || value.value);
    const classes = computed(() => [`${classPrefix.value}-select-option`, sizeClassNames[selectProvider && selectProvider.size.value], {
      [statusClassNames.disabled]: isDisabled.value,
      [statusClassNames.selected]: isSelected.value,
      [`${classPrefix.value}-select-option__hover`]: (isHover.value || selectProvider.hoverIndex.value === index.value) && !isDisabled.value && !isSelected.value
    }]);
    const handleClick = e => {
      if (isDisabled.value) return;
      e.preventDefault();
      if (isCreatedOption.value) selectProvider.handleCreate?.(value.value);
      if (multiple.value) {
        const newValue = getNewMultipleValue(selectProvider.selectValue.value, value.value);
        selectProvider.handleValueChange(newValue.value, {
          e,
          trigger: newValue.isCheck ? "check" : "uncheck"
        }, value.value);
        if (!selectProvider.reserveKeyword.value) selectProvider.handlerInputChange("");
      } else {
        selectProvider.handleValueChange(value.value, {
          e,
          trigger: "check"
        }, value.value);
        selectProvider.handlePopupVisibleChange(false, {
          e
        });
      }
    };
    onMounted(() => {
      const {
        trs,
        rowIndex,
        scrollType: scrollType2,
        isVirtual
      } = props2;
      if (scrollType2 === "virtual") {
        if (isVirtual) {
          trs.set(rowIndex, optionNode.value);
          context.emit("onRowMounted");
        }
      }
    });
    onBeforeUnmount(() => {
      if (props2.isVirtual) {
        const {
          trs,
          rowIndex
        } = props2;
        trs.delete(rowIndex);
      }
    });
    return {
      isHover,
      isSelected,
      isDisabled,
      mouseEvent,
      classes,
      selectProvider,
      labelText,
      optionNode,
      tRowHeight,
      hasLazyLoadHolder,
      handleClick
    };
  },
  render() {
    const h = arguments[0];
    const {
      classes,
      multiple,
      labelText,
      isSelected,
      isDisabled,
      mouseEvent
    } = this;
    const children = renderContent(this, "default", "content");
    const optionChild = children || h("span", [labelText]);
    if (this.hasLazyLoadHolder) {
      return h("li", {
        "ref": "optionNode",
        "class": classes,
        "on": {
          "mouseenter": () => mouseEvent(true),
          "mouseleave": () => mouseEvent(false),
          "click": this.handleClick
        },
        "directives": [{
          name: "ripple",
          value: this.keepAnimation.ripple
        }]
      }, [h("span", {
        "style": {
          height: `${this.tRowHeight}px`,
          border: "none"
        }
      })]);
    }
    return h("li", {
      "ref": "optionNode",
      "class": classes,
      "on": {
        "mouseenter": () => mouseEvent(true),
        "mouseleave": () => mouseEvent(false),
        "click": this.handleClick
      },
      "directives": [{
        name: "ripple",
        value: this.keepAnimation.ripple
      }]
    }, [multiple ? h("t-checkbox", {
      "attrs": {
        "checked": isSelected,
        "disabled": isDisabled
      }
    }, [optionChild]) : h("span", [optionChild])]);
  }
});

export { _Option as default };
//# sourceMappingURL=option.js.map
