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

'use strict';

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

var _typeof = require('@babel/runtime/helpers/typeof');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var get = require('lodash/get');
var utils_renderTnode = require('../utils/render-tnode.js');
var utils_mixins = require('../utils/mixins.js');
var utils_dom = require('../utils/dom.js');
var config = require('../config.js');
var utils_classnames = require('../utils/classnames.js');
var utils_ripple = require('../utils/ripple.js');
var select_optionProps = require('./option-props.js');
var checkbox_index = require('../checkbox/index.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
require('vue');
require('lodash/isObject');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('../_common/js/utils/set-style.js');
require('../checkbox/group.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../_chunks/dep-33ee4bce.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('lodash/camelCase');
require('../utils/event.js');
require('../checkbox/checkbox-group-props.js');
require('../utils/map-props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
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');

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

var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var get__default = /*#__PURE__*/_interopDefaultLegacy(get);

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 selectName = "".concat(config.prefix, "-select");
var _Option = utils_mixins["default"](keepAnimationMixins).extend({
  name: "TOption",
  data: function data() {
    return {
      isHover: false,
      formDisabled: void 0
    };
  },
  props: _objectSpread({}, select_optionProps["default"]),
  components: {
    TCheckbox: checkbox_index.Checkbox
  },
  directives: {
    ripple: utils_ripple["default"]
  },
  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 () {
          utils_dom.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(config.prefix, "-select-option"), (_ref = {}, _defineProperty__default["default"](_ref, utils_classnames["default"].STATUS.disabled, this.tDisabled || this.multiLimitDisabled), _defineProperty__default["default"](_ref, utils_classnames["default"].STATUS.selected, this.selected), _defineProperty__default["default"](_ref, utils_classnames["default"].SIZE[this.tSelect && this.tSelect.size], this.tSelect && this.tSelect.size), _defineProperty__default["default"](_ref, "".concat(config.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__default["default"](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__default["default"](item, _this3.tSelect.realValue);
          }).indexOf(this.value) !== -1;
        } else {
          flag = this.tSelect.value.indexOf(this.value) !== -1;
        }
      } else if (_typeof__default["default"](this.tSelect.value) === "object") {
        flag = get__default["default"](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 = utils_renderTnode.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])]);
  }
});

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