import * as _vue from "vue";

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; }

import classnames from '../_util/classNames';
import raf from 'raf';
import Trigger from '../vc-trigger';
import PropTypes from '../_util/vue-types';
import DropdownMenu from './DropdownMenu';
import { isSingleMode, saveRef } from './util';
import BaseMixin from '../_util/BaseMixin';
import { findDOMNode, getSlot } from '../_util/props-util';
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
    }
  }
};
export default {
  name: 'SelectTrigger',
  mixins: [BaseMixin],
  inheritAttrs: false,
  props: {
    // onPopupFocus: PropTypes.func,
    // onPopupScroll: PropTypes.func,
    dropdownMatchSelectWidth: PropTypes.bool,
    defaultActiveFirstOption: PropTypes.bool,
    dropdownAlign: PropTypes.object,
    visible: PropTypes.bool,
    disabled: PropTypes.bool,
    showSearch: PropTypes.bool,
    dropdownClassName: PropTypes.string,
    dropdownStyle: PropTypes.object,
    dropdownMenuStyle: PropTypes.object,
    multiple: PropTypes.bool,
    inputValue: PropTypes.string,
    filterOption: PropTypes.any,
    empty: PropTypes.bool,
    options: PropTypes.any,
    prefixCls: PropTypes.string,
    popupClassName: PropTypes.string,
    value: PropTypes.array,
    // children: PropTypes.any,
    showAction: PropTypes.arrayOf(PropTypes.string),
    combobox: PropTypes.bool,
    animation: PropTypes.string,
    transitionName: PropTypes.string,
    getPopupContainer: PropTypes.func,
    backfillValue: PropTypes.any,
    menuItemSelectedIcon: PropTypes.any,
    dropdownRender: PropTypes.func,
    ariaId: PropTypes.string
  },
  data: function data() {
    return {
      dropdownWidth: 0
    };
  },
  created: function created() {
    this.rafInstance = null;
    this.saveDropdownMenuRef = saveRef(this, 'dropdownMenuRef');
    this.saveTriggerRef = 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 = raf(function () {
        var _findDOMNode;

        var width = (_findDOMNode = 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.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, _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 (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(Trigger, _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": classnames(popupClassName),
      "popupStyle": popupStyle
    }), {
      default: function _default() {
        return [getSlot(_this4)[0]];
      }
    });
  }
};