import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React from 'react';
import classNames from 'classnames';
import { RcTooltip } from '../components';
import { ConfigContext } from '../config-provider/context';
import getTooltipPlacements from './getTooltipPlacements';
var whiteColor = '#ffffff';
var contentColor = '#262626';
var blackColor = 'rgba(0, 0, 0, 0.75)';
var Tooltip = /*#__PURE__*/React.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 = _objectWithoutProperties(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.useState(visible === undefined ? defaultVisible : visible),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      tipVisible = _React$useState2[0],
      setTipVisible = _React$useState2[1];

  var _React$useContext = React.useContext(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 = _objectSpread({
    background: bgColor
  }, overlayInnerStyle);

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

  var arrowContent = /*#__PURE__*/React.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.createElement(RcTooltip, Object.assign({
    ref: ref,
    visible: tipVisible,
    prefixCls: tooltipPrefix,
    placement: placement,
    trigger: trigger,
    transitionName: transitionName,
    builtinPlacements: getTooltipPlacements(arrowPointAtCenter),
    overlayInnerStyle: rcOverlayInnerStyle,
    overlay: overlay || title,
    arrowContent: arrowContent,
    onVisibleChange: handleVisibleChange,
    monitorWindowResize: monitorWindowResize,
    getTooltipContainer: tooltipGetPopupContainer || getPopupContainer
  }, reset), parcelDisabledChild( /*#__PURE__*/React.isValidElement(children) ? children : /*#__PURE__*/React.createElement("span", null, children), tooltipPrefix));
});
Tooltip.defaultProps = {
  mouseEnterDelay: 0.1,
  mouseLeaveDelay: 0.1,
  placement: 'top',
  trigger: ['hover'],
  arrowPointAtCenter: false
};
export default Tooltip;

var splitObject = function splitObject(obj, keys) {
  var picked = {};

  var omitted = _objectSpread({}, 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 = _objectSpread(_objectSpread({
      display: 'inline-block'
    }, picked), {}, {
      cursor: 'not-allowed',
      width: element.props.block ? '100%' : null
    });

    var buttonStyle = _objectSpread(_objectSpread({}, omitted), {}, {
      pointerEvents: 'none'
    });

    var child = /*#__PURE__*/React.cloneElement(element, {
      style: buttonStyle,
      className: null
    });
    return /*#__PURE__*/React.createElement("span", {
      style: spanStyle,
      className: classNames(element.props.className, "".concat(prefixCls, "-disabled-compatible-wrapper"))
    }, child);
  }

  return element;
}