function _typeof(obj) { 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); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

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 React from 'react';
import PropTypes from 'prop-types';
import Overlay from '../overlay';
import { func, obj, log } from '../util';
import BalloonInner from './inner';
import { normalMap, edgeMap } from './alignMap';
import getDisabledCompatibleTrigger from './util';
var noop = func.noop;
var Popup = Overlay.Popup;
var alignList = ['t', 'r', 'b', 'l', 'tl', 'tr', 'bl', 'br', 'lt', 'lb', 'rt', 'rb'];
var alignMap = normalMap;
/** Balloon */

var Balloon =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Balloon, _React$Component);

  function Balloon(props, context) {
    var _this;

    _classCallCheck(this, Balloon);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Balloon).call(this, props, context));

    _defineProperty(_assertThisInitialized(_this), "_onVisibleChange", function (visible, trigger) {
      // Not Controlled
      if (!('visible' in _this.props)) {
        _this.setState({
          visible: visible
        });
      }

      _this.props.onVisibleChange(visible, trigger);

      if (!visible) {
        _this.props.onClose();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "_onClose", function (e) {
      _this._onVisibleChange(false, 'closeClick'); // 必须加上preventDefault,否则单测IE下报错,出现full page reload 异常


      e.preventDefault();
    });

    _defineProperty(_assertThisInitialized(_this), "_onPosition", function (res) {
      var rtl = _this.props.rtl;
      alignMap = _this.props.alignEdge ? edgeMap : normalMap;
      var newAlign = res.align.join(' ');
      var resAlign;
      var alignKey = 'align';

      if (rtl) {
        alignKey = 'rtlAlign';
      }

      for (var key in alignMap) {
        if (alignMap[key][alignKey] === newAlign) {
          resAlign = key;
          break;
        }
      }

      resAlign = resAlign || _this.state.align;

      if (resAlign !== _this.state.align) {
        _this.setState({
          align: resAlign
        });
      }
    });

    _this.state = {
      align: alignList.includes(props.align) ? props.align : 'b',
      visible: 'visible' in props ? props.visible : props.defaultVisible
    };
    return _this;
  }

  _createClass(Balloon, [{
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if ('visible' in nextProps) {
        this.setState({
          visible: nextProps.visible
        });
      }

      if ('align' in nextProps && alignList.includes(nextProps.align)) {
        this.setState({
          align: nextProps.align
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          id = _this$props.id,
          type = _this$props.type,
          prefix = _this$props.prefix,
          className = _this$props.className,
          alignEdge = _this$props.alignEdge,
          trigger = _this$props.trigger,
          triggerType = _this$props.triggerType,
          children = _this$props.children,
          closable = _this$props.closable,
          shouldUpdatePosition = _this$props.shouldUpdatePosition,
          delay = _this$props.delay,
          needAdjust = _this$props.needAdjust,
          safeId = _this$props.safeId,
          autoFocus = _this$props.autoFocus,
          safeNode = _this$props.safeNode,
          onClick = _this$props.onClick,
          onHover = _this$props.onHover,
          animation = _this$props.animation,
          offset = _this$props.offset,
          style = _this$props.style,
          container = _this$props.container,
          popupContainer = _this$props.popupContainer,
          cache = _this$props.cache,
          popupStyle = _this$props.popupStyle,
          popupClassName = _this$props.popupClassName,
          popupProps = _this$props.popupProps,
          followTrigger = _this$props.followTrigger,
          rtl = _this$props.rtl,
          others = _objectWithoutProperties(_this$props, ["id", "type", "prefix", "className", "alignEdge", "trigger", "triggerType", "children", "closable", "shouldUpdatePosition", "delay", "needAdjust", "safeId", "autoFocus", "safeNode", "onClick", "onHover", "animation", "offset", "style", "container", "popupContainer", "cache", "popupStyle", "popupClassName", "popupProps", "followTrigger", "rtl"]);

      if (container) {
        log.deprecated('container', 'popupContainer', 'Balloon');
      }

      var align = this.state.align;
      alignMap = alignEdge ? edgeMap : normalMap;

      var _prefix = this.context.prefix || prefix;

      var trOrigin = 'trOrigin';

      if (rtl) {
        trOrigin = 'rtlTrOrigin';
      }

      var _offset = [alignMap[align].offset[0] + offset[0], alignMap[align].offset[1] + offset[1]];
      var transformOrigin = alignMap[align][trOrigin];

      var _style = _objectSpread({}, {
        transformOrigin: transformOrigin
      }, {}, style);

      var content = React.createElement(BalloonInner, _extends({}, obj.pickOthers(Object.keys(Balloon.propTypes), others), {
        id: id,
        prefix: _prefix,
        closable: closable,
        onClose: this._onClose,
        className: className,
        style: _style,
        align: align,
        type: type,
        rtl: rtl,
        alignEdge: alignEdge
      }), children);
      var triggerProps = {};
      triggerProps['aria-describedby'] = id;
      triggerProps.tabIndex = '0';
      var ariaTrigger = id ? React.cloneElement(trigger, triggerProps) : trigger;
      var newTrigger = getDisabledCompatibleTrigger(React.isValidElement(ariaTrigger) ? ariaTrigger : React.createElement("span", null, ariaTrigger));
      return React.createElement(Popup, _extends({}, popupProps, {
        followTrigger: followTrigger,
        trigger: newTrigger,
        cache: cache,
        safeId: safeId,
        triggerType: triggerType,
        align: alignMap[align].align,
        offset: _offset,
        visible: this.state.visible,
        onPosition: this._onPosition,
        onClick: onClick,
        onHover: onHover,
        afterClose: this.props.afterClose,
        onVisibleChange: this._onVisibleChange,
        shouldUpdatePosition: shouldUpdatePosition,
        needAdjust: needAdjust,
        animation: animation,
        delay: delay,
        autoFocus: triggerType === 'focus' ? false : autoFocus,
        safeNode: safeNode,
        container: popupContainer || container,
        className: popupClassName,
        style: popupStyle,
        rtl: rtl
      }), content);
    }
  }]);

  return Balloon;
}(React.Component);

_defineProperty(Balloon, "displayName", 'Balloon');

_defineProperty(Balloon, "contextTypes", {
  prefix: PropTypes.string
});

_defineProperty(Balloon, "propTypes", {
  prefix: PropTypes.string,
  pure: PropTypes.bool,
  rtl: PropTypes.bool,

  /**
   * 自定义类名
   */
  className: PropTypes.string,

  /**
   * 自定义内敛样式
   */
  style: PropTypes.object,

  /**
   * 浮层的内容
   */
  children: PropTypes.any,
  size: PropTypes.string,

  /**
   * 样式类型
   */
  type: PropTypes.oneOf(['normal', 'primary']),

  /**
   * 弹层当前显示的状态
   */
  visible: PropTypes.bool,

  /**
   * 弹层默认显示的状态
   */
  defaultVisible: PropTypes.bool,

  /**
   * 弹层在显示和隐藏触发的事件
   * @param {Boolean} visible 弹层是否隐藏和显示
   * @param {String} type 触发弹层显示或隐藏的来源， closeClick 表示由自带的关闭按钮触发； fromTrigger 表示由trigger的点击触发； docClick 表示由document的点击触发
   */
  onVisibleChange: PropTypes.func,

  /**
   * 弹出层对齐方式
   */
  alignEdge: PropTypes.bool,

  /**
   * 是否显示关闭按钮
   */
  closable: PropTypes.bool,

  /**
   * 弹出层位置
   * @enumdesc 上, 右, 下, 左, 上左, 上右, 下左, 下右, 左上, 左下, 右上, 右下 及其 两两组合
   */
  align: PropTypes.oneOf(alignList),

  /**
   * 弹层相对于trigger的定位的微调, 接收数组[hoz, ver], 表示弹层在 left / top 上的增量
   * e.g. [100, 100] 表示往右(RTL 模式下是往左) 、下分布偏移100px
   */
  offset: PropTypes.array,

  /**
   * 触发元素
   */
  trigger: PropTypes.any,

  /**
   * 触发行为
   * 鼠标悬浮, 鼠标点击('hover','click')或者它们组成的数组，如 ['hover', 'click'], 强烈不建议使用'focus'，若弹窗内容有复杂交互请使用click
   */
  triggerType: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
  onClick: PropTypes.func,

  /**
   * 任何visible为false时会触发的事件
   */
  onClose: PropTypes.func,
  onHover: PropTypes.func,

  /**
   * 是否进行自动位置调整
   */
  needAdjust: PropTypes.bool,

  /**
   * 弹层在触发以后的延时显示, 单位毫秒 ms
   */
  delay: PropTypes.number,

  /**
   * 浮层关闭后触发的事件, 如果有动画，则在动画结束后触发
   */
  afterClose: PropTypes.func,

  /**
   * 强制更新定位信息
   */
  shouldUpdatePosition: PropTypes.bool,

  /**
   * 弹层出现后是否自动focus到内部第一个元素
   */
  autoFocus: PropTypes.bool,

  /**
   * 安全节点:对于triggetType为click的浮层,会在点击除了浮层外的其它区域时关闭浮层.safeNode用于添加不触发关闭的节点, 值可以是dom节点的id或者是节点的dom对象
   */
  safeNode: PropTypes.string,

  /**
   * 用来指定safeNode节点的id，和safeNode配合使用
   */
  safeId: PropTypes.string,

  /**
   * 配置动画的播放方式
   * @param {String} in 进场动画
   * @param {String} out 出场动画
   */
  animation: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),

  /**
   * 弹层的dom节点关闭时是否删除
   */
  cache: PropTypes.bool,

  /**
   * 指定浮层渲染的父节点, 可以为节点id的字符串，也可以返回节点的函数。
   */
  popupContainer: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
  container: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),

  /**
   * 弹层组件style，透传给Popup
   */
  popupStyle: PropTypes.object,

  /**
   * 弹层组件className，透传给Popup
   */
  popupClassName: PropTypes.string,

  /**
   * 弹层组件属性，透传给Popup
   */
  popupProps: PropTypes.object,

  /**
   * 是否跟随滚动
   */
  followTrigger: PropTypes.bool,

  /**
   * 弹层id, 传入值才会支持无障碍
   */
  id: PropTypes.string
});

_defineProperty(Balloon, "defaultProps", {
  prefix: 'next-',
  pure: false,
  type: 'normal',
  closable: true,
  defaultVisible: false,
  size: 'medium',
  alignEdge: true,
  align: 'b',
  offset: [0, 0],
  trigger: React.createElement("span", null),
  onClose: noop,
  afterClose: noop,
  onVisibleChange: noop,
  needAdjust: true,
  triggerType: 'hover',
  safeNode: undefined,
  safeId: null,
  autoFocus: true,
  animation: {
    "in": 'zoomIn',
    out: 'zoomOut'
  },
  cache: false,
  popupStyle: {},
  popupClassName: '',
  popupProps: {}
});

export { Balloon as default };