import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["content", "children", "trigger", "placement", "arrowContent", "overlayInnerStyle", "overlayClassName", "showArrow", "transparent", "onVisibleChange", "onPopupAlign", "motion", "getTooltipContainer", "mouseEnterDelay", "mouseLeaveDelay", "overlayOffset", "verticalOffset", "afterVisibleChange", "disable", "isArrowAndTargetCenter"];
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import "core-js/modules/es.symbol.js";
import "core-js/modules/es.array.concat.js";
import "core-js/modules/es.array.filter.js";
import "core-js/modules/es.array.for-each.js";
import "core-js/modules/es.array.includes.js";
import "core-js/modules/es.array.push.js";
import "core-js/modules/es.object.define-properties.js";
import "core-js/modules/es.object.define-property.js";
import "core-js/modules/es.object.get-own-property-descriptor.js";
import "core-js/modules/es.object.get-own-property-descriptors.js";
import "core-js/modules/es.object.keys.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.string.includes.js";
import "core-js/modules/esnext.iterator.constructor.js";
import "core-js/modules/esnext.iterator.filter.js";
import "core-js/modules/esnext.iterator.for-each.js";
import "core-js/modules/web.dom-collections.for-each.js";
import RcToolTip from 'rc-tooltip';
import { useRef } from 'react';
import './index.css';
import './arrow.css';
import { FcrIcon } from '../icon';
import { FcrIconType } from '../icon/type';
import React from 'react';
import classNames from 'classnames';
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
var calcOverlayOffset = function calcOverlayOffset(placement, offset, verticalOffset) {
  var useVerticalOffset = verticalOffset !== null && verticalOffset !== void 0 ? verticalOffset : 0;
  if (placement.includes('top')) {
    return [useVerticalOffset, -offset];
  }
  if (placement.includes('bottom')) {
    return [useVerticalOffset, offset];
  }
  if (placement.includes('left')) {
    return [-offset, useVerticalOffset];
  }
  if (placement.includes('right')) {
    return [offset, useVerticalOffset];
  }
};
export var FcrToolTip = function FcrToolTip(props) {
  var targetRef = useRef(null);
  var content = props.content,
    children = props.children,
    trigger = props.trigger,
    _props$placement = props.placement,
    placement = _props$placement === void 0 ? 'top' : _props$placement,
    arrowContent = props.arrowContent,
    overlayInnerStyle = props.overlayInnerStyle,
    overlayClassName = props.overlayClassName,
    _props$showArrow = props.showArrow,
    propsShowArrow = _props$showArrow === void 0 ? true : _props$showArrow,
    _props$transparent = props.transparent,
    transparent = _props$transparent === void 0 ? false : _props$transparent,
    onVisibleChange = props.onVisibleChange,
    _onPopupAlign = props.onPopupAlign,
    motion = props.motion,
    _getTooltipContainer = props.getTooltipContainer,
    mouseEnterDelay = props.mouseEnterDelay,
    mouseLeaveDelay = props.mouseLeaveDelay,
    _props$overlayOffset = props.overlayOffset,
    overlayOffset = _props$overlayOffset === void 0 ? 12 : _props$overlayOffset,
    verticalOffset = props.verticalOffset,
    afterVisibleChange = props.afterVisibleChange,
    disable = props.disable,
    _props$isArrowAndTarg = props.isArrowAndTargetCenter,
    isArrowAndTargetCenter = _props$isArrowAndTarg === void 0 ? false : _props$isArrowAndTarg,
    others = _objectWithoutProperties(props, _excluded);

  // 如果是透明的 tooltip，则不显示箭头
  var showArrow = transparent ? false : propsShowArrow;
  var transparentStyle = {
    padding: '6px 8px',
    background: "var(--fcr_mobile_ui_scene_color_popup_fill2)",
    border: "1px solid var(--fcr_ui_scene_line1)",
    borderRadius: "var(--fcr_cornerradius_xs)",
    fontWeight: 'var(--fcr_font_weight_ms)',
    fontSize: '12px',
    color: "var(--fcr_ui_scene_icontext1)",
    boxShadow: "var(--fcr_web_light_dropup_m)"
  };
  var defaultOverlayInnerStyle = _objectSpread({
    padding: '10px',
    background: "var(--fcr_ui_scene_inverse2)",
    border: "1px solid var(--fcr_ui_scene_inverse2)",
    fontStyle: 'normal',
    fontWeight: 'var(--fcr_font_weight_s)',
    fontSize: '14px',
    lineHeight: '138%',
    color: "var(--fcr_ui_scene_inverse)",
    borderRadius: "var(--fcr_cornerradius_l)",
    boxShadow: '0px 4px 4px 0px rgba(0, 0, 0, 0.25)',
    textAlign: 'center'
  }, transparent ? transparentStyle : {});
  var handleArrowAndTargetCenter = function handleArrowAndTargetCenter(tooltip) {
    if (!isArrowAndTargetCenter) {
      return;
    }
    if (targetRef.current) {
      var targetRect = targetRef.current.getBoundingClientRect();
      var tooltipRect = tooltip.getBoundingClientRect();

      /**
       * @cn 获取元素偏移的轴向，默认返回 ['left', 'width', 'X']，第一个元素表示偏移轴向，第二个元素表示元素的宽度或高度，第三个元素表示偏移的坐标轴。
       */
      var getCenterAxis = function getCenterAxis(placement) {
        if (placement.includes('top') || placement.includes('bottom')) {
          return ['left', 'width', 'X'];
        }
        if (placement.includes('left') || placement.includes('right')) {
          return ['top', 'height', 'Y'];
        }
        return ['left', 'width', 'X'];
      };
      var _getCenterAxis = getCenterAxis(placement),
        _getCenterAxis2 = _slicedToArray(_getCenterAxis, 3),
        leftOrRight = _getCenterAxis2[0],
        widthOrHeight = _getCenterAxis2[1],
        xOrY = _getCenterAxis2[2];
      var targetRectCenter = targetRect[leftOrRight] + targetRect[widthOrHeight] / 2;
      var tooltipRectCenter = tooltipRect[leftOrRight] + tooltipRect[widthOrHeight] / 2;
      var offset = targetRectCenter - tooltipRectCenter;

      // 使用类名后缀选择箭头 Dom
      var arrowEl = tooltip.querySelector('[class$="-arrow"]');
      arrowEl.style[leftOrRight] = '50%';
      arrowEl.style.transform = "translate".concat(xOrY, "(").concat(offset, "px)");
    }
  };
  return /*#__PURE__*/_jsx(RcToolTip, _objectSpread(_objectSpread({
    destroyTooltipOnHide: true,
    afterVisibleChange: afterVisibleChange,
    mouseLeaveDelay: mouseLeaveDelay,
    mouseEnterDelay: mouseEnterDelay !== null && mouseEnterDelay !== void 0 ? mouseEnterDelay : 1,
    getTooltipContainer: function getTooltipContainer(target) {
      targetRef.current = target;
      if (_getTooltipContainer) {
        return _getTooltipContainer(target);
      }
      var doc = target.ownerDocument || document;
      return doc.body;
    },
    onVisibleChange: onVisibleChange,
    prefixCls: "fcr-tooltip",
    overlayClassName: classNames(overlayClassName, {
      'fcr-tooltip-disable': disable
    }),
    arrowContent: showArrow === false ? null : arrowContent || /*#__PURE__*/_jsx(FcrIcon, {
      colors: {
        iconPrimary: 'var(--fcr_ui_scene_inverse2)'
      },
      type: FcrIconType.FCR_TOOLTIP_ARROW,
      size: 16
    }),
    align: {
      offset: calcOverlayOffset(placement, overlayOffset, verticalOffset)
    },
    trigger: trigger,
    placement: placement,
    overlay: content,
    overlayInnerStyle: _objectSpread(_objectSpread({}, defaultOverlayInnerStyle), overlayInnerStyle),
    motion: motion || {
      motionAppear: true,
      motionName: 'fcr-tooltip-anim'
    },
    onPopupAlign: function onPopupAlign(tooltipEl, align) {
      handleArrowAndTargetCenter(tooltipEl);
      if (_onPopupAlign) {
        _onPopupAlign(tooltipEl, align);
      }
    }
  }, others), {}, {
    children: children || /*#__PURE__*/_jsx(_Fragment, {})
  }));
};