"use client";
'use strict';

const jsxRuntime = require('react/jsx-runtime');
const LucideIcons = require('lucide-react');
const button = require('../../../../../../../ui/src/components/button.cjs');
const table = require('../../../../../../../ui/src/components/table.cjs');
const context = require('@btst/stack/context');
const emptyState = require('../shared/empty-state.cjs');
const pageWrapper = require('../shared/page-wrapper.cjs');
const index = require('../../localization/index.cjs');
const useRouteLifecycle = require('../../../../../../../ui/src/hooks/use-route-lifecycle.cjs');
const sonner = require('sonner');
const cmsHooks = require('../../hooks/cms-hooks.cjs');

function ContentListPage({ typeSlug }) {
  const overrides = context.usePluginOverrides("cms");
  const { navigate, Link } = overrides;
  const localization = { ...index.CMS_LOCALIZATION, ...overrides.localization };
  const basePath = context.useBasePath();
  useRouteLifecycle.useRouteLifecycle({
    routeName: "contentList",
    context: {
      path: `/cms/${typeSlug}`,
      params: { typeSlug },
      isSSR: typeof window === "undefined"
    },
    overrides,
    beforeRenderHook: (overrides2, context) => {
      if (overrides2.onBeforeListRendered) {
        return overrides2.onBeforeListRendered(typeSlug, context);
      }
      return true;
    }
  });
  const limit = 20;
  const { contentTypes } = cmsHooks.useSuspenseContentTypes();
  const contentType = contentTypes.find((ct) => ct.slug === typeSlug);
  const { items, total, refetch, loadMore, hasMore, isLoadingMore } = cmsHooks.useSuspenseContent(typeSlug, {
    limit
  });
  const deleteContent = cmsHooks.useDeleteContent(typeSlug);
  const LinkComponent = Link || "a";
  const handleDelete = async (id) => {
    try {
      await deleteContent.mutateAsync(id);
      sonner.toast.success(localization.CMS_TOAST_DELETE_SUCCESS);
      void refetch();
    } catch {
      sonner.toast.error(localization.CMS_TOAST_ERROR);
    }
  };
  const formatDate = (dateString) => {
    return new Date(dateString).toLocaleDateString();
  };
  if (!contentType) {
    return /* @__PURE__ */ jsxRuntime.jsx(pageWrapper.PageWrapper, { testId: "cms-list-page", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full max-w-5xl", children: /* @__PURE__ */ jsxRuntime.jsx(
      emptyState.EmptyState,
      {
        title: localization.CMS_ERROR_NOT_FOUND,
        description: "Content type not found"
      }
    ) }) });
  }
  return /* @__PURE__ */ jsxRuntime.jsx(pageWrapper.PageWrapper, { testId: "cms-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", { className: "flex items-center gap-4", children: [
        /* @__PURE__ */ jsxRuntime.jsx(
          button.Button,
          {
            variant: "ghost",
            size: "icon",
            onClick: () => navigate(`${basePath}/cms`),
            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 tracking-tight", children: contentType.name }),
          contentType.description && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-muted-foreground", children: contentType.description })
        ] })
      ] }),
      /* @__PURE__ */ jsxRuntime.jsxs(button.Button, { onClick: () => navigate(`${basePath}/cms/${typeSlug}/new`), children: [
        /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Plus, { className: "h-4 w-4 mr-2" }),
        localization.CMS_BUTTON_NEW_ITEM
      ] })
    ] }),
    items.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx(
      emptyState.EmptyState,
      {
        title: localization.CMS_LIST_EMPTY,
        description: localization.CMS_LIST_EMPTY_DESCRIPTION,
        action: /* @__PURE__ */ jsxRuntime.jsxs(
          button.Button,
          {
            onClick: () => navigate(`${basePath}/cms/${typeSlug}/new`),
            children: [
              /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Plus, { className: "h-4 w-4 mr-2" }),
              localization.CMS_BUTTON_CREATE
            ]
          }
        )
      }
    ) : /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "border rounded-lg", 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: localization.CMS_LIST_COLUMN_SLUG }),
          /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { children: localization.CMS_LIST_COLUMN_CREATED }),
          /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { children: localization.CMS_LIST_COLUMN_UPDATED }),
          /* @__PURE__ */ jsxRuntime.jsx(table.TableHead, { className: "w-[100px]", children: localization.CMS_LIST_COLUMN_ACTIONS })
        ] }) }),
        /* @__PURE__ */ jsxRuntime.jsx(table.TableBody, { children: items.map((item) => /* @__PURE__ */ jsxRuntime.jsxs(table.TableRow, { children: [
          /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { className: "font-medium", children: /* @__PURE__ */ jsxRuntime.jsx(
            LinkComponent,
            {
              href: `${basePath}/cms/${typeSlug}/${item.id}`,
              className: "hover:underline",
              children: item.slug
            }
          ) }),
          /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { children: formatDate(item.createdAt) }),
          /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { children: formatDate(item.updatedAt) }),
          /* @__PURE__ */ jsxRuntime.jsx(table.TableCell, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1", children: [
            /* @__PURE__ */ jsxRuntime.jsx(
              button.Button,
              {
                variant: "ghost",
                size: "icon",
                onClick: () => navigate(`${basePath}/cms/${typeSlug}/${item.id}`),
                children: /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Pencil, { className: "h-4 w-4" })
              }
            ),
            /* @__PURE__ */ jsxRuntime.jsx(
              button.Button,
              {
                variant: "ghost",
                size: "icon",
                onClick: () => handleDelete(item.id),
                disabled: deleteContent.isPending,
                children: /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Trash2, { className: "h-4 w-4 text-destructive" })
              }
            )
          ] }) })
        ] }, item.id)) })
      ] }),
      /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between px-4 py-3 border-t", children: [
        /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-sm text-muted-foreground", children: localization.CMS_LIST_PAGINATION_SHOWING.replace("{from}", "1").replace("{to}", String(items.length)).replace("{total}", String(total)) }),
        hasMore && /* @__PURE__ */ jsxRuntime.jsxs(
          button.Button,
          {
            variant: "outline",
            size: "sm",
            onClick: () => loadMore(),
            disabled: isLoadingMore,
            children: [
              isLoadingMore && /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Loader2, { className: "h-4 w-4 mr-2 animate-spin" }),
              localization.CMS_LIST_PAGINATION_NEXT
            ]
          }
        )
      ] })
    ] })
  ] }) });
}

exports.ContentListPage = ContentListPage;
