"use strict";

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

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _overlay = _interopRequireDefault(require("../overlay"));

var _inner = _interopRequireDefault(require("./inner"));

var _alignMap = require("./alignMap");

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

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

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

var Popup = _overlay["default"].Popup;
/** Balloon.Tooltip */

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

  function Tooltip(props) {
    var _this;

    _classCallCheck(this, Tooltip);

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

    _defineProperty(_assertThisInitialized(_this), "getSimpleAlign", function (expectedAlign, rtl) {
      var simpleKeys = Object.keys(_alignMap.edgeMap);

      if (rtl) {
        return simpleKeys.find(function (k) {
          return _alignMap.edgeMap[k].rtlAlign === expectedAlign;
        });
      }

      return simpleKeys.find(function (k) {
        return _alignMap.edgeMap[k].align === expectedAlign;
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onPosition", function () {
      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      if (_this.state.expectedAlign !== args[0].align.join(' ')) {
        var _this$props;

        var expectedAlign = args[0].align.join(' ');

        _this.setState({
          expectedAlign: expectedAlign
        });

        _this.props.onPosition && (_this$props = _this.props).onPosition.apply(_this$props, args);
      }
    });

    var _expectedAlign = _alignMap.edgeMap[props.align][props.rlt ? 'rtlAlign' : 'align'];
    _this.state = {
      expectedAlign: _expectedAlign
    };
    return _this;
  } // 通过overylay的align可选值、alignMap、dir获取toolotip align的简写值


  _createClass(Tooltip, [{
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          id = _this$props2.id,
          type = _this$props2.type,
          className = _this$props2.className,
          align = _this$props2.align,
          style = _this$props2.style,
          prefix = _this$props2.prefix,
          trigger = _this$props2.trigger,
          children = _this$props2.children,
          popupContainer = _this$props2.popupContainer,
          popupProps = _this$props2.popupProps,
          popupClassName = _this$props2.popupClassName,
          popupStyle = _this$props2.popupStyle,
          followTrigger = _this$props2.followTrigger,
          triggerType = _this$props2.triggerType,
          autoFocus = _this$props2.autoFocus,
          rtl = _this$props2.rtl,
          others = _objectWithoutProperties(_this$props2, ["id", "type", "className", "align", "style", "prefix", "trigger", "children", "popupContainer", "popupProps", "popupClassName", "popupStyle", "followTrigger", "triggerType", "autoFocus", "rtl"]);

      var trOrigin = 'trOrigin';

      if (rtl) {
        others.rtl = true;
        trOrigin = 'rtlTrOrigin';
      } // 开启对齐方式的自适应调整


      var simpleAlign = this.getSimpleAlign(this.state.expectedAlign, rtl);
      var transformOrigin = _alignMap.edgeMap[simpleAlign][trOrigin];
      var _offset = _alignMap.edgeMap[simpleAlign].offset;

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

      var content = _react["default"].createElement(_inner["default"], _extends({}, others, {
        id: id,
        prefix: prefix,
        closable: false,
        type: type,
        isTooltip: true,
        className: className,
        style: _style,
        align: simpleAlign,
        rtl: rtl
      }), _react["default"].createElement("span", {
        className: "".concat(prefix, "balloon-tooltip-content")
      }, children));

      var triggerProps = {};
      triggerProps['aria-describedby'] = id;
      triggerProps.tabIndex = '0';
      var newTriggerType = triggerType;

      if (triggerType === 'hover' && id) {
        newTriggerType = ['focus', 'hover'];
      }

      var ariaTrigger = id ? _react["default"].cloneElement(trigger, triggerProps) : trigger;
      var newTrigger = (0, _util["default"])(_react["default"].isValidElement(ariaTrigger) ? ariaTrigger : _react["default"].createElement("span", null, ariaTrigger)); // 不能采用有transform: scale相关的动画，因node.offsetWidth获取的值不精确，
      // 是四舍五入的，而node.getBoundingClientRect()获取的值虽然精确，但是值是视觉大小的
      // 受scale影响

      return _react["default"].createElement(Popup, _extends({
        role: "tooltip",
        container: popupContainer,
        followTrigger: followTrigger,
        trigger: newTrigger,
        triggerType: newTriggerType,
        align: this.state.expectedAlign,
        offset: _offset,
        delay: 300,
        className: popupClassName,
        style: popupStyle,
        rtl: rtl,
        autoFocus: triggerType === 'focus' ? false : autoFocus,
        shouldUpdatePosition: true // needAdjust

      }, popupProps, {
        onPosition: this.onPosition,
        animation: {
          "in": 'fadeIn',
          out: 'fadeOut'
        }
      }), content);
    }
  }]);

  return Tooltip;
}(_react["default"].Component);

exports["default"] = Tooltip;

_defineProperty(Tooltip, "propTypes", {
  /**
   * 样式类名的品牌前缀
   */
  prefix: _propTypes["default"].string,

  /**
   * 自定义类名
   */
  className: _propTypes["default"].string,

  /**
   * 自定义内联样式
   */
  style: _propTypes["default"].object,

  /**
   * 类型
   */
  type: _propTypes["default"].oneOf(['normal', 'primary']),

  /**
   * tooltip的内容
   */
  children: _propTypes["default"].any,

  /**
   * 弹出层位置
   * @enumdesc 上, 右, 下, 左, 上左, 上右, 下左, 下右, 左上, 左下, 右上, 右下 及其 两两组合
   */
  align: _propTypes["default"].oneOf(['t', 'r', 'b', 'l', 'tl', 'tr', 'bl', 'br', 'lt', 'lb', 'rt', 'rb']),

  /**
   * 触发元素
   */
  trigger: _propTypes["default"].any,

  /**
   * 触发行为
   * 鼠标悬浮,  鼠标点击('hover', 'click')或者它们组成的数组，如 ['hover', 'click'], 强烈不建议使用'focus'，若有复杂交互，推荐使用triggerType为click的Balloon组件
   */
  triggerType: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].array]),

  /**
   * 弹层组件style，透传给Popup
   */
  popupStyle: _propTypes["default"].object,

  /**
   * 弹层组件className，透传给Popup
   */
  popupClassName: _propTypes["default"].string,

  /**
   * 弹层组件属性，透传给Popup
   */
  popupProps: _propTypes["default"].object,

  /**
   * 是否pure render
   */
  pure: _propTypes["default"].bool,

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

  /**
   * 是否跟随滚动
   */
  followTrigger: _propTypes["default"].bool,

  /**
   * 弹层id, 传入值才会支持无障碍
   */
  id: _propTypes["default"].string
});

_defineProperty(Tooltip, "defaultProps", {
  type: 'primary',
  triggerType: 'hover',
  prefix: 'next-',
  align: 'b',
  trigger: _react["default"].createElement("span", null)
});