"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const kit_Tooltip = require("../Tooltip.cjs");
const kit_utils_loadable = require("../utils/loadable.cjs");
function useTableTooltip({ columns, data }) {
  const [tooltipProps, setTooltipProps] = React.useState(void 0);
  const timeoutRef = React.useRef(0);
  const closeTooltip = React.useCallback(() => {
    window.clearTimeout(timeoutRef.current);
    timeoutRef.current = 0;
    setTooltipProps(void 0);
  }, []);
  const onItemHovered = React.useCallback(
    (args) => {
      var _a;
      if (args.kind === "cell") {
        window.clearTimeout(timeoutRef.current);
        setTooltipProps(void 0);
        const [columnIdx, rowIdx] = args.location;
        const record = data[rowIdx];
        if (record && kit_utils_loadable.Loadable.isLoaded(record)) {
          const text = (_a = columns[columnIdx]) == null ? void 0 : _a.tooltip(record.data);
          if (text) {
            timeoutRef.current = window.setTimeout(() => {
              setTooltipProps({
                open: true,
                text,
                x: args.bounds.x + 20,
                y: args.bounds.y + 20
              });
            }, 500);
          }
        }
      } else {
        closeTooltip();
      }
    },
    [data, columns, closeTooltip]
  );
  React.useEffect(() => () => window.clearTimeout(timeoutRef.current), []);
  const tooltip = React.useMemo(
    () => tooltipProps ? /* @__PURE__ */ jsxRuntime.jsx(
      "div",
      {
        style: {
          left: tooltipProps.x,
          position: "fixed",
          top: tooltipProps.y,
          zIndex: 10
        },
        children: /* @__PURE__ */ jsxRuntime.jsx(kit_Tooltip, { content: tooltipProps.text, open: tooltipProps.open, placement: "top" })
      }
    ) : null,
    [tooltipProps]
  );
  return {
    closeTooltip,
    onItemHovered,
    tooltip
  };
}
exports.useTableTooltip = useTableTooltip;
