"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { usePatternUsages } from "@nextsparkjs/core/hooks/usePatternUsages";
import { PatternUsageStats } from "./PatternUsageStats.js";
import { PatternUsageTable } from "./PatternUsageTable.js";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "@nextsparkjs/core/components/ui/select";
import { Button } from "@nextsparkjs/core/components/ui/button";
import { Alert, AlertDescription, AlertTitle } from "@nextsparkjs/core/components/ui/alert";
import { ChevronLeft, ChevronRight, AlertCircle, RefreshCw } from "lucide-react";
import { sel } from "@nextsparkjs/core/lib/test";
function PatternUsageReport({
  patternId,
  pageSize = 20
}) {
  const [entityTypeFilter, setEntityTypeFilter] = useState("all");
  const [page, setPage] = useState(1);
  const {
    usages,
    counts,
    total,
    totalPages,
    isLoading,
    isError,
    error,
    refetch
  } = usePatternUsages(patternId, {
    entityType: entityTypeFilter === "all" ? void 0 : entityTypeFilter,
    limit: pageSize,
    page
  });
  const handleFilterChange = (value) => {
    setEntityTypeFilter(value);
    setPage(1);
  };
  const handlePrevPage = () => setPage((p) => Math.max(1, p - 1));
  const handleNextPage = () => setPage((p) => Math.min(totalPages, p + 1));
  if (isError) {
    return /* @__PURE__ */ jsxs(Alert, { variant: "destructive", "data-cy": sel("patterns.usageReport.error"), children: [
      /* @__PURE__ */ jsx(AlertCircle, { className: "h-4 w-4" }),
      /* @__PURE__ */ jsx(AlertTitle, { children: "Error Loading Usages" }),
      /* @__PURE__ */ jsxs(AlertDescription, { className: "flex items-center justify-between", children: [
        /* @__PURE__ */ jsx("span", { children: error instanceof Error ? error.message : "Failed to load pattern usages" }),
        /* @__PURE__ */ jsxs(Button, { variant: "outline", size: "sm", onClick: () => refetch(), children: [
          /* @__PURE__ */ jsx(RefreshCw, { className: "mr-2 h-4 w-4" }),
          "Retry"
        ] })
      ] })
    ] });
  }
  return /* @__PURE__ */ jsxs("div", { className: "space-y-6", "data-cy": sel("patterns.usageReport.container"), children: [
    /* @__PURE__ */ jsx(
      PatternUsageStats,
      {
        counts,
        total,
        isLoading
      }
    ),
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", "data-cy": sel("patterns.usageReport.filters"), children: [
      /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-4", children: [
        /* @__PURE__ */ jsx("span", { className: "text-sm text-muted-foreground", children: "Filter by type:" }),
        /* @__PURE__ */ jsxs(Select, { value: entityTypeFilter, onValueChange: handleFilterChange, children: [
          /* @__PURE__ */ jsx(SelectTrigger, { className: "w-[180px]", "data-cy": sel("patterns.usageReport.filterSelect"), children: /* @__PURE__ */ jsx(SelectValue, { placeholder: "All entity types" }) }),
          /* @__PURE__ */ jsxs(SelectContent, { children: [
            /* @__PURE__ */ jsx(SelectItem, { value: "all", children: "All entity types" }),
            counts.map((item) => /* @__PURE__ */ jsxs(SelectItem, { value: item.entityType, children: [
              item.entityType.charAt(0).toUpperCase() + item.entityType.slice(1),
              " (",
              item.count,
              ")"
            ] }, item.entityType))
          ] })
        ] })
      ] }),
      totalPages > 1 && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", "data-cy": sel("patterns.usageReport.pagination"), children: [
        /* @__PURE__ */ jsx(
          Button,
          {
            variant: "outline",
            size: "sm",
            onClick: handlePrevPage,
            disabled: page === 1 || isLoading,
            "data-cy": sel("patterns.usageReport.prevPage"),
            children: /* @__PURE__ */ jsx(ChevronLeft, { className: "h-4 w-4" })
          }
        ),
        /* @__PURE__ */ jsxs("span", { className: "text-sm text-muted-foreground", children: [
          "Page ",
          page,
          " of ",
          totalPages
        ] }),
        /* @__PURE__ */ jsx(
          Button,
          {
            variant: "outline",
            size: "sm",
            onClick: handleNextPage,
            disabled: page >= totalPages || isLoading,
            "data-cy": sel("patterns.usageReport.nextPage"),
            children: /* @__PURE__ */ jsx(ChevronRight, { className: "h-4 w-4" })
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsx(
      PatternUsageTable,
      {
        usages,
        isLoading,
        emptyMessage: entityTypeFilter !== "all" ? `This pattern is not used in any ${entityTypeFilter}` : "This pattern is not used in any entities yet"
      }
    ),
    !isLoading && usages.length > 0 && /* @__PURE__ */ jsxs("p", { className: "text-sm text-muted-foreground", "data-cy": sel("patterns.usageReport.resultsInfo"), children: [
      "Showing ",
      (page - 1) * pageSize + 1,
      " to ",
      Math.min(page * pageSize, total),
      " of ",
      total,
      " usages"
    ] })
  ] });
}
export {
  PatternUsageReport
};
