import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["onPopupFocus"];
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import classnames from 'classnames';
import Trigger from '../trigger';
import DropdownMenu from './DropdownMenu';
import { isSingleMode, saveRef } from './util';
import Spin from '../../spin';
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
    }
  },
  bottomRight: {
    points: ['tr', 'br'],
    offset: [0, 4],
    overflow: {
      adjustX: 0,
      adjustY: 1
    }
  }
};

var SelectTrigger = /*#__PURE__*/function (_Component) {
  _inherits(SelectTrigger, _Component);

  var _super = _createSuper(SelectTrigger);

  function SelectTrigger(_props) {
    var _this;

    _classCallCheck(this, SelectTrigger);

    _this = _super.call(this, _props);

    _defineProperty(_assertThisInitialized(_this), "setDropdownWidth", function () {
      var width = ReactDOM.findDOMNode(_assertThisInitialized(_this)).offsetWidth;

      if (width !== _this.state.dropdownWidth) {
        _this.setState({
          dropdownWidth: width
        });
      }
    });

    _defineProperty(_assertThisInitialized(_this), "getInnerMenu", function () {
      return _this.dropdownMenuRef && _this.dropdownMenuRef.menuRef;
    });

    _defineProperty(_assertThisInitialized(_this), "getFilterInput", function () {
      return _this.dropdownMenuRef && _this.dropdownMenuRef.filterRef;
    });

    _defineProperty(_assertThisInitialized(_this), "getPopupDOMNode", function () {
      return _this.triggerRef.getPopupDomNode();
    });

    _defineProperty(_assertThisInitialized(_this), "getDropdownElement", function (newProps) {
      var props = _this.props;
      var loading = props.loading;

      if (typeof loading === 'boolean') {
        loading = {
          spinning: loading
        };
      }

      return /*#__PURE__*/React.createElement(Spin, _extends({
        prefixCls: props.spinPrefixCls
      }, loading), /*#__PURE__*/React.createElement(DropdownMenu, _extends({
        ref: _this.saveDropdownMenuRef
      }, newProps, {
        prefixCls: _this.getDropdownPrefixCls(),
        onMenuSelect: props.onMenuSelect,
        onMenuDeselect: props.onMenuDeselect,
        onPopupScroll: props.onPopupScroll,
        onKeyDown: props.onKeyDown,
        value: props.value,
        placeholder: props.filterPlaceholder,
        checkAll: props.checkAll,
        backfillValue: props.backfillValue,
        firstActiveValue: props.firstActiveValue,
        defaultActiveFirstOption: props.defaultActiveFirstOption,
        dropdownMenuStyle: props.dropdownMenuStyle,
        dropdownMenuRippleDisabled: props.dropdownMenuRippleDisabled,
        onFilterChange: props.onFilterChange,
        footer: props.footer,
        onMouseDown: props.onDropdownMouseDown
      })));
    });

    _defineProperty(_assertThisInitialized(_this), "getDropdownTransitionName", function () {
      var props = _this.props;
      var transitionName = props.transitionName;

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

      return transitionName;
    });

    _defineProperty(_assertThisInitialized(_this), "getDropdownPrefixCls", function () {
      return "".concat(_this.props.prefixCls, "-dropdown");
    });

    _this.saveDropdownMenuRef = saveRef(_assertThisInitialized(_this), 'dropdownMenuRef');
    _this.saveTriggerRef = saveRef(_assertThisInitialized(_this), 'triggerRef');
    _this.state = {
      dropdownWidth: null
    };
    return _this;
  }

  _createClass(SelectTrigger, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.setDropdownWidth();
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      this.setDropdownWidth();
    }
  }, {
    key: "render",
    value: function render() {
      var _popupClassName;

      var _this$props = this.props,
          onPopupFocus = _this$props.onPopupFocus,
          props = _objectWithoutProperties(_this$props, _excluded);

      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,
          filter = props.filter,
          filterValue = props.filterValue,
          checkAll = props.checkAll,
          footer = props.footer;
      var dropdownPrefixCls = this.getDropdownPrefixCls();
      var popupClassName = (_popupClassName = {}, _defineProperty(_popupClassName, dropdownClassName, !!dropdownClassName), _defineProperty(_popupClassName, "".concat(dropdownPrefixCls, "--").concat(multiple ? 'multiple' : 'single'), 1), _popupClassName);
      var popupElement = this.getDropdownElement({
        menuItems: props.options,
        onPopupFocus: onPopupFocus,
        multiple: multiple,
        inputValue: inputValue,
        visible: visible,
        filter: filter,
        filterValue: filterValue
      });
      var hideAction;

      if (disabled) {
        hideAction = [];
      } else if (footer || multiple && checkAll || filter || isSingleMode(props) && !showSearch) {
        hideAction = ['click'];
      } else {
        hideAction = ['blur'];
      }

      var popupStyle = _objectSpread({}, dropdownStyle);

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

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

      return /*#__PURE__*/React.createElement(Trigger, _extends({}, props, {
        showAction: disabled ? [] : props.showAction,
        hideAction: hideAction,
        ref: this.saveTriggerRef,
        popupPlacement: props.popupPlacement,
        builtinPlacements: props.builtinPlacements || BUILT_IN_PLACEMENTS,
        prefixCls: dropdownPrefixCls,
        popupTransitionName: this.getDropdownTransitionName(),
        onPopupVisibleChange: props.onDropdownVisibleChange,
        popup: popupElement,
        popupAlign: dropdownAlign,
        popupVisible: visible,
        getPopupContainer: props.getPopupContainer,
        popupClassName: classnames(popupClassName),
        popupStyle: popupStyle
      }), props.children);
    }
  }]);

  return SelectTrigger;
}(Component);

_defineProperty(SelectTrigger, "defaultProps", {
  popupPlacement: 'bottomLeft',
  loading: false
});

export { SelectTrigger as default };
SelectTrigger.displayName = 'SelectTrigger';
//# sourceMappingURL=SelectTrigger.js.map
