/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 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/use-lazy-load.js';
import { useConfig } from '../config-provider/useConfig.js';
import useCommonClassName from '../../hooks/use-common-classname.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 '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../../utils/map-props.js';
import '../../utils/with-install.js';
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-2e6815bd.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 = ref(void 0);
    const isDisabled = computed(() => formDisabled.value || disabled.value || selectProvider.isReachMaxLimit.value);
    const isSelected = computed(() => {
      var _selectProvider$selec;
      return multiple.value ? (_selectProvider$selec = selectProvider.selectValue.value) === null || _selectProvider$selec === void 0 ? void 0 : _selectProvider$selec.includes(props2.value) : selectProvider.selectValue.value === props2.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 (multiple.value || isDisabled.value) return;
      e.stopPropagation();
      if (isCreatedOption.value) {
        var _selectProvider$handl;
        (_selectProvider$handl = selectProvider.handleCreate) === null || _selectProvider$handl === void 0 ? void 0 : _selectProvider$handl.call(selectProvider, value.value);
        if (selectProvider.multiple.value) {
          const newValue = getNewMultipleValue(selectProvider.selectValue.value, value.value);
          selectProvider.handleValueChange(newValue.value, {
            e,
            trigger: "check"
          }, value.value);
          return;
        }
      }
      selectProvider.handleValueChange(value.value, {
        e,
        trigger: "check"
      }, value.value);
      selectProvider.handlePopupVisibleChange(false, {
        e
      });
    };
    const handleCheckboxClick = (val, context2) => {
      const newValue = getNewMultipleValue(selectProvider.selectValue.value, value.value);
      selectProvider.handleValueChange(newValue.value, {
        e: context2.e,
        trigger: val ? "check" : "uncheck"
      }, value.value);
      if (!selectProvider.reserveKeyword.value) selectProvider.handlerInputChange("");
    };
    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,
      mouseEvent,
      classes,
      selectProvider,
      labelText,
      optionNode,
      tRowHeight,
      hasLazyLoadHolder,
      handleClick,
      handleCheckboxClick
    };
  },
  render() {
    const h = arguments[0];
    const {
      classes,
      multiple,
      labelText,
      isSelected,
      disabled,
      selectProvider,
      handleCheckboxClick,
      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": disabled || !isSelected && selectProvider.isReachMaxLimit.value
      },
      "on": {
        "change": handleCheckboxClick
      }
    }, [optionChild]) : h("span", [optionChild])]);
  }
});

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