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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var checkbox_index = require('../checkbox/index.js');
var form_hooks = require('../form/hooks.js');
var hooks_useRipple = require('../hooks/useRipple.js');
var hooks_tnode = require('../hooks/tnode.js');
var hooks_useConfig = require('../hooks/useConfig.js');
var select_optionProps = require('./option-props.js');
var select_helper = require('./helper.js');
require('../utils/withInstall.js');
require('../checkbox/checkbox.js');
require('@babel/runtime/helpers/slicedToArray');
require('../hooks/useVModel.js');
require('lodash/kebabCase');
require('../checkbox/props.js');
require('../checkbox/constants.js');
require('../utils/set-style.js');
require('../hooks/useKeepAnimation.js');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-7f0559c3.js');
require('dayjs');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('lodash/isFunction');
require('lodash/isString');
require('lodash/camelCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('../checkbox/group.js');
require('@babel/runtime/helpers/toConsumableArray');
require('lodash/intersection');
require('lodash/isUndefined');
require('../hooks/slot.js');
require('../checkbox/checkbox-group-props.js');

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; }
function _isSlot(s) {
  return typeof s === 'function' || Object.prototype.toString.call(s) === '[object Object]' && !vue.isVNode(s);
}
var _Option = vue.defineComponent({
  name: "TOption",
  props: _objectSpread(_objectSpread({}, select_optionProps["default"]), {}, {
    createAble: Boolean,
    multiple: Boolean,
    index: Number,
    rowIndex: Number,
    trs: Map,
    scrollType: String,
    isVirtual: Boolean,
    bufferSize: Number,
    checkAll: Boolean
  }),
  emits: ["row-mounted"],
  setup: function setup(props2, context) {
    var selectProvider = vue.inject(select_helper.selectInjectKey);
    var formDisabled = form_hooks.useFormDisabled();
    var disabled = vue.computed(function () {
      return formDisabled.value || props2.multiple && selectProvider.value.max <= selectProvider.value.selectValue.length && selectProvider.value.max !== 0;
    });
    var renderContent = hooks_tnode.useContent();
    var selectName = hooks_useConfig.usePrefixClass("select");
    var _useCommonClassName = hooks_useConfig.useCommonClassName(),
      STATUS = _useCommonClassName.STATUS,
      SIZE = _useCommonClassName.SIZE;
    var liRef = vue.ref();
    var isHover = vue.ref(false);
    var isSelected = vue.computed(function () {
      if (selectProvider.value.isCheckAll && !props2.disabled) return true;
      return !props2.multiple ? selectProvider.value.selectValue === props2.value : selectProvider.value.selectValue.includes(props2.value);
    });
    var isIndeterminate = vue.computed(function () {
      if (!props2.checkAll) return false;
      return selectProvider.value.indeterminate;
    });
    var classes = vue.computed(function () {
      var _ref;
      return ["".concat(selectName.value, "-option"), [SIZE.value[selectProvider.value.size]], (_ref = {}, _defineProperty__default["default"](_ref, STATUS.value.disabled, disabled.value), _defineProperty__default["default"](_ref, STATUS.value.selected, isSelected.value), _defineProperty__default["default"](_ref, "".concat(selectName.value, "-option__hover"), (isHover.value || selectProvider.value.hoverIndex === props2.index) && !disabled.value && !isSelected.value), _ref)];
    });
    var labelText = vue.computed(function () {
      return props2.label || props2.value;
    });
    var handleClick = function handleClick(e) {
      if (props2.disabled) return;
      if (props2.multiple) {
        handleCheckboxClick(!isSelected.value, {
          e: e
        });
        return;
      }
      e.stopPropagation();
      if (props2.createAble) {
        var _selectProvider$value, _selectProvider$value2;
        (_selectProvider$value = (_selectProvider$value2 = selectProvider.value).handleCreate) === null || _selectProvider$value === void 0 ? void 0 : _selectProvider$value.call(_selectProvider$value2, props2.value);
        if (selectProvider.value.multiple) {
          selectProvider.value.selectValue.push(props2.value);
          selectProvider.value.handleValueChange(selectProvider.value.selectValue, {
            selectedOptions: selectProvider.value.getSelectedOptions(),
            trigger: "check",
            e: e
          });
          return;
        }
      }
      selectProvider.value.handleValueChange(props2.value, {
        selectedOptions: selectProvider.value.getSelectedOptions(props2.value),
        trigger: "check",
        e: e
      });
      selectProvider.value.handlePopupVisibleChange(false, {
        e: e
      });
    };
    var handleCheckboxClick = function handleCheckboxClick(val, context2) {
      if (props2.checkAll) {
        selectProvider.value.onCheckAllChange(val);
        return;
      }
      var newValue = select_helper.getNewMultipleValue(selectProvider.value.selectValue, props2.value);
      selectProvider.value.handleValueChange(newValue.value, {
        selectedOptions: selectProvider.value.getSelectedOptions(newValue.value),
        trigger: val ? "check" : "uncheck",
        e: context2.e
      });
      if (!selectProvider.value.reserveKeyword) selectProvider.value.handlerInputChange("");
    };
    vue.onMounted(function () {
      var trs = props2.trs,
        rowIndex = props2.rowIndex,
        isVirtual = props2.isVirtual;
      if (isVirtual) {
        trs.set(rowIndex, liRef.value);
        context.emit("row-mounted");
      }
    });
    vue.onBeforeUnmount(function () {
      if (props2.isVirtual) {
        var trs = props2.trs,
          rowIndex = props2.rowIndex;
        trs["delete"](rowIndex);
      }
    });
    hooks_useRipple["default"](liRef);
    return function () {
      var optionChild = renderContent("default", "content") || labelText.value;
      return vue.createVNode("li", {
        "ref": liRef,
        "class": classes.value,
        "title": props2.title || "".concat(labelText.value),
        "onMouseenter": function onMouseenter() {
          return isHover.value = true;
        },
        "onMouseleave": function onMouseleave() {
          return isHover.value = false;
        },
        "onClick": handleClick
      }, [selectProvider && props2.multiple ? vue.createVNode(checkbox_index.Checkbox, {
        "checked": isSelected.value,
        "disabled": disabled.value && !isSelected.value,
        "onChange": handleCheckboxClick,
        "indeterminate": isIndeterminate.value
      }, _isSlot(optionChild) ? optionChild : {
        "default": function _default() {
          return [optionChild];
        }
      }) : vue.createVNode("span", null, [optionChild])]);
    };
  }
});

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