"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { Check, X, Infinity } from "lucide-react";
import { Badge } from "../../ui/badge";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow
} from "../../ui/table";
import { cn } from "../../../lib/utils";
import { sel } from "../../../lib/test";
function PlanFeaturesMatrix({
  plans,
  features,
  limits,
  matrix
}) {
  const sortedPlans = [...plans].sort((a, b) => {
    var _a, _b;
    const typeOrder = { free: 0, paid: 1, enterprise: 2 };
    const typeCompare = (typeOrder[a.type] ?? 1) - (typeOrder[b.type] ?? 1);
    if (typeCompare !== 0) return typeCompare;
    return (((_a = a.price) == null ? void 0 : _a.monthly) ?? 0) - (((_b = b.price) == null ? void 0 : _b.monthly) ?? 0);
  });
  const getPlanBadgeVariant = (plan) => {
    switch (plan.type) {
      case "free":
        return "secondary";
      case "paid":
        return "default";
      case "enterprise":
        return "destructive";
      default:
        return "outline";
    }
  };
  const formatPrice = (plan) => {
    if (plan.type === "free") return "Free";
    if (plan.type === "enterprise") return "Contact Sales";
    if (!plan.price) return "N/A";
    return `$${(plan.price.monthly / 100).toFixed(0)}/mo`;
  };
  const formatLimit = (value, limitDef) => {
    if (value === -1) return "Unlimited";
    if (value === 0) return "\u2014";
    switch (limitDef.unit) {
      case "bytes":
        return `${value} GB`;
      case "calls":
        return value.toLocaleString();
      case "count":
      default:
        return value.toLocaleString();
    }
  };
  const formatFeatureLabel = (slug, def) => {
    return slug.split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
  };
  const formatLimitLabel = (slug, def) => {
    return slug.split("_").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
  };
  const featureSlugs = Object.keys(features);
  const limitSlugs = Object.keys(limits);
  return /* @__PURE__ */ jsx("div", { className: "rounded-md border", children: /* @__PURE__ */ jsxs(Table, { children: [
    /* @__PURE__ */ jsx(TableHeader, { children: /* @__PURE__ */ jsxs(TableRow, { children: [
      /* @__PURE__ */ jsx(TableHead, { className: "w-[280px]", children: "Feature / Limit" }),
      sortedPlans.map((plan) => /* @__PURE__ */ jsx(TableHead, { className: "text-center min-w-[120px]", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col items-center gap-1.5", children: [
        /* @__PURE__ */ jsx("span", { className: "capitalize font-semibold", children: plan.slug }),
        /* @__PURE__ */ jsx(
          Badge,
          {
            variant: getPlanBadgeVariant(plan),
            className: "text-[10px] px-1.5 py-0",
            children: formatPrice(plan)
          }
        ),
        plan.visibility === "hidden" && /* @__PURE__ */ jsx(Badge, { variant: "outline", className: "text-[9px] px-1 py-0", children: "hidden" })
      ] }) }, plan.slug))
    ] }) }),
    /* @__PURE__ */ jsxs(TableBody, { children: [
      /* @__PURE__ */ jsx(TableRow, { className: "bg-muted/50 hover:bg-muted/50", children: /* @__PURE__ */ jsx(
        TableCell,
        {
          colSpan: sortedPlans.length + 1,
          className: "font-semibold text-sm py-3",
          children: "Features"
        }
      ) }),
      featureSlugs.map((featureSlug) => /* @__PURE__ */ jsxs(
        TableRow,
        {
          "data-cy": sel("superadmin.planFeatures.featureRow", { slug: featureSlug }),
          className: "hover:bg-muted/30",
          children: [
            /* @__PURE__ */ jsx(TableCell, { className: "font-medium text-sm text-muted-foreground pl-6", children: formatFeatureLabel(featureSlug, features[featureSlug]) }),
            sortedPlans.map((plan) => {
              var _a;
              const hasFeature = ((_a = matrix.features[featureSlug]) == null ? void 0 : _a[plan.slug]) ?? false;
              return /* @__PURE__ */ jsx(
                TableCell,
                {
                  className: "text-center",
                  children: hasFeature ? /* @__PURE__ */ jsx(Check, { className: "h-4 w-4 text-green-600 mx-auto" }) : /* @__PURE__ */ jsx(X, { className: "h-4 w-4 text-red-400/60 mx-auto" })
                },
                `${plan.slug}-${featureSlug}`
              );
            })
          ]
        },
        featureSlug
      )),
      /* @__PURE__ */ jsx(TableRow, { className: "bg-muted/50 hover:bg-muted/50", children: /* @__PURE__ */ jsx(
        TableCell,
        {
          colSpan: sortedPlans.length + 1,
          className: "font-semibold text-sm py-3",
          children: "Limits & Quotas"
        }
      ) }),
      limitSlugs.map((limitSlug) => {
        const limitDef = limits[limitSlug];
        return /* @__PURE__ */ jsxs(
          TableRow,
          {
            "data-cy": sel("superadmin.planFeatures.limitRow", { slug: limitSlug }),
            className: "hover:bg-muted/30",
            children: [
              /* @__PURE__ */ jsx(TableCell, { className: "font-medium text-sm text-muted-foreground pl-6", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
                formatLimitLabel(limitSlug, limitDef),
                limitDef.resetPeriod !== "never" && /* @__PURE__ */ jsx(Badge, { variant: "outline", className: "text-[9px] px-1 py-0", children: limitDef.resetPeriod })
              ] }) }),
              sortedPlans.map((plan) => {
                var _a;
                const value = ((_a = matrix.limits[limitSlug]) == null ? void 0 : _a[plan.slug]) ?? 0;
                const isUnlimited = value === -1;
                return /* @__PURE__ */ jsx(
                  TableCell,
                  {
                    className: cn(
                      "text-center font-mono text-sm",
                      isUnlimited && "text-green-600"
                    ),
                    children: isUnlimited ? /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center gap-1", children: /* @__PURE__ */ jsx(Infinity, { className: "h-4 w-4" }) }) : formatLimit(value, limitDef)
                  },
                  `${plan.slug}-${limitSlug}`
                );
              })
            ]
          },
          limitSlug
        );
      })
    ] })
  ] }) });
}
export {
  PlanFeaturesMatrix
};
