"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import {
  BarChart as RechartsBarChart,
  Bar,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  Legend,
  ResponsiveContainer
} 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 BarChart({
  title,
  data,
  index,
  categories,
  loading,
  error,
  colors,
  layout = "vertical",
  valueFormatter = defaultValueFormatter,
  showLegend = true,
  showGridLines = true,
  className = "",
  emptyMessage = "No data available",
  categoryLabels
}) {
  const getBarColor = (idx) => {
    return resolveChartColor(colors?.[idx], idx);
  };
  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-40 sm: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-40 sm: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-40 sm:h-48 items-center justify-center", children: /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: emptyMessage }) })
    ] });
  }
  const isHorizontal = layout === "horizontal";
  const chartHeight = isHorizontal ? Math.max(200, data.length * 28) : 200;
  const chartContent = /* @__PURE__ */ jsx(ResponsiveContainer, { width: "100%", height: chartHeight, children: /* @__PURE__ */ jsxs(
    RechartsBarChart,
    {
      data,
      layout: isHorizontal ? "vertical" : "horizontal",
      margin: { top: 5, right: 30, left: 20, bottom: 5 },
      children: [
        showGridLines && /* @__PURE__ */ jsx(CartesianGrid, { strokeDasharray: "3 3", stroke: "hsl(var(--border))" }),
        /* @__PURE__ */ jsx(
          XAxis,
          {
            type: isHorizontal ? "number" : "category",
            dataKey: isHorizontal ? void 0 : index,
            tickFormatter: isHorizontal ? valueFormatter : void 0,
            tick: { fontSize: 11 }
          }
        ),
        /* @__PURE__ */ jsx(
          YAxis,
          {
            type: isHorizontal ? "category" : "number",
            dataKey: isHorizontal ? index : void 0,
            tickFormatter: isHorizontal ? void 0 : valueFormatter,
            width: isHorizontal ? 90 : 50,
            interval: 0,
            tick: { fontSize: 10 }
          }
        ),
        /* @__PURE__ */ jsx(
          Tooltip,
          {
            content: /* @__PURE__ */ jsx(
              ChartTooltipContent,
              {
                valueFormatter,
                categoryLabels,
                labelFormatter: (label, payload) => {
                  const entry = payload?.[0];
                  const item = entry?.payload;
                  return item?.[index] ? String(item[index]) : label;
                }
              }
            ),
            cursor: { fill: "hsl(var(--muted))", opacity: 0.2 }
          }
        ),
        showLegend && categories.length > 1 && /* @__PURE__ */ jsx(Legend, { verticalAlign: "top", height: 36 }),
        categories.map((category, idx) => /* @__PURE__ */ jsx(
          Bar,
          {
            dataKey: category,
            fill: getBarColor(idx),
            radius: isHorizontal ? [0, 4, 4, 0] : [4, 4, 0, 0]
          },
          category
        ))
      ]
    }
  ) });
  return /* @__PURE__ */ jsxs("div", { className: wrapperClass, children: [
    title && /* @__PURE__ */ jsx("h3", { className: "mb-4 text-base font-medium text-card-foreground", children: title }),
    chartContent
  ] });
}
var BarChart_default = BarChart;
export {
  BarChart,
  BarChart_default as default
};
//# sourceMappingURL=BarChart.js.map
