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 Tooltip from "../tooltip";

function useCombinedRefs() {
  var refs = [];

  for (var _i = 0; _i < arguments.length; _i++) {
    refs[_i] = arguments[_i];
  }

  var targetRef = React.useRef(null);
  React.useEffect(function () {
    refs.forEach(function (ref) {
      if (!ref) return;

      if (typeof ref === 'function') {
        ref(targetRef.current);
      } else {
        ref.current = targetRef.current;
      }
    });
  }, [refs]);
  return targetRef;
}

var SliderTooltip = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var visible = props.visible;
  var innerRef = React.useRef(null);
  var tooltipRef = useCombinedRefs(ref, innerRef);
  var rafRef = React.useRef(null);

  var cancelKeepAlign = function cancelKeepAlign() {
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    window.cancelAnimationFrame(rafRef.current);
    rafRef.current = null;
  };

  var keepAlign = function keepAlign() {
    if (rafRef.current !== null) {
      return;
    }

    rafRef.current = window.requestAnimationFrame(function () {
      var _a;

      (_a = tooltipRef.current) === null || _a === void 0 ? void 0 : _a.forceAlign();
      rafRef.current = null;
      keepAlign();
    });
  };

  React.useEffect(function () {
    if (visible) {
      keepAlign();
    } else {
      cancelKeepAlign();
    }

    return cancelKeepAlign; // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [visible]);
  return _jsx(Tooltip, __assign({
    ref: tooltipRef
  }, props), void 0);
});
export default SliderTooltip;

