/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import _typeof from '@babel/runtime/helpers/typeof';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import get from 'lodash/get';
import { renderContent } from '../utils/render-tnode.js';
import mixins from '../utils/mixins.js';
import { scrollSelectedIntoView } from '../utils/dom.js';
import { prefix } from '../config.js';
import ClASSNAMES from '../utils/classnames.js';
import Ripple from '../utils/ripple.js';
import props from './option-props.js';
import { Checkbox } from '../checkbox/index.js';
import { getKeepAnimationMixins } from '../config-provider/config-receiver.js';
import 'vue';
import 'lodash/isObject';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';
import '../_common/js/utils/set-style.js';
import '../checkbox/group.js';
import '@babel/runtime/helpers/toConsumableArray';
import '../_chunks/dep-1ec74b8b.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 'lodash/camelCase';
import '../utils/event.js';
import '../checkbox/checkbox-group-props.js';
import '../utils/map-props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import './style/css.js';
import '../checkbox/type.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 '../config-provider/type.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 selectName = "".concat(prefix, "-select");
var _Option = mixins(keepAnimationMixins).extend({
  name: "TOption",
  data: function data() {
    return {
      isHover: false,
      formDisabled: void 0
    };
  },
  props: _objectSpread({}, props),
  components: {
    TCheckbox: Checkbox
  },
  directives: {
    ripple: Ripple
  },
  inject: {
    tSelect: {
      "default": void 0
    }
  },
  watch: {
    value: function value() {
      this.tSelect && this.tSelect.getOptions(this);
    },
    label: function label() {
      this.tSelect && this.tSelect.getOptions(this);
    },
    hovering: function hovering() {
      var _this = this;

      if (this.hovering) {
        var timer = setTimeout(function () {
          scrollSelectedIntoView(_this.tSelect.getOverlayElm(), _this.$el);
          clearTimeout(timer);
        }, this.tSelect.popupOpenTime);
      }
    }
  },
  computed: {
    tDisabled: function tDisabled() {
      return this.formDisabled || this.disabled;
    },
    hovering: function hovering() {
      return this.tSelect && this.tSelect.visible && this.tSelect.hoverOptions[this.tSelect.hoverIndex] && this.tSelect.hoverOptions[this.tSelect.hoverIndex][this.tSelect.realValue] === this.value;
    },
    multiLimitDisabled: function multiLimitDisabled() {
      if (this.tSelect && this.tSelect.multiple && this.tSelect.max) {
        if (this.tSelect.value instanceof Array && this.tSelect.value.indexOf(this.value) === -1 && this.tSelect.max <= this.tSelect.value.length) {
          return true;
        }
      }

      return false;
    },
    classes: function classes() {
      var _ref;

      return ["".concat(prefix, "-select-option"), (_ref = {}, _defineProperty(_ref, ClASSNAMES.STATUS.disabled, this.tDisabled || this.multiLimitDisabled), _defineProperty(_ref, ClASSNAMES.STATUS.selected, this.selected), _defineProperty(_ref, ClASSNAMES.SIZE[this.tSelect && this.tSelect.size], this.tSelect && this.tSelect.size), _defineProperty(_ref, "".concat(prefix, "-select-option__hover"), this.hovering), _ref)];
    },
    isCreatedOption: function isCreatedOption() {
      return this.tSelect.creatable && this.value === this.tSelect.searchInput;
    },
    show: function show() {
      var _this2 = this;

      if (!this.tSelect) return false;
      if (this.isCreatedOption) return true;

      if (this.tSelect.canFilter && this.tSelect.searchInput !== "") {
        return this.tSelect.filterOptions.some(function (option) {
          return get(option, _this2.tSelect.realValue) === _this2.value;
        });
      }

      return true;
    },
    labelText: function labelText() {
      return this.label || this.value;
    },
    selected: function selected() {
      var _this3 = this;

      var flag = false;
      if (!this.tSelect) return false;

      if (this.tSelect.value instanceof Array) {
        if (this.tSelect.labelInValue) {
          flag = this.tSelect.value.map(function (item) {
            return get(item, _this3.tSelect.realValue);
          }).indexOf(this.value) !== -1;
        } else {
          flag = this.tSelect.value.indexOf(this.value) !== -1;
        }
      } else if (_typeof(this.tSelect.value) === "object") {
        flag = get(this.tSelect.value, this.tSelect.realValue) === this.value;
      } else {
        flag = this.tSelect.value === this.value;
      }

      return flag;
    }
  },
  methods: {
    select: function select(e) {
      e.stopPropagation();

      if (this.tDisabled || this.multiLimitDisabled) {
        return false;
      }

      var parent = this.$el.parentNode;

      if (parent && parent.className.indexOf("".concat(selectName, "__create-option")) !== -1) {
        this.tSelect && this.tSelect.createOption(this.value.toString());
      }

      this.tSelect && this.tSelect.onOptionClick(this.value, e);
    },
    mouseEvent: function mouseEvent(v) {
      this.isHover = v;
    }
  },
  mounted: function mounted() {
    this.tSelect && this.tSelect.getOptions(this);
  },
  beforeDestroy: function beforeDestroy() {
    this.tSelect && this.tSelect.hasSlotOptions && this.tSelect.destroyOptions(this);
  },
  render: function render() {
    var h = arguments[0];
    var classes = this.classes,
        labelText = this.labelText,
        selected = this.selected,
        disabled = this.disabled,
        multiLimitDisabled = this.multiLimitDisabled,
        show = this.show;
    var children = renderContent(this, "default", "content");
    var optionChild = children || labelText;
    return h("li", {
      "directives": [{
        name: "show",
        value: show
      }, {
        name: "ripple",
        value: this.keepAnimation.ripple
      }],
      "class": classes,
      "attrs": {
        "title": labelText
      },
      "on": {
        "mouseenter": this.mouseEvent.bind(true),
        "mouseleave": this.mouseEvent.bind(false),
        "click": this.select
      }
    }, [this.tSelect && this.tSelect.multiple ? h("t-checkbox", {
      "attrs": {
        "checked": selected,
        "disabled": disabled || multiLimitDisabled
      },
      "nativeOn": {
        "click": function click(e) {
          e.preventDefault();
        }
      }
    }, [optionChild]) : h("span", [optionChild])]);
  }
});

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