"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _vue = _interopRequireWildcard(require("vue"));

var _classNames = _interopRequireDefault(require("../_util/classNames"));

var _raf = _interopRequireDefault(require("raf"));

var _vcTrigger = _interopRequireDefault(require("../vc-trigger"));

var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));

var _DropdownMenu = _interopRequireDefault(require("./DropdownMenu"));

var _util = require("./util");

var _BaseMixin = _interopRequireDefault(require("../_util/BaseMixin"));

var _propsUtil = require("../_util/props-util");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var BUILT_IN_PLACEMENTS = {
  bottomLeft: {
    points: ['tl', 'bl'],
    offset: [0, 4],
    overflow: {
      adjustX: 0,
      adjustY: 1
    }
  },
  topLeft: {
    points: ['bl', 'tl'],
    offset: [0, -4],
    overflow: {
      adjustX: 0,
      adjustY: 1
    }
  }
};
var _default2 = {
  name: 'SelectTrigger',
  mixins: [_BaseMixin.default],
  inheritAttrs: false,
  props: {
    // onPopupFocus: PropTypes.func,
    // onPopupScroll: PropTypes.func,
    dropdownMatchSelectWidth: _vueTypes.default.bool,
    defaultActiveFirstOption: _vueTypes.default.bool,
    dropdownAlign: _vueTypes.default.object,
    visible: _vueTypes.default.bool,
    disabled: _vueTypes.default.bool,
    showSearch: _vueTypes.default.bool,
    dropdownClassName: _vueTypes.default.string,
    dropdownStyle: _vueTypes.default.object,
    dropdownMenuStyle: _vueTypes.default.object,
    multiple: _vueTypes.default.bool,
    inputValue: _vueTypes.default.string,
    filterOption: _vueTypes.default.any,
    empty: _vueTypes.default.bool,
    options: _vueTypes.default.any,
    prefixCls: _vueTypes.default.string,
    popupClassName: _vueTypes.default.string,
    value: _vueTypes.default.array,
    // children: PropTypes.any,
    showAction: _vueTypes.default.arrayOf(_vueTypes.default.string),
    combobox: _vueTypes.default.bool,
    animation: _vueTypes.default.string,
    transitionName: _vueTypes.default.string,
    getPopupContainer: _vueTypes.default.func,
    backfillValue: _vueTypes.default.any,
    menuItemSelectedIcon: _vueTypes.default.any,
    dropdownRender: _vueTypes.default.func,
    ariaId: _vueTypes.default.string
  },
  data: function data() {
    return {
      dropdownWidth: 0
    };
  },
  created: function created() {
    this.rafInstance = null;
    this.saveDropdownMenuRef = (0, _util.saveRef)(this, 'dropdownMenuRef');
    this.saveTriggerRef = (0, _util.saveRef)(this, 'triggerRef');
  },
  mounted: function mounted() {
    var _this = this;

    this.$nextTick(function () {
      _this.setDropdownWidth();
    });
  },
  updated: function updated() {
    var _this2 = this;

    this.$nextTick(function () {
      _this2.setDropdownWidth();
    });
  },
  beforeUnmount: function beforeUnmount() {
    this.cancelRafInstance();
  },
  methods: {
    setDropdownWidth: function setDropdownWidth() {
      var _this3 = this;

      this.cancelRafInstance();
      this.rafInstance = (0, _raf.default)(function () {
        var _findDOMNode;

        var width = (_findDOMNode = (0, _propsUtil.findDOMNode)(_this3)) === null || _findDOMNode === void 0 ? void 0 : _findDOMNode.offsetWidth;

        if (width !== _this3.dropdownWidth) {
          _this3.setState({
            dropdownWidth: width
          });
        }
      });
    },
    cancelRafInstance: function cancelRafInstance() {
      if (this.rafInstance) {
        _raf.default.cancel(this.rafInstance);
      }
    },
    getInnerMenu: function getInnerMenu() {
      return this.dropdownMenuRef && this.dropdownMenuRef.menuRef;
    },
    getPopupDOMNode: function getPopupDOMNode() {
      return this.triggerRef.getPopupDomNode();
    },
    getDropdownElement: function getDropdownElement(newProps) {
      var props = _objectSpread(_objectSpread({}, this.$props), this.$attrs);

      var dropdownRender = props.dropdownRender,
          ariaId = props.ariaId;

      var menuNode = _vue.createVNode(_DropdownMenu.default, _vue.mergeProps(newProps, {
        "ref": this.saveDropdownMenuRef,
        "ariaId": ariaId,
        "prefixCls": this.getDropdownPrefixCls(),
        "onMenuSelect": props.onMenuSelect,
        "onMenuDeselect": props.onMenuDeselect,
        "onPopupScroll": props.onPopupScroll,
        "value": props.value,
        "backfillValue": props.backfillValue,
        "firstActiveValue": props.firstActiveValue,
        "defaultActiveFirstOption": props.defaultActiveFirstOption,
        "dropdownMenuStyle": props.dropdownMenuStyle,
        "menuItemSelectedIcon": props.menuItemSelectedIcon
      }), null);

      if (dropdownRender) {
        return dropdownRender({
          menuNode: menuNode,
          props: props
        });
      }

      return null;
    },
    getDropdownTransitionName: function getDropdownTransitionName() {
      var props = this.$props;
      var transitionName = props.transitionName;

      if (!transitionName && props.animation) {
        transitionName = "".concat(this.getDropdownPrefixCls(), "-").concat(props.animation);
      }

      return transitionName;
    },
    getDropdownPrefixCls: function getDropdownPrefixCls() {
      return "".concat(this.prefixCls, "-dropdown");
    }
  },
  render: function render() {
    var _popupClassName,
        _this4 = this;

    var _this$$props$this$$at = _objectSpread(_objectSpread({}, this.$props), this.$attrs),
        onPopupFocus = _this$$props$this$$at.onPopupFocus,
        empty = _this$$props$this$$at.empty,
        props = _objectWithoutProperties(_this$$props$this$$at, ["onPopupFocus", "empty"]);

    var multiple = props.multiple,
        visible = props.visible,
        inputValue = props.inputValue,
        dropdownAlign = props.dropdownAlign,
        disabled = props.disabled,
        showSearch = props.showSearch,
        dropdownClassName = props.dropdownClassName,
        dropdownStyle = props.dropdownStyle,
        dropdownMatchSelectWidth = props.dropdownMatchSelectWidth;
    var dropdownPrefixCls = this.getDropdownPrefixCls();
    var popupClassName = (_popupClassName = {}, _defineProperty(_popupClassName, dropdownClassName, !!dropdownClassName), _defineProperty(_popupClassName, "".concat(dropdownPrefixCls, "--").concat(multiple ? 'multiple' : 'single'), 1), _defineProperty(_popupClassName, "".concat(dropdownPrefixCls, "--empty"), empty), _popupClassName);
    var popupElement = this.getDropdownElement({
      menuItems: props.options,
      multiple: multiple,
      inputValue: inputValue,
      visible: visible,
      onPopupFocus: onPopupFocus
    });
    var hideAction;

    if (disabled) {
      hideAction = [];
    } else if ((0, _util.isSingleMode)(props) && !showSearch) {
      hideAction = ['click'];
    } else {
      hideAction = ['blur'];
    }

    var popupStyle = _objectSpread({}, dropdownStyle);

    var widthProp = dropdownMatchSelectWidth ? 'width' : 'minWidth';

    if (this.dropdownWidth) {
      popupStyle[widthProp] = "".concat(this.dropdownWidth, "px");
    }

    return _vue.createVNode(_vcTrigger.default, _vue.mergeProps(props, {
      "showAction": disabled ? [] : this.$props.showAction,
      "hideAction": hideAction,
      "ref": this.saveTriggerRef,
      "popupPlacement": "bottomLeft",
      "builtinPlacements": BUILT_IN_PLACEMENTS,
      "prefixCls": dropdownPrefixCls,
      "popupTransitionName": this.getDropdownTransitionName(),
      "onPopupVisibleChange": props.onDropdownVisibleChange,
      "popup": popupElement,
      "popupAlign": dropdownAlign,
      "popupVisible": visible,
      "getPopupContainer": props.getPopupContainer,
      "popupClassName": (0, _classNames.default)(popupClassName),
      "popupStyle": popupStyle
    }), {
      default: function _default() {
        return [(0, _propsUtil.getSlot)(_this4)[0]];
      }
    });
  }
};
exports.default = _default2;