"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import Link from "next/link";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow
} from "@nextsparkjs/core/components/ui/table";
import { Button } from "@nextsparkjs/core/components/ui/button";
import { Badge } from "@nextsparkjs/core/components/ui/badge";
import { Skeleton } from "@nextsparkjs/core/components/ui/skeleton";
import { ExternalLink, FileText, LayoutGrid, Newspaper } from "lucide-react";
import { sel } from "@nextsparkjs/core/lib/test";
const entityIcons = {
  pages: FileText,
  posts: Newspaper,
  patterns: LayoutGrid
};
function getStatusVariant(status) {
  switch (status) {
    case "published":
      return "default";
    case "draft":
      return "secondary";
    case "archived":
      return "outline";
    default:
      return "secondary";
  }
}
function formatDate(dateString) {
  if (!dateString) return "-";
  return new Date(dateString).toLocaleDateString("en-US", {
    year: "numeric",
    month: "short",
    day: "numeric"
  });
}
function getEntityDisplayName(usage) {
  if (usage.entityTitle) return usage.entityTitle;
  if (usage.entityName) return usage.entityName;
  if (usage.entityFirstName || usage.entityLastName) {
    return [usage.entityFirstName, usage.entityLastName].filter(Boolean).join(" ");
  }
  if (usage.entityEmail) return usage.entityEmail;
  if (usage.entitySlug) return usage.entitySlug;
  return usage.entityId;
}
function PatternUsageTable({
  usages,
  isLoading,
  emptyMessage = "No usages found"
}) {
  if (isLoading) {
    return /* @__PURE__ */ jsx("div", { className: "rounded-md border", "data-cy": sel("patterns.usageTable.loading"), children: /* @__PURE__ */ jsxs(Table, { children: [
      /* @__PURE__ */ jsx(TableHeader, { children: /* @__PURE__ */ jsxs(TableRow, { children: [
        /* @__PURE__ */ jsx(TableHead, { children: "Entity" }),
        /* @__PURE__ */ jsx(TableHead, { children: "Type" }),
        /* @__PURE__ */ jsx(TableHead, { children: "Status" }),
        /* @__PURE__ */ jsx(TableHead, { children: "Last Updated" }),
        /* @__PURE__ */ jsx(TableHead, { className: "w-[80px]", children: "Actions" })
      ] }) }),
      /* @__PURE__ */ jsx(TableBody, { children: [...Array(5)].map((_, i) => /* @__PURE__ */ jsxs(TableRow, { children: [
        /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-4 w-40" }) }),
        /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-4 w-20" }) }),
        /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-5 w-16" }) }),
        /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-4 w-24" }) }),
        /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-8 w-8" }) })
      ] }, i)) })
    ] }) });
  }
  if (usages.length === 0) {
    return /* @__PURE__ */ jsxs(
      "div",
      {
        className: "flex flex-col items-center justify-center rounded-md border border-dashed p-8 text-center",
        "data-cy": sel("patterns.usageTable.empty"),
        children: [
          /* @__PURE__ */ jsx(LayoutGrid, { className: "h-12 w-12 text-muted-foreground/50" }),
          /* @__PURE__ */ jsx("h3", { className: "mt-4 text-lg font-semibold", children: "No Usages Found" }),
          /* @__PURE__ */ jsx("p", { className: "mt-2 text-sm text-muted-foreground", children: emptyMessage })
        ]
      }
    );
  }
  return /* @__PURE__ */ jsx("div", { className: "rounded-md border", "data-cy": sel("patterns.usageTable.container"), children: /* @__PURE__ */ jsxs(Table, { children: [
    /* @__PURE__ */ jsx(TableHeader, { children: /* @__PURE__ */ jsxs(TableRow, { children: [
      /* @__PURE__ */ jsx(TableHead, { children: "Entity" }),
      /* @__PURE__ */ jsx(TableHead, { children: "Type" }),
      /* @__PURE__ */ jsx(TableHead, { children: "Status" }),
      /* @__PURE__ */ jsx(TableHead, { children: "Last Updated" }),
      /* @__PURE__ */ jsx(TableHead, { className: "w-[80px]", children: "Actions" })
    ] }) }),
    /* @__PURE__ */ jsx(TableBody, { children: usages.map((usage) => {
      const Icon = entityIcons[usage.entityType] || FileText;
      const displayName = getEntityDisplayName(usage);
      const entityUrl = `/dashboard/${usage.entityType}/${usage.entityId}`;
      return /* @__PURE__ */ jsxs(TableRow, { "data-cy": sel("patterns.usageTable.row", { id: usage.id }), children: [
        /* @__PURE__ */ jsx(TableCell, { className: "font-medium", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
          /* @__PURE__ */ jsx(Icon, { className: "h-4 w-4 text-muted-foreground" }),
          /* @__PURE__ */ jsx("span", { children: displayName })
        ] }) }),
        /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsx("span", { className: "capitalize", children: usage.entityType }) }),
        /* @__PURE__ */ jsx(TableCell, { children: usage.entityStatus ? /* @__PURE__ */ jsx(Badge, { variant: getStatusVariant(usage.entityStatus), children: usage.entityStatus }) : /* @__PURE__ */ jsx("span", { className: "text-muted-foreground", children: "-" }) }),
        /* @__PURE__ */ jsx(TableCell, { className: "text-muted-foreground", children: formatDate(usage.entityUpdatedAt || usage.createdAt) }),
        /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsx(
          Button,
          {
            variant: "ghost",
            size: "icon",
            asChild: true,
            "data-cy": sel("patterns.usageTable.viewLink", { id: usage.id }),
            children: /* @__PURE__ */ jsx(
              Link,
              {
                href: entityUrl,
                target: "_blank",
                rel: "noopener noreferrer",
                title: "View entity (opens in new tab)",
                children: /* @__PURE__ */ jsx(ExternalLink, { className: "h-4 w-4" })
              }
            )
          }
        ) })
      ] }, usage.id);
    }) })
  ] }) });
}
export {
  PatternUsageTable
};
