/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var VueCompositionAPI = require('@vue/composition-api');
var utils_renderTnode = require('../utils/render-tnode.js');
var utils_ripple = require('../utils/ripple.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var select_optionProps = require('./option-props.js');
var checkbox_index = require('../checkbox/index.js');
var hooks_useLazyLoad = require('../hooks/useLazyLoad.js');
var select_util = require('./util.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
var hooks_useCommonClassName = require('../hooks/useCommonClassName.js');
require('@babel/runtime/helpers/typeof');
require('vue');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../_common/js/utils/set-style.js');
require('../config.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../checkbox/group.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../_chunks/dep-d7a1e02c.js');
require('lodash/intersection');
require('../checkbox/checkbox.js');
require('../checkbox/props.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('../utils/event.js');
require('../utils/mixins.js');
require('../checkbox/checkbox-group-props.js');
require('../utils/map-props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../_common/js/utils/observe.js');
require('lodash/cloneDeep');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

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__default["default"](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 = configProvider_configReceiver.getKeepAnimationMixins();
var _Option = VueCompositionAPI.defineComponent({
  name: "TOption",
  props: _objectSpread(_objectSpread({}, select_optionProps["default"]), {}, {
    isCreatedOption: Boolean,
    multiple: Boolean,
    rowIndex: Number,
    trs: Map,
    scrollType: String,
    isVirtual: Boolean,
    bufferSize: Number,
    index: Number
  }),
  components: {
    TCheckbox: checkbox_index.Checkbox
  },
  mixins: [keepAnimationMixins],
  directives: {
    Ripple: utils_ripple["default"]
  },
  setup: function setup(props2, context) {
    var selectProvider = VueCompositionAPI.inject("tSelect");
    var optionNode = VueCompositionAPI.ref(null);

    var _useCommonClassName = hooks_useCommonClassName["default"](),
        sizeClassNames = _useCommonClassName.sizeClassNames,
        statusClassNames = _useCommonClassName.statusClassNames;

    var _useConfig = configProvider_useConfig.useConfig("classPrefix"),
        classPrefix = _useConfig.classPrefix;

    var _toRefs = VueCompositionAPI.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 = hooks_useLazyLoad["default"](panelElement, optionNode, VueCompositionAPI.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 = VueCompositionAPI.ref(false);
    var formDisabled = VueCompositionAPI.ref(void 0);
    var isDisabled = VueCompositionAPI.computed(function () {
      return formDisabled.value || disabled.value || selectProvider.isReachMaxLimit.value;
    });
    var isSelected = VueCompositionAPI.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 = VueCompositionAPI.computed(function () {
      return label.value || value.value;
    });
    var classes = VueCompositionAPI.computed(function () {
      var _ref;

      return ["".concat(classPrefix.value, "-select-option"), sizeClassNames[selectProvider && selectProvider.size.value], (_ref = {}, _defineProperty__default["default"](_ref, statusClassNames.disabled, isDisabled.value), _defineProperty__default["default"](_ref, statusClassNames.selected, isSelected.value), _defineProperty__default["default"](_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 = select_util.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 = select_util.getNewMultipleValue(selectProvider.selectValue.value, value.value);
      selectProvider.handleValueChange(newValue.value, {
        e: context2.e,
        trigger: val ? "check" : "uncheck"
      });

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

    VueCompositionAPI.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");
        }
      }
    });
    VueCompositionAPI.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 = utils_renderTnode.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])]);
  }
});

exports["default"] = _Option;
//# sourceMappingURL=option.js.map
