var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

import { jsx as _jsx } from "react/jsx-runtime";
import * as React from 'react';
import RcTooltip from 'rc-tooltip';
import classNames from 'classnames';
import { cloneElement, isValidElement } from "../_utils/reactNode";
import getPlacements from "./placements";
import "./style/tooltip.css";

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

  var omitted = __assign({}, obj);

  keys.forEach(function (key) {
    if (obj && key in obj) {
      picked[key] = obj[key];
      delete omitted[key];
    }
  });
  return {
    picked: picked,
    omitted: omitted
  };
};

var layoutAttr = ['position', 'left', 'right', 'top', 'bottom', 'float', 'display', 'zIndex'];
var ignoreChildren = ['Handle']; // eslint-disable-next-line @typescript-eslint/no-explicit-any

var getDisabledCompatibleChildren = function getDisabledCompatibleChildren(element, prefixCls) {
  var type = typeof element.type === 'string' ? element.type : element.type.name;

  if (element.props.disabled && !ignoreChildren.includes(type)) {
    var _a = splitObject(element.props.style, layoutAttr),
        picked = _a.picked,
        omitted = _a.omitted;

    var spanStyle = __assign(__assign({
      display: 'inline-block'
    }, picked), {
      cursor: 'not-allowed',
      width: element.props.block ? '100%' : undefined
    });

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

    var child = cloneElement(element, {
      style: buttonStyle
    });
    return _jsx("span", __assign({
      style: spanStyle,
      className: prefixCls + "-disabled-compatible-wrapper"
    }, {
      children: child
    }), void 0);
  }

  return element;
};

var DEFAULT_PREFIXCLS = 'dmc-tooltip';
export var Tooltip = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _a;

  var _b = React.useState(!!props.visible || !!props.defaultVisible),
      visible = _b[0],
      setVisible = _b[1];

  React.useEffect(function () {
    if ('visible' in props) {
      setVisible(!!props.visible);
    } // eslint-disable-next-line react-hooks/exhaustive-deps

  }, [props.visible]);

  var isNoTitle = function isNoTitle() {
    var title = props.title,
        overlay = props.overlay;
    return !title && !overlay && title !== 0; // overlay for old version compatibility
  };

  var onVisibleChange = function onVisibleChange(vis) {
    if (!('visible' in props)) {
      setVisible(isNoTitle() ? false : vis);
    }

    if (props.onVisibleChange && !isNoTitle()) {
      props.onVisibleChange(vis);
    }
  };

  var getTooltipPlacements = function getTooltipPlacements() {
    var builtinPlacements = props.builtinPlacements,
        arrowPointAtCenter = props.arrowPointAtCenter,
        autoAdjustOverflow = props.autoAdjustOverflow;
    return builtinPlacements || getPlacements({
      arrowPointAtCenter: arrowPointAtCenter,
      autoAdjustOverflow: autoAdjustOverflow
    });
  }; // 动态设置动画点


  var onPopupAlign = function onPopupAlign(domNode, align) {
    var placements = getTooltipPlacements(); // 当前返回的位置

    var placement = Object.keys(placements).filter(function (key) {
      return placements[key].points[0] === align.points[0] && placements[key].points[1] === align.points[1];
    })[0];

    if (!placement) {
      return;
    } // 根据当前坐标设置动画点


    var rect = domNode.getBoundingClientRect();
    var transformOrigin = {
      top: '50%',
      left: '50%'
    };

    if (placement.indexOf('top') >= 0 || placement.indexOf('Bottom') >= 0) {
      transformOrigin.top = rect.height - align.offset[1] + "px";
    } else if (placement.indexOf('Top') >= 0 || placement.indexOf('bottom') >= 0) {
      transformOrigin.top = -align.offset[1] + "px";
    }

    if (placement.indexOf('left') >= 0 || placement.indexOf('Right') >= 0) {
      transformOrigin.left = rect.width - align.offset[0] + "px";
    } else if (placement.indexOf('right') >= 0 || placement.indexOf('Left') >= 0) {
      transformOrigin.left = -align.offset[0] + "px";
    }

    domNode.style.transformOrigin = transformOrigin.left + " " + transformOrigin.top;
  };

  var getOverlay = function getOverlay() {
    var title = props.title,
        overlay = props.overlay;

    if (title === 0) {
      return title;
    }

    return overlay || title || '';
  };

  var _c = props.prefixCls,
      prefixCls = _c === void 0 ? DEFAULT_PREFIXCLS : _c,
      openClassName = props.openClassName,
      getPopupContainer = props.getPopupContainer,
      getTooltipContainer = props.getTooltipContainer,
      overlayClassName = props.overlayClassName; // eslint-disable-next-line @typescript-eslint/no-explicit-any

  var children = props.children;
  var tempVisible = visible; // Hide tooltip when there is no title

  if (!('visible' in props) && isNoTitle()) {
    tempVisible = false;
  }

  var wrapChildren = isValidElement(children) ? children : _jsx("span", {
    children: children
  }, void 0);
  var child = getDisabledCompatibleChildren(wrapChildren, prefixCls);
  var childProps = child.props;
  var childCls = classNames(childProps.className, (_a = {}, _a[openClassName || prefixCls + "-open"] = true, _a));
  return _jsx(RcTooltip, __assign({}, props, {
    prefixCls: prefixCls,
    overlayClassName: overlayClassName,
    getTooltipContainer: getPopupContainer || getTooltipContainer,
    ref: ref,
    builtinPlacements: getTooltipPlacements(),
    overlay: getOverlay(),
    visible: tempVisible,
    onVisibleChange: onVisibleChange,
    onPopupAlign: onPopupAlign
  }, {
    children: tempVisible ? cloneElement(child, {
      className: childCls
    }) : child
  }), void 0);
});
Tooltip.defaultProps = {
  placement: 'top',
  mouseEnterDelay: 0.1,
  mouseLeaveDelay: 0.1,
  arrowPointAtCenter: false,
  autoAdjustOverflow: true
};
Tooltip.displayName = 'Tooltip';
export default Tooltip;
var TooltipMemo = /*#__PURE__*/React.memo(Tooltip);
TooltipMemo.displayName = 'TooltipMemo';
export { TooltipMemo };

