"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 alertDialog = require('../../../../../../../ui/src/components/alert-dialog.cjs');
const dialog = require('../../../../../../../ui/src/components/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 SubmissionsPage({ formId }) {
  const { navigate, Link, localization } = context.usePluginOverrides("form-builder", {
    localization: index.FORM_BUILDER_LOCALIZATION
  });
  const basePath = context.useBasePath();
  const { form } = formBuilderHooks.useSuspenseFormById(formId);
  const { submissions, total, hasMore, isLoadingMore, loadMore, refetch } = formBuilderHooks.useSuspenseSubmissions(formId);
  const deleteMutation = formBuilderHooks.useDeleteSubmission(formId);
  const [deleteId, setDeleteId] = React.useState(null);
  const [viewSubmission, setViewSubmission] = 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_SUBMISSION_DELETED);
      setDeleteId(null);
      await refetch();
    } catch (error) {
      sonner.toast.error(loc.FORM_BUILDER_TOAST_ERROR);
    }
  };
  const formatSubmissionData = (data) => {
    const entries = Object.entries(data).slice(0, 3);
    return entries.map(([key, value]) => {
      const strValue = typeof value === "string" ? value : JSON.stringify(value);
      const truncated = strValue.length > 30 ? `${strValue.slice(0, 30)}...` : strValue;
      return `${key}: ${truncated}`;
    }).join(", ");
  };
  return /* @__PURE__ */ jsxRuntime.jsxs(pageWrapper.PageWrapper, { testId: "submissions-page", children: [
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full max-w-5xl space-y-6", children: [
      /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-4", children: [
        /* @__PURE__ */ jsxRuntime.jsx(button.Button, { variant: "ghost", size: "icon", asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(LinkComponent, { href: `${basePath}/forms`, children: /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.ArrowLeft, { className: "h-4 w-4" }) }) }),
        /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
          /* @__PURE__ */ jsxRuntime.jsx("h1", { className: "text-2xl font-bold", children: form?.name || loc.FORM_BUILDER_SUBMISSIONS_TITLE }),
          /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-muted-foreground", children: loc.FORM_BUILDER_SUBMISSIONS_SUBTITLE })
        ] })
      ] }),
      submissions.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(
        emptyState.EmptyState,
        {
          title: loc.FORM_BUILDER_SUBMISSIONS_EMPTY,
          description: loc.FORM_BUILDER_SUBMISSIONS_EMPTY_DESCRIPTION
        }
      ) : /* @__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, { className: "w-24", children: loc.FORM_BUILDER_SUBMISSIONS_COLUMN_ID }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { children: loc.FORM_BUILDER_SUBMISSIONS_COLUMN_DATA }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { children: loc.FORM_BUILDER_SUBMISSIONS_COLUMN_SUBMITTED_AT }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { children: loc.FORM_BUILDER_SUBMISSIONS_COLUMN_IP_ADDRESS }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { className: "w-24", children: loc.FORM_BUILDER_SUBMISSIONS_COLUMN_ACTIONS })
          ] }) }),
          /* @__PURE__ */ jsxRuntime.jsx(table.TableBody, { children: submissions.map((sub) => /* @__PURE__ */ jsxRuntime.jsxs(table.TableRow, { children: [
            /* @__PURE__ */ jsxRuntime.jsxs(table.TableCell, { className: "font-mono text-xs", children: [
              sub.id.slice(0, 8),
              "..."
            ] }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { className: "max-w-xs truncate text-sm text-muted-foreground", children: formatSubmissionData(sub.parsedData ?? {}) }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { className: "text-muted-foreground", children: new Date(sub.submittedAt).toLocaleString() }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { className: "text-muted-foreground font-mono text-xs", children: sub.ipAddress || "-" }),
            /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex gap-1", children: [
              /* @__PURE__ */ jsxRuntime.jsxs(
                button.Button,
                {
                  variant: "ghost",
                  size: "icon",
                  onClick: () => setViewSubmission(sub),
                  children: [
                    /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Eye, { className: "h-4 w-4" }),
                    /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "View" })
                  ]
                }
              ),
              /* @__PURE__ */ jsxRuntime.jsxs(
                button.Button,
                {
                  variant: "ghost",
                  size: "icon",
                  className: "text-destructive",
                  onClick: () => setDeleteId(sub.id),
                  children: [
                    /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Trash2, { className: "h-4 w-4" }),
                    /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Delete" })
                  ]
                }
              )
            ] }) })
          ] }, sub.id)) })
        ] }) }),
        /* @__PURE__ */ jsxRuntime.jsx(
          pagination.Pagination,
          {
            total,
            showing: submissions.length,
            hasMore,
            isLoadingMore,
            onLoadMore: loadMore
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntime.jsx(
      dialog.Dialog,
      {
        open: !!viewSubmission,
        onOpenChange: () => setViewSubmission(null),
        children: /* @__PURE__ */ jsxRuntime.jsxs(dialog.DialogContent, { className: "max-w-2xl max-h-[80vh] overflow-auto", children: [
          /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogHeader, { children: /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogTitle, { children: "Submission Details" }) }),
          viewSubmission && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "space-y-4", children: [
            /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4 text-sm", children: [
              /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
                /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-muted-foreground", children: "ID:" }),
                /* @__PURE__ */ jsxRuntime.jsx("p", { className: "font-mono truncate", children: viewSubmission.id })
              ] }),
              /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
                /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-muted-foreground", children: "Submitted:" }),
                /* @__PURE__ */ jsxRuntime.jsx("p", { className: "truncate", children: new Date(viewSubmission.submittedAt).toLocaleString() })
              ] }),
              /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
                /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-muted-foreground", children: "IP Address:" }),
                /* @__PURE__ */ jsxRuntime.jsx("p", { className: "font-mono truncate", children: viewSubmission.ipAddress || "-" })
              ] }),
              /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
                /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-muted-foreground", children: "User Agent:" }),
                /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-xs truncate", children: viewSubmission.userAgent || "-" })
              ] })
            ] }),
            /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
              /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-muted-foreground text-sm", children: "Data:" }),
              /* @__PURE__ */ jsxRuntime.jsx("pre", { className: "mt-2 p-4 bg-muted rounded-lg text-sm overflow-auto", children: JSON.stringify(viewSubmission.parsedData, null, 2) })
            ] })
          ] })
        ] })
      }
    ),
    /* @__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 Submission" }),
        /* @__PURE__ */ jsxRuntime.jsx(alertDialog.AlertDialogDescription, { children: loc.FORM_BUILDER_SUBMISSIONS_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.SubmissionsPage = SubmissionsPage;
