"use client";
'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const context = require('@btst/stack/context');
const button = require('../../../../../../../ui/src/components/button.cjs');
const table = require('../../../../../../../ui/src/components/table.cjs');
const dropdownMenu = require('../../../../../../../ui/src/components/dropdown-menu.cjs');
const alertDialog = require('../../../../../../../ui/src/components/alert-dialog.cjs');
const LucideIcons = require('lucide-react');
const sonner = require('sonner');
const uiBuilderHooks = require('../../hooks/ui-builder-hooks.cjs');
const index = require('../../localization/index.cjs');
const pageWrapper = require('../shared/page-wrapper.cjs');
const emptyState = require('../shared/empty-state.cjs');
const pagination = require('../shared/pagination.cjs');

function PageListPage() {
  const { navigate, Link } = context.usePluginOverrides("ui-builder");
  const basePath = context.useBasePath();
  const { pages, total, hasMore, isLoadingMore, loadMore, refetch } = uiBuilderHooks.useSuspenseUIBuilderPages();
  const deleteMutation = uiBuilderHooks.useDeleteUIBuilderPage();
  const [deleteId, setDeleteId] = React.useState(null);
  const loc = index.uiBuilderLocalization;
  const LinkComponent = Link || "a";
  const handleDelete = async () => {
    if (!deleteId) return;
    try {
      await deleteMutation.mutateAsync(deleteId);
      sonner.toast.success("Page deleted successfully");
      setDeleteId(null);
      await refetch();
    } catch {
      sonner.toast.error("Failed to delete page");
    }
  };
  const getStatusBadge = (status) => {
    const colors = {
      published: "bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200",
      draft: "bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200",
      archived: "bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-200"
    };
    return /* @__PURE__ */ jsxRuntime.jsx(
      "span",
      {
        className: `px-2 py-1 rounded-full text-xs font-medium ${colors[status] || colors.draft}`,
        children: loc.pageBuilder.statusOptions[status] || status
      }
    );
  };
  return /* @__PURE__ */ jsxRuntime.jsxs(pageWrapper.PageWrapper, { testId: "page-list-page", children: [
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full max-w-5xl space-y-6", children: [
      /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between", children: [
        /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
          /* @__PURE__ */ jsxRuntime.jsx("h1", { className: "text-2xl font-bold", children: loc.pageList.title }),
          /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-muted-foreground", children: loc.pageList.description })
        ] }),
        /* @__PURE__ */ jsxRuntime.jsx(button.Button, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs(LinkComponent, { href: `${basePath}/ui-builder/new`, children: [
          /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Plus, { className: "mr-2 h-4 w-4" }),
          loc.pageList.createButton
        ] }) })
      ] }),
      pages.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(
        emptyState.EmptyState,
        {
          title: loc.pageList.emptyState.title,
          description: loc.pageList.emptyState.description,
          action: /* @__PURE__ */ jsxRuntime.jsx(button.Button, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs(LinkComponent, { href: `${basePath}/ui-builder/new`, children: [
            /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Plus, { className: "mr-2 h-4 w-4" }),
            loc.pageList.createButton
          ] }) })
        }
      ) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
        /* @__PURE__ */ jsxRuntime.jsx("div", { className: "rounded-lg border", children: /* @__PURE__ */ jsxRuntime.jsxs(table.Table, { children: [
          /* @__PURE__ */ jsxRuntime.jsx(table.TableHeader, { children: /* @__PURE__ */ jsxRuntime.jsxs(table.TableRow, { children: [
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { children: loc.pageList.columns.slug }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { children: loc.pageList.columns.status }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { children: loc.pageList.columns.updatedAt }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { className: "w-12", children: loc.pageList.columns.actions })
          ] }) }),
          /* @__PURE__ */ jsxRuntime.jsx(table.TableBody, { children: pages.map((page) => /* @__PURE__ */ jsxRuntime.jsxs(table.TableRow, { children: [
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { className: "font-mono text-sm", children: page.slug }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { children: getStatusBadge(page.parsedData.status) }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { className: "text-muted-foreground", children: new Date(page.updatedAt).toLocaleDateString() }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { children: /* @__PURE__ */ jsxRuntime.jsxs(dropdownMenu.DropdownMenu, { children: [
              /* @__PURE__ */ jsxRuntime.jsx(dropdownMenu.DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs(button.Button, { variant: "ghost", size: "icon", children: [
                /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.MoreHorizontal, { className: "h-4 w-4" }),
                /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Actions" })
              ] }) }),
              /* @__PURE__ */ jsxRuntime.jsxs(dropdownMenu.DropdownMenuContent, { align: "end", children: [
                /* @__PURE__ */ jsxRuntime.jsxs(
                  dropdownMenu.DropdownMenuItem,
                  {
                    onClick: () => navigate?.(
                      `${basePath}/ui-builder/${page.id}/edit`
                    ),
                    children: [
                      /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Pencil, { className: "mr-2 h-4 w-4" }),
                      loc.pageList.actions.edit
                    ]
                  }
                ),
                /* @__PURE__ */ jsxRuntime.jsxs(
                  dropdownMenu.DropdownMenuItem,
                  {
                    className: "text-destructive",
                    onClick: () => setDeleteId(page.id),
                    children: [
                      /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Trash2, { className: "mr-2 h-4 w-4" }),
                      loc.pageList.actions.delete
                    ]
                  }
                )
              ] })
            ] }) })
          ] }, page.id)) })
        ] }) }),
        /* @__PURE__ */ jsxRuntime.jsx(
          pagination.Pagination,
          {
            total,
            showing: pages.length,
            hasMore,
            isLoadingMore,
            onLoadMore: loadMore
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntime.jsx(alertDialog.AlertDialog, { open: !!deleteId, onOpenChange: () => setDeleteId(null), children: /* @__PURE__ */ jsxRuntime.jsxs(alertDialog.AlertDialogContent, { children: [
      /* @__PURE__ */ jsxRuntime.jsxs(alertDialog.AlertDialogHeader, { children: [
        /* @__PURE__ */ jsxRuntime.jsx(alertDialog.AlertDialogTitle, { children: loc.pageList.deleteDialog.title }),
        /* @__PURE__ */ jsxRuntime.jsx(alertDialog.AlertDialogDescription, { children: loc.pageList.deleteDialog.description })
      ] }),
      /* @__PURE__ */ jsxRuntime.jsxs(alertDialog.AlertDialogFooter, { children: [
        /* @__PURE__ */ jsxRuntime.jsx(alertDialog.AlertDialogCancel, { children: loc.pageList.deleteDialog.cancel }),
        /* @__PURE__ */ jsxRuntime.jsx(
          alertDialog.AlertDialogAction,
          {
            onClick: handleDelete,
            className: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
            children: deleteMutation.isPending ? "Deleting..." : loc.pageList.deleteDialog.confirm
          }
        )
      ] })
    ] }) })
  ] });
}

exports.PageListPage = PageListPage;
