"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 formBuilderHooks = require('../../hooks/form-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 FormListPage() {
  const { navigate, Link, localization } = context.usePluginOverrides("form-builder", {
    localization: index.FORM_BUILDER_LOCALIZATION
  });
  const basePath = context.useBasePath();
  const { forms, total, hasMore, isLoadingMore, loadMore, refetch } = formBuilderHooks.useSuspenseForms();
  const deleteMutation = formBuilderHooks.useDeleteForm();
  const [deleteId, setDeleteId] = React.useState(null);
  const loc = localization || index.FORM_BUILDER_LOCALIZATION;
  const LinkComponent = Link || "a";
  const handleDelete = async () => {
    if (!deleteId) return;
    try {
      await deleteMutation.mutateAsync(deleteId);
      sonner.toast.success(loc.FORM_BUILDER_TOAST_DELETE_SUCCESS);
      setDeleteId(null);
      await refetch();
    } catch (error) {
      sonner.toast.error(loc.FORM_BUILDER_TOAST_ERROR);
    }
  };
  const getStatusBadge = (status) => {
    const colors = {
      active: "bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200",
      inactive: "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.inactive}`,
        children: status
      }
    );
  };
  return /* @__PURE__ */ jsxRuntime.jsxs(pageWrapper.PageWrapper, { testId: "form-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.FORM_BUILDER_LIST_TITLE }),
          /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-muted-foreground", children: loc.FORM_BUILDER_LIST_SUBTITLE })
        ] }),
        /* @__PURE__ */ jsxRuntime.jsx(button.Button, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs(LinkComponent, { href: `${basePath}/forms/new`, children: [
          /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Plus, { className: "mr-2 h-4 w-4" }),
          loc.FORM_BUILDER_BUTTON_NEW_FORM
        ] }) })
      ] }),
      forms.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(
        emptyState.EmptyState,
        {
          title: loc.FORM_BUILDER_LIST_EMPTY,
          description: loc.FORM_BUILDER_LIST_EMPTY_DESCRIPTION,
          action: /* @__PURE__ */ jsxRuntime.jsx(button.Button, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs(LinkComponent, { href: `${basePath}/forms/new`, children: [
            /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Plus, { className: "mr-2 h-4 w-4" }),
            loc.FORM_BUILDER_BUTTON_NEW_FORM
          ] }) })
        }
      ) : /* @__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.FORM_BUILDER_LIST_COLUMN_NAME }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { children: loc.FORM_BUILDER_LIST_COLUMN_SLUG }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { children: loc.FORM_BUILDER_LIST_COLUMN_STATUS }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { children: loc.FORM_BUILDER_LIST_COLUMN_CREATED }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { className: "w-12", children: loc.FORM_BUILDER_LIST_COLUMN_ACTIONS })
          ] }) }),
          /* @__PURE__ */ jsxRuntime.jsx(table.TableBody, { children: forms.map((form) => /* @__PURE__ */ jsxRuntime.jsxs(table.TableRow, { children: [
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { className: "font-medium", children: form.name }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { className: "text-muted-foreground font-mono text-sm", children: form.slug }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { children: getStatusBadge(form.status) }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { className: "text-muted-foreground", children: new Date(form.createdAt).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}/forms/${form.id}/edit`),
                    children: [
                      /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Pencil, { className: "mr-2 h-4 w-4" }),
                      loc.FORM_BUILDER_LIST_ACTION_EDIT
                    ]
                  }
                ),
                /* @__PURE__ */ jsxRuntime.jsxs(
                  dropdownMenu.DropdownMenuItem,
                  {
                    onClick: () => navigate?.(
                      `${basePath}/forms/${form.id}/submissions`
                    ),
                    children: [
                      /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.FileText, { className: "mr-2 h-4 w-4" }),
                      loc.FORM_BUILDER_LIST_ACTION_SUBMISSIONS
                    ]
                  }
                ),
                /* @__PURE__ */ jsxRuntime.jsxs(
                  dropdownMenu.DropdownMenuItem,
                  {
                    className: "text-destructive",
                    onClick: () => setDeleteId(form.id),
                    children: [
                      /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Trash2, { className: "mr-2 h-4 w-4" }),
                      loc.FORM_BUILDER_LIST_ACTION_DELETE
                    ]
                  }
                )
              ] })
            ] }) })
          ] }, form.id)) })
        ] }) }),
        /* @__PURE__ */ jsxRuntime.jsx(
          pagination.Pagination,
          {
            total,
            showing: forms.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: "Delete Form" }),
        /* @__PURE__ */ jsxRuntime.jsx(alertDialog.AlertDialogDescription, { children: loc.FORM_BUILDER_EDITOR_DELETE_CONFIRM })
      ] }),
      /* @__PURE__ */ jsxRuntime.jsxs(alertDialog.AlertDialogFooter, { children: [
        /* @__PURE__ */ jsxRuntime.jsx(alertDialog.AlertDialogCancel, { children: loc.FORM_BUILDER_BUTTON_CANCEL }),
        /* @__PURE__ */ jsxRuntime.jsx(
          alertDialog.AlertDialogAction,
          {
            onClick: handleDelete,
            className: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
            children: deleteMutation.isPending ? loc.FORM_BUILDER_STATUS_DELETING : loc.FORM_BUILDER_BUTTON_DELETE
          }
        )
      ] })
    ] }) })
  ] });
}

exports.FormListPage = FormListPage;
