import * as _vue from "vue";

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 { provide, inject, cloneVNode } from 'vue';
import RcDropdown from '../vc-dropdown/src/index';
import DropdownButton from './dropdown-button';
import PropTypes from '../_util/vue-types';
import { cloneElement } from '../_util/vnode';
import classNames from '../_util/classNames';
import { getOptionProps, getPropsData, getComponent, isValidElement, getSlot } from '../_util/props-util';
import getDropdownProps from './getDropdownProps';
import { ConfigConsumerProps } from '../config-provider';
import RightOutlined from '@ant-design/icons-vue/RightOutlined';
var DropdownProps = getDropdownProps();
var Dropdown = {
  name: 'ADropdown',
  inheritAttrs: false,
  props: _objectSpread(_objectSpread({}, DropdownProps), {}, {
    prefixCls: PropTypes.string,
    mouseEnterDelay: PropTypes.number.def(0.15),
    mouseLeaveDelay: PropTypes.number.def(0.1),
    placement: DropdownProps.placement.def('bottomLeft'),
    onVisibleChange: PropTypes.func
  }),
  setup: function setup() {
    return {
      configProvider: inject('configProvider', ConfigConsumerProps)
    };
  },
  created: function created() {
    provide('savePopupRef', this.savePopupRef);
  },
  methods: {
    savePopupRef: function savePopupRef(ref) {
      this.popupRef = ref;
    },
    getTransitionName: function getTransitionName() {
      var _this$$props = this.$props,
          _this$$props$placemen = _this$$props.placement,
          placement = _this$$props$placemen === void 0 ? '' : _this$$props$placemen,
          transitionName = _this$$props.transitionName;

      if (transitionName !== undefined) {
        return transitionName;
      }

      if (placement.indexOf('top') >= 0) {
        return 'slide-down';
      }

      return 'slide-up';
    },
    renderOverlay: function renderOverlay(prefixCls) {
      var overlay = getComponent(this, 'overlay');
      var overlayNode = Array.isArray(overlay) ? overlay[0] : overlay; // menu cannot be selectable in dropdown defaultly
      // menu should be focusable in dropdown defaultly

      var overlayProps = overlayNode && getPropsData(overlayNode);

      var _ref = overlayProps || {},
          _ref$selectable = _ref.selectable,
          selectable = _ref$selectable === void 0 ? false : _ref$selectable,
          _ref$focusable = _ref.focusable,
          focusable = _ref$focusable === void 0 ? true : _ref$focusable;

      var expandIcon = _vue.createVNode("span", {
        "class": "".concat(prefixCls, "-menu-submenu-arrow")
      }, [_vue.createVNode(RightOutlined, {
        "class": "".concat(prefixCls, "-menu-submenu-arrow-icon")
      }, null)]);

      var fixedModeOverlay = isValidElement(overlayNode) ? cloneVNode(overlayNode, {
        mode: 'vertical',
        selectable: selectable,
        focusable: focusable,
        expandIcon: expandIcon
      }) : overlay;
      return fixedModeOverlay;
    },
    handleVisibleChange: function handleVisibleChange(val) {
      this.$emit('update:visible', val);
      this.$emit('visibleChange', val);
    }
  },
  render: function render() {
    var _child$props;

    var props = getOptionProps(this);
    var customizePrefixCls = props.prefixCls,
        trigger = props.trigger,
        disabled = props.disabled,
        getPopupContainer = props.getPopupContainer;
    var getContextPopupContainer = this.configProvider.getPopupContainer;
    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('dropdown', customizePrefixCls);
    var child = getSlot(this)[0];
    var dropdownTrigger = cloneElement(child, {
      class: classNames(child === null || child === void 0 ? void 0 : (_child$props = child.props) === null || _child$props === void 0 ? void 0 : _child$props.class, "".concat(prefixCls, "-trigger")),
      disabled: disabled
    });
    var triggerActions = disabled ? [] : trigger;
    var alignPoint;

    if (triggerActions && triggerActions.indexOf('contextmenu') !== -1) {
      alignPoint = true;
    }

    var dropdownProps = _objectSpread(_objectSpread(_objectSpread({
      alignPoint: alignPoint
    }, props), this.$attrs), {}, {
      prefixCls: prefixCls,
      getPopupContainer: getPopupContainer || getContextPopupContainer,
      transitionName: this.getTransitionName(),
      trigger: triggerActions,
      overlay: this.renderOverlay(prefixCls),
      onVisibleChange: this.handleVisibleChange
    });

    return _vue.createVNode(RcDropdown, dropdownProps, {
      default: function _default() {
        return [dropdownTrigger];
      }
    });
  }
};
Dropdown.Button = DropdownButton;
export default Dropdown;
export { DropdownProps };