/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @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-node.js';
import Ripple from '../../utils/ripple.js';
import { getKeepAnimationMixins } from '../config-provider/config-receiver.js';
import { SelectOptionProps } from './props.js';
import { Checkbox } from '../checkbox/index.js';
import useLazyLoad from '../../hooks/use-lazy-load.js';
import { getNewMultipleValue } from './utils.js';
import { useConfig } from '../config-provider/useConfig.js';
import useCommonClassName from '../../hooks/use-common-classname.js';
import '@babel/runtime/helpers/typeof';
import 'vue';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../chunks/set-style.js';
import '../../config.js';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../checkbox/group.js';
import '@babel/runtime/helpers/toConsumableArray';
import '../../chunks/helper.js';
import '../../chunks/_commonjsHelpers.js';
import 'lodash/intersection';
import '../checkbox/checkbox.js';
import '../checkbox/props.js';
import '../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import '../../utils/event.js';
import '../../utils/mixins.js';
import '../../utils/map-props.js';
import '../../utils/with-install.js';
import '../checkbox/style';
import '../checkbox/type.js';
import '../../chunks/observe.js';

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: "LOption",
  props: _objectSpread(_objectSpread({}, SelectOptionProps), {}, {
    isCreatedOption: Boolean,
    multiple: Boolean,
    rowIndex: Number,
    trs: Map,
    scrollType: String,
    isVirtual: Boolean,
    bufferSize: Number,
    index: Number
  }),
  components: {
    TCheckbox: Checkbox
  },
  mixins: [keepAnimationMixins],
  directives: {
    Ripple: Ripple
  },
  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,
        disabled = _toRefs.disabled,
        panelElement = _toRefs.panelElement,
        scrollType = _toRefs.scrollType,
        bufferSize = _toRefs.bufferSize,
        index = _toRefs.index,
        multiple = _toRefs.multiple,
        isCreatedOption = _toRefs.isCreatedOption;

    var _useLazyLoad = useLazyLoad(panelElement, optionNode, reactive({
      type: scrollType,
      bufferSize: bufferSize,
      rowIndex: props2.rowIndex
    })),
        _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 formDisabled = ref(void 0);
    var isDisabled = computed(function () {
      return formDisabled.value || disabled.value || selectProvider.isReachMaxLimit.value;
    });
    var isSelected = computed(function () {
      return multiple.value ? selectProvider.selectValue.value.includes(props2.value) : selectProvider.selectValue.value === props2.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) {
      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) {
          var newValue = getNewMultipleValue(selectProvider.selectValue.value, value.value);
          selectProvider.handleValueChange(newValue.value, {
            e: e,
            trigger: "check"
          });
          return;
        }
      }

      selectProvider.handleValueChange(value.value, {
        e: e,
        trigger: "check"
      });
      selectProvider.handlePopupVisibleChange(false, {
        e: e
      });
    };

    var handleCheckboxClick = function handleCheckboxClick(val, context2) {
      var newValue = getNewMultipleValue(selectProvider.selectValue.value, value.value);
      selectProvider.handleValueChange(newValue.value, {
        e: context2.e,
        trigger: val ? "check" : "uncheck"
      });

      if (!selectProvider.reserveKeyword.value) {
        selectProvider.handlerInputChange("");
      }
    };

    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,
      mouseEvent: mouseEvent,
      classes: classes,
      selectProvider: selectProvider,
      labelText: labelText,
      optionNode: optionNode,
      tRowHeight: tRowHeight,
      hasLazyLoadHolder: hasLazyLoadHolder,
      handleClick: handleClick,
      handleCheckboxClick: handleCheckboxClick
    };
  },
  render: function render() {
    var h = arguments[0];
    var classes = this.classes,
        multiple = this.multiple,
        labelText = this.labelText,
        isSelected = this.isSelected,
        disabled = this.disabled,
        selectProvider = this.selectProvider,
        handleCheckboxClick = this.handleCheckboxClick,
        mouseEvent = this.mouseEvent;
    var children = renderContent(this, "default", "content");
    var optionChild = children || h("span", [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
        }]
      }, [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
      }]
    }, [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
