"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import {
  PieChart as RechartsPieChart,
  Pie,
  Cell,
  Tooltip,
  ResponsiveContainer,
  Legend,
  Label
} from "recharts";
import { Spinner } from "@open-mercato/ui/primitives/spinner";
import { ChartTooltipContent, resolveChartColor } from "./ChartUtils.js";
function defaultValueFormatter(value) {
  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 });
}
function PieChart({
  title,
  data,
  loading,
  error,
  colors,
  variant = "donut",
  valueFormatter = defaultValueFormatter,
  showLabel = true,
  showTooltip = true,
  className = "",
  emptyMessage = "No data available"
}) {
  const getSliceColor = (idx) => {
    return resolveChartColor(colors?.[idx], idx);
  };
  const total = React.useMemo(() => {
    return data.reduce((sum, item) => sum + item.value, 0);
  }, [data]);
  const hasWrapper = !!title;
  const wrapperClass = hasWrapper ? `rounded-lg border bg-card p-4 ${className}` : className;
  if (error) {
    return /* @__PURE__ */ jsxs("div", { className: wrapperClass, 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: wrapperClass, 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: wrapperClass, 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 innerRadius = variant === "donut" ? "60%" : 0;
  const outerRadius = "80%";
  return /* @__PURE__ */ jsxs("div", { className: wrapperClass, children: [
    title && /* @__PURE__ */ jsx("h3", { className: "mb-4 text-base font-medium text-card-foreground", children: title }),
    /* @__PURE__ */ jsx("div", { className: "h-52 w-full", children: /* @__PURE__ */ jsx(ResponsiveContainer, { width: "100%", height: "100%", children: /* @__PURE__ */ jsxs(RechartsPieChart, { children: [
      /* @__PURE__ */ jsxs(
        Pie,
        {
          data,
          dataKey: "value",
          nameKey: "name",
          cx: "50%",
          cy: "40%",
          innerRadius,
          outerRadius,
          paddingAngle: 2,
          strokeWidth: 0,
          children: [
            data.map((_, idx) => /* @__PURE__ */ jsx(Cell, { fill: getSliceColor(idx) }, `cell-${idx}`)),
            showLabel && variant === "donut" && /* @__PURE__ */ jsx(
              Label,
              {
                value: valueFormatter(total),
                position: "center",
                className: "fill-foreground text-2xl font-bold"
              }
            )
          ]
        }
      ),
      showTooltip && /* @__PURE__ */ jsx(
        Tooltip,
        {
          content: /* @__PURE__ */ jsx(
            ChartTooltipContent,
            {
              valueFormatter,
              hideLabel: true
            }
          )
        }
      ),
      /* @__PURE__ */ jsx(
        Legend,
        {
          verticalAlign: "bottom",
          height: 36,
          formatter: (value) => /* @__PURE__ */ jsx("span", { style: { color: "hsl(var(--muted-foreground))", fontSize: "12px" }, children: value })
        }
      )
    ] }) }) })
  ] });
}
var PieChart_default = PieChart;
export {
  PieChart,
  PieChart_default as default
};
//# sourceMappingURL=PieChart.js.map
