"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { Spinner } from "@open-mercato/ui/primitives/spinner";
function getNestedValue(obj, path) {
  return path.split(".").reduce((current, key) => {
    if (current && typeof current === "object" && key in current) {
      return current[key];
    }
    return void 0;
  }, obj);
}
function defaultFormatter(value) {
  if (value === null || value === void 0) return "--";
  if (typeof value === "number") {
    if (Math.abs(value) >= 1e6) {
      return `${(value / 1e6).toFixed(1)}M`;
    }
    if (Math.abs(value) >= 1e3) {
      return `${(value / 1e3).toFixed(1)}K`;
    }
    return value.toLocaleString(void 0, { maximumFractionDigits: 2 });
  }
  return String(value);
}
function TopNTable({
  title,
  data,
  columns,
  loading,
  error,
  className = "",
  emptyMessage = "No data available",
  maxRows
}) {
  if (error) {
    return /* @__PURE__ */ jsxs("div", { className: `rounded-lg border bg-card p-4 ${className}`, children: [
      title && /* @__PURE__ */ jsx("h3", { className: "mb-4 text-base font-medium text-card-foreground", children: title }),
      /* @__PURE__ */ jsx("div", { className: "flex h-48 items-center justify-center", children: /* @__PURE__ */ jsx("p", { className: "text-sm text-destructive", children: error }) })
    ] });
  }
  if (loading) {
    return /* @__PURE__ */ jsxs("div", { className: `rounded-lg border bg-card p-4 ${className}`, children: [
      title && /* @__PURE__ */ jsx("h3", { className: "mb-4 text-base font-medium text-card-foreground", children: title }),
      /* @__PURE__ */ jsx("div", { className: "flex h-48 items-center justify-center", children: /* @__PURE__ */ jsx(Spinner, { className: "h-6 w-6 text-muted-foreground" }) })
    ] });
  }
  if (!data || data.length === 0) {
    return /* @__PURE__ */ jsxs("div", { className: `rounded-lg border bg-card p-4 ${className}`, children: [
      title && /* @__PURE__ */ jsx("h3", { className: "mb-4 text-base font-medium text-card-foreground", children: title }),
      /* @__PURE__ */ jsx("div", { className: "flex h-48 items-center justify-center", children: /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: emptyMessage }) })
    ] });
  }
  const displayData = maxRows && maxRows > 0 ? data.slice(0, maxRows) : data;
  const getAlignClass = (align) => {
    if (align === "right") return "text-right";
    if (align === "center") return "text-center";
    return "text-left";
  };
  return /* @__PURE__ */ jsxs("div", { className: `rounded-lg border bg-card p-4 ${className}`, children: [
    title && /* @__PURE__ */ jsx("h3", { className: "mb-4 text-base font-medium text-card-foreground", children: title }),
    /* @__PURE__ */ jsx("div", { className: "overflow-x-auto", children: /* @__PURE__ */ jsxs("table", { className: "w-full", children: [
      /* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsx("tr", { className: "border-b border-border", children: columns.map((column) => /* @__PURE__ */ jsx(
        "th",
        {
          className: `pb-2 text-sm font-medium text-muted-foreground ${getAlignClass(column.align)}`,
          style: column.width ? { width: column.width } : void 0,
          children: column.header
        },
        String(column.key)
      )) }) }),
      /* @__PURE__ */ jsx("tbody", { children: displayData.map((row, rowIndex) => /* @__PURE__ */ jsx("tr", { className: "border-b border-border last:border-0", children: columns.map((column) => {
        const rawValue = getNestedValue(row, String(column.key));
        const formatted = column.formatter ? column.formatter(rawValue, row) : defaultFormatter(rawValue);
        return /* @__PURE__ */ jsx(
          "td",
          {
            className: `py-2 text-sm text-card-foreground ${getAlignClass(column.align)}`,
            children: formatted
          },
          String(column.key)
        );
      }) }, rowIndex)) })
    ] }) })
  ] });
}
var TopNTable_default = TopNTable;
export {
  TopNTable,
  TopNTable_default as default
};
//# sourceMappingURL=TopNTable.js.map
