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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, inject, ref, toRefs, reactive, computed, onMounted, onBeforeUnmount } from '@vue/composition-api';
import { renderContent } from '../utils/render-tnode.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 './util.js';
import props from './option-props.js';
import '@babel/runtime/helpers/typeof';
import 'vue';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../_common/js/utils/set-style.js';
import '../config.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-2cb61c79.js';
import 'dayjs';
import '../utils/map-props.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import '../checkbox/group.js';
import '@babel/runtime/helpers/toConsumableArray';
import '../_chunks/dep-b6ac6e96.js';
import 'lodash/intersection';
import '../utils/event.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/slicedToArray';
import '../utils/mixins.js';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../checkbox/checkbox-group-props.js';
import '../_common/js/utils/observe.js';
import 'lodash/cloneDeep';

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 keepAnimationMixins = getKeepAnimationMixins();
var _Option = defineComponent({
  name: "TOption",
  components: {
    TCheckbox: Checkbox
  },
  directives: {
    Ripple: Ripple
  },
  mixins: [keepAnimationMixins],
  props: _objectSpread(_objectSpread({}, props), {}, {
    isCreatedOption: Boolean,
    multiple: Boolean,
    rowIndex: Number,
    trs: Map,
    scrollType: String,
    isVirtual: Boolean,
    bufferSize: Number,
    index: Number
  }),
  setup: function setup(props2, context) {
    var selectProvider = inject("tSelect");
    var optionNode = ref(null);
    var _useCommonClassName = useCommonClassName(),
      sizeClassNames = _useCommonClassName.sizeClassNames,
      statusClassNames = _useCommonClassName.statusClassNames;
    var _useConfig = useConfig("classPrefix"),
      classPrefix = _useConfig.classPrefix;
    var _toRefs = toRefs(props2),
      value = _toRefs.value,
      label = _toRefs.label,
      multiple = _toRefs.multiple,
      disabled = _toRefs.disabled,
      panelElement = _toRefs.panelElement,
      scrollType = _toRefs.scrollType,
      bufferSize = _toRefs.bufferSize,
      index = _toRefs.index,
      isCreatedOption = _toRefs.isCreatedOption;
    var _useLazyLoad = useLazyLoad(panelElement, optionNode, reactive({
        type: scrollType,
        bufferSize: bufferSize
      })),
      _useLazyLoad$hasLazyL = _useLazyLoad.hasLazyLoadHolder,
      hasLazyLoadHolder = _useLazyLoad$hasLazyL === void 0 ? null : _useLazyLoad$hasLazyL,
      _useLazyLoad$tRowHeig = _useLazyLoad.tRowHeight,
      tRowHeight = _useLazyLoad$tRowHeig === void 0 ? null : _useLazyLoad$tRowHeig;
    var isHover = ref(false);
    var _useFormDisabled = useFormDisabled(),
      formDisabled = _useFormDisabled.formDisabled;
    var isSelected = computed(function () {
      if (multiple.value) {
        var _selectProvider$selec;
        if (props2.checkAll) return selectProvider.isAllOptionsChecked.value;
        return (_selectProvider$selec = selectProvider.selectValue.value) === null || _selectProvider$selec === void 0 ? void 0 : _selectProvider$selec.includes(props2.value);
      }
      return selectProvider.selectValue.value === props2.value;
    });
    var isIndeterminate = computed(function () {
      return multiple.value && !selectProvider.isAllOptionsChecked.value && selectProvider.selectValue.value.length > 0;
    });
    var isDisabled = computed(function () {
      return disabled.value || formDisabled.value || multiple.value && !isSelected.value && !props2.checkAll && selectProvider.isReachMaxLimit.value;
    });
    var mouseEvent = function mouseEvent(v) {
      isHover.value = v;
    };
    var labelText = computed(function () {
      return label.value || value.value;
    });
    var classes = computed(function () {
      var _ref;
      return ["".concat(classPrefix.value, "-select-option"), sizeClassNames[selectProvider && selectProvider.size.value], (_ref = {}, _defineProperty(_ref, statusClassNames.disabled, isDisabled.value), _defineProperty(_ref, statusClassNames.selected, isSelected.value), _defineProperty(_ref, "".concat(classPrefix.value, "-select-option__hover"), (isHover.value || selectProvider.hoverIndex.value === index.value) && !isDisabled.value && !isSelected.value), _ref)];
    });
    var handleClick = function handleClick(e) {
      var _selectProvider$handl;
      if (isDisabled.value) return;
      e.preventDefault();
      if (isCreatedOption.value) (_selectProvider$handl = selectProvider.handleCreate) === null || _selectProvider$handl === void 0 ? void 0 : _selectProvider$handl.call(selectProvider, value.value);
      if (multiple.value) {
        if (props2.checkAll) {
          selectProvider.handleCheckAllClick(e);
        } else {
          var newValue = getNewMultipleValue(selectProvider.selectValue.value, value.value);
          selectProvider.handleValueChange(newValue.value, {
            e: e,
            trigger: newValue.isCheck ? "check" : "uncheck"
          }, value.value);
          if (!selectProvider.reserveKeyword.value) selectProvider.handlerInputChange("");
        }
      } else {
        selectProvider.handleValueChange(value.value, {
          e: e,
          trigger: "check"
        }, value.value);
        selectProvider.handlePopupVisibleChange(false, {
          e: e
        });
      }
    };
    onMounted(function () {
      var trs = props2.trs,
        rowIndex = props2.rowIndex,
        scrollType2 = props2.scrollType,
        isVirtual = props2.isVirtual;
      if (scrollType2 === "virtual") {
        if (isVirtual) {
          trs.set(rowIndex, optionNode.value);
          context.emit("onRowMounted");
        }
      }
    });
    onBeforeUnmount(function () {
      if (props2.isVirtual) {
        var trs = props2.trs,
          rowIndex = props2.rowIndex;
        trs["delete"](rowIndex);
      }
    });
    return {
      isHover: isHover,
      isSelected: isSelected,
      isDisabled: isDisabled,
      isIndeterminate: isIndeterminate,
      mouseEvent: mouseEvent,
      classes: classes,
      selectProvider: selectProvider,
      labelText: labelText,
      optionNode: optionNode,
      tRowHeight: tRowHeight,
      hasLazyLoadHolder: hasLazyLoadHolder,
      handleClick: handleClick
    };
  },
  render: function render() {
    var h = arguments[0];
    var classes = this.classes,
      mouseEvent = this.mouseEvent,
      labelText = this.labelText,
      title = this.title;
    var optionChild = renderContent(this, "default", "content") || h("span", [labelText]);
    var titleText = title || labelText;
    if (this.hasLazyLoadHolder) {
      return h("li", {
        "ref": "optionNode",
        "class": classes,
        "on": {
          "mouseenter": function mouseenter() {
            return mouseEvent(true);
          },
          "mouseleave": function mouseleave() {
            return mouseEvent(false);
          },
          "click": this.handleClick
        },
        "directives": [{
          name: "ripple",
          value: this.keepAnimation.ripple
        }],
        "attrs": {
          "title": titleText
        }
      }, [h("span", {
        "style": {
          height: "".concat(this.tRowHeight, "px"),
          border: "none"
        }
      })]);
    }
    return h("li", {
      "ref": "optionNode",
      "class": classes,
      "on": {
        "mouseenter": function mouseenter() {
          return mouseEvent(true);
        },
        "mouseleave": function mouseleave() {
          return mouseEvent(false);
        },
        "click": this.handleClick
      },
      "directives": [{
        name: "ripple",
        value: this.keepAnimation.ripple
      }],
      "attrs": {
        "title": titleText
      }
    }, [this.multiple ? h("t-checkbox", {
      "attrs": {
        "checked": this.isSelected,
        "disabled": this.isDisabled,
        "indeterminate": this.checkAll && this.isIndeterminate
      }
    }, [optionChild]) : h("span", [optionChild])]);
  }
});

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