"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Badge } from "../../primitives/badge.js";
import { SimpleTooltip } from "../../primitives/tooltip.js";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { cn } from "@open-mercato/shared/lib/utils";
const STATUS_DOT_CLASSES = {
  healthy: "bg-green-500",
  warning: "bg-yellow-500",
  error: "bg-red-500",
  unknown: "bg-gray-400"
};
function StatusDot({ status }) {
  return /* @__PURE__ */ jsx(
    "span",
    {
      className: cn("inline-block size-2 shrink-0 rounded-full", STATUS_DOT_CLASSES[status]),
      "aria-hidden": "true"
    }
  );
}
function StatusBadgeRenderer({ widget, context }) {
  const t = useT();
  const [result, setResult] = React.useState({ status: "unknown" });
  const mountedRef = React.useRef(true);
  React.useEffect(() => {
    mountedRef.current = true;
    const load = () => {
      widget.badge.statusLoader(context).then((data) => {
        if (mountedRef.current) setResult(data);
      }).catch(() => {
        if (mountedRef.current) setResult({ status: "unknown" });
      });
    };
    load();
    const intervalMs = (widget.badge.pollInterval ?? 60) * 1e3;
    const interval = setInterval(load, intervalMs);
    return () => {
      mountedRef.current = false;
      clearInterval(interval);
    };
  }, [widget.badge, context]);
  const label = t(widget.badge.label, widget.badge.label);
  const content = /* @__PURE__ */ jsxs("span", { className: "inline-flex items-center gap-1.5 text-sm", children: [
    /* @__PURE__ */ jsx(StatusDot, { status: result.status }),
    /* @__PURE__ */ jsx("span", { className: "truncate", children: label }),
    result.count != null && /* @__PURE__ */ jsx(Badge, { variant: "secondary", className: "ml-0.5 px-1.5 py-0 text-xs", children: result.count })
  ] });
  const wrapped = result.tooltip ? /* @__PURE__ */ jsx(SimpleTooltip, { content: result.tooltip, children: widget.badge.href ? /* @__PURE__ */ jsx("a", { href: widget.badge.href, className: "inline-flex items-center hover:opacity-80 transition-opacity", children: content }) : /* @__PURE__ */ jsx("span", { className: "inline-flex items-center", children: content }) }) : widget.badge.href ? /* @__PURE__ */ jsx("a", { href: widget.badge.href, className: "inline-flex items-center hover:opacity-80 transition-opacity", children: content }) : content;
  return /* @__PURE__ */ jsx("div", { className: "px-2 py-1", children: wrapped });
}
var StatusBadgeRenderer_default = StatusBadgeRenderer;
export {
  StatusBadgeRenderer,
  StatusBadgeRenderer_default as default
};
//# sourceMappingURL=StatusBadgeRenderer.js.map
