"use client";
import { Fragment, jsx } from "react/jsx-runtime";
import * as React from "react";
import { SimpleTooltip } from "../primitives/tooltip.js";
import { cn } from "@open-mercato/shared/lib/utils";
function extractTextContent(node) {
  if (node == null) return "";
  if (typeof node === "string") return node;
  if (typeof node === "number") return String(node);
  if (typeof node === "boolean") return "";
  if (Array.isArray(node)) {
    return node.map(extractTextContent).join("");
  }
  if (React.isValidElement(node)) {
    const props = node.props;
    if (props) {
      if (props.children != null) {
        const childText = extractTextContent(props.children);
        if (childText) return childText;
      }
      if (typeof props.value === "string") return props.value;
      if (typeof props.label === "string") return props.label;
      if (typeof props.title === "string") return props.title;
    }
  }
  if (node && typeof node === "object" && "toString" in node) {
    const str = String(node);
    if (str !== "[object Object]") return str;
  }
  return "";
}
function TruncatedCell({
  children,
  maxWidth = "max-w-[150px]",
  className,
  tooltipContent,
  disabled = false
}) {
  const contentRef = React.useRef(null);
  const [isTruncated, setIsTruncated] = React.useState(false);
  const resolvedTooltipContent = tooltipContent ?? extractTextContent(children);
  React.useEffect(() => {
    const checkTruncation = () => {
      const el2 = contentRef.current;
      if (el2) {
        setIsTruncated(el2.scrollWidth > el2.clientWidth);
      }
    };
    checkTruncation();
    const el = contentRef.current;
    if (el) {
      const observer = new ResizeObserver(checkTruncation);
      observer.observe(el);
      return () => observer.disconnect();
    }
  }, [children, maxWidth]);
  if (disabled) {
    return /* @__PURE__ */ jsx(Fragment, { children });
  }
  const isTailwindClass = maxWidth.startsWith("max-w-");
  const styleMaxWidth = isTailwindClass ? void 0 : maxWidth;
  const classMaxWidth = isTailwindClass ? maxWidth : "";
  const content = /* @__PURE__ */ jsx(
    "div",
    {
      ref: contentRef,
      className: cn(
        "overflow-hidden text-ellipsis whitespace-nowrap",
        classMaxWidth,
        className
      ),
      style: styleMaxWidth ? { maxWidth: styleMaxWidth } : void 0,
      children
    }
  );
  if (!resolvedTooltipContent || !isTruncated) {
    return content;
  }
  return /* @__PURE__ */ jsx(
    SimpleTooltip,
    {
      content: resolvedTooltipContent,
      side: "top",
      delayDuration: 300,
      children: content
    }
  );
}
export {
  TruncatedCell
};
//# sourceMappingURL=TruncatedCell.js.map
