"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

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

var _classnames = _interopRequireDefault(require("classnames"));

var _components = require("../components");

var _context = require("../config-provider/context");

var _getTooltipPlacements = _interopRequireDefault(require("./getTooltipPlacements"));

var whiteColor = '#ffffff';
var contentColor = '#262626';
var blackColor = 'rgba(0, 0, 0, 0.75)';

var Tooltip = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var prefixCls = props.prefixCls,
      title = props.title,
      overlayInnerStyle = props.overlayInnerStyle,
      color = props.color,
      placement = props.placement,
      children = props.children,
      trigger = props.trigger,
      visible = props.visible,
      overlay = props.overlay,
      defaultVisible = props.defaultVisible,
      arrowPointAtCenter = props.arrowPointAtCenter,
      onVisibleChange = props.onVisibleChange,
      _props$monitorWindowR = props.monitorWindowResize,
      monitorWindowResize = _props$monitorWindowR === void 0 ? true : _props$monitorWindowR,
      tooltipGetPopupContainer = props.getPopupContainer,
      reset = (0, _objectWithoutProperties2.default)(props, ["prefixCls", "title", "overlayInnerStyle", "color", "placement", "children", "trigger", "visible", "overlay", "defaultVisible", "arrowPointAtCenter", "onVisibleChange", "monitorWindowResize", "getPopupContainer"]);
  var defaultTooltipBgColor = 'white' in props ? whiteColor : blackColor;

  var _React$useState = _react.default.useState(visible === undefined ? defaultVisible : visible),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      tipVisible = _React$useState2[0],
      setTipVisible = _React$useState2[1];

  var _React$useContext = _react.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName,
      getPopupContainer = _React$useContext.getPopupContainer;

  var tooltipPrefix = getPrefixClassName('tooltip', prefixCls);
  var transitionName = "".concat(tooltipPrefix, "-zoom");
  var bgColor = color || defaultTooltipBgColor;

  if (visible !== undefined && visible !== tipVisible) {
    setTipVisible(visible);
  }

  var rcOverlayInnerStyle = (0, _objectSpread2.default)({
    background: bgColor
  }, overlayInnerStyle);

  if ('white' in props) {
    rcOverlayInnerStyle.color = contentColor;
  }

  var arrowContent = /*#__PURE__*/_react.default.createElement("span", {
    style: {
      borderTopColor: bgColor,
      color: bgColor
    },
    className: "".concat(tooltipPrefix, "-arrow-content")
  });

  var handleVisibleChange = function handleVisibleChange(status) {
    if (!('visible' in props)) {
      setTipVisible(status);
    }

    onVisibleChange === null || onVisibleChange === void 0 ? void 0 : onVisibleChange(status);
  };

  return /*#__PURE__*/_react.default.createElement(_components.RcTooltip, Object.assign({
    ref: ref,
    visible: tipVisible,
    prefixCls: tooltipPrefix,
    placement: placement,
    trigger: trigger,
    transitionName: transitionName,
    builtinPlacements: (0, _getTooltipPlacements.default)(arrowPointAtCenter),
    overlayInnerStyle: rcOverlayInnerStyle,
    overlay: overlay || title,
    arrowContent: arrowContent,
    onVisibleChange: handleVisibleChange,
    monitorWindowResize: monitorWindowResize,
    getTooltipContainer: tooltipGetPopupContainer || getPopupContainer
  }, reset), parcelDisabledChild( /*#__PURE__*/_react.default.isValidElement(children) ? children : /*#__PURE__*/_react.default.createElement("span", null, children), tooltipPrefix));
});

Tooltip.defaultProps = {
  mouseEnterDelay: 0.1,
  mouseLeaveDelay: 0.1,
  placement: 'top',
  trigger: ['hover'],
  arrowPointAtCenter: false
};
var _default = Tooltip;
exports.default = _default;

var splitObject = function splitObject(obj, keys) {
  var picked = {};
  var omitted = (0, _objectSpread2.default)({}, obj);
  keys.forEach(function (key) {
    if (obj && key in obj) {
      picked[key] = obj[key];
      delete omitted[key];
    }
  });
  return {
    picked: picked,
    omitted: omitted
  };
};
/** 给disable的button switch checkbox包装一层，使其能正常触发事件 */


function parcelDisabledChild(element, prefixCls) {
  var elementType = element.type;
  var disableTypes = ['Button', 'Switch', 'Checkbox'];

  if ((disableTypes.includes(elementType.displayName) || element.type === 'button') && element.props.disabled) {
    var _splitObject = splitObject(element.props.style, ['position', 'left', 'right', 'top', 'bottom', 'float', 'display', 'zIndex']),
        picked = _splitObject.picked,
        omitted = _splitObject.omitted;

    var spanStyle = (0, _objectSpread2.default)((0, _objectSpread2.default)({
      display: 'inline-block'
    }, picked), {}, {
      cursor: 'not-allowed',
      width: element.props.block ? '100%' : null
    });
    var buttonStyle = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, omitted), {}, {
      pointerEvents: 'none'
    });

    var child = /*#__PURE__*/_react.default.cloneElement(element, {
      style: buttonStyle,
      className: null
    });

    return /*#__PURE__*/_react.default.createElement("span", {
      style: spanStyle,
      className: (0, _classnames.default)(element.props.className, "".concat(prefixCls, "-disabled-compatible-wrapper"))
    }, child);
  }

  return element;
}