"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import Link from "next/link";
import { Page, PageBody } from "@open-mercato/ui/backend/Page";
import { DataTable } from "@open-mercato/ui/backend/DataTable";
import { Button } from "@open-mercato/ui/primitives/button";
import { RowActions } from "@open-mercato/ui/backend/RowActions";
import { apiCall, readApiResultOrThrow, withScopedApiRequestHeaders } from "@open-mercato/ui/backend/utils/apiCall";
import { buildOptimisticLockHeader } from "@open-mercato/ui/backend/utils/optimisticLock";
import { flash } from "@open-mercato/ui/backend/FlashMessages";
import { raiseCrudError } from "@open-mercato/ui/backend/utils/serverErrors";
import { useOrganizationScopeVersion } from "@open-mercato/shared/lib/frontend/useOrganizationScope";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { useConfirmDialog } from "@open-mercato/ui/backend/confirm-dialog";
import { ListEmptyState } from "@open-mercato/ui/backend/filters/ListEmptyState";
function RolesListPage() {
  const { confirm, ConfirmDialogElement } = useConfirmDialog();
  const [sorting, setSorting] = React.useState([{ id: "name", desc: false }]);
  const [page, setPage] = React.useState(1);
  const [total, setTotal] = React.useState(0);
  const [totalPages, setTotalPages] = React.useState(1);
  const [search, setSearch] = React.useState("");
  const [rows, setRows] = React.useState([]);
  const [isLoading, setIsLoading] = React.useState(true);
  const [reloadToken, setReloadToken] = React.useState(0);
  const [isSuperAdmin, setIsSuperAdmin] = React.useState(false);
  const scopeVersion = useOrganizationScopeVersion();
  const t = useT();
  React.useEffect(() => {
    let cancelled = false;
    async function load() {
      setIsLoading(true);
      try {
        const params = new URLSearchParams();
        params.set("page", String(page));
        params.set("pageSize", "50");
        if (search) params.set("search", search);
        const fallback = { items: [], total: 0, totalPages: 1, isSuperAdmin: false };
        const j = await readApiResultOrThrow(
          `/api/auth/roles?${params.toString()}`,
          void 0,
          { errorMessage: t("auth.roles.list.error.load", "Failed to load roles"), fallback }
        );
        if (!cancelled) {
          setRows(j.items || []);
          setTotal(j.total || 0);
          setTotalPages(j.totalPages || 1);
          setIsSuperAdmin(!!j.isSuperAdmin);
        }
      } finally {
        if (!cancelled) setIsLoading(false);
      }
    }
    load();
    return () => {
      cancelled = true;
    };
  }, [page, search, reloadToken, scopeVersion, t]);
  const handleDelete = React.useCallback(async (row) => {
    const confirmed = await confirm({
      title: t("auth.roles.list.confirmDelete", 'Delete role "{{name}}"?').replace("{{name}}", row.name),
      variant: "destructive"
    });
    if (!confirmed) return;
    try {
      const call = await withScopedApiRequestHeaders(
        buildOptimisticLockHeader(row.updatedAt),
        () => apiCall(
          `/api/auth/roles?id=${encodeURIComponent(row.id)}`,
          { method: "DELETE" }
        )
      );
      if (!call.ok) {
        await raiseCrudError(call.response, t("auth.roles.list.error.delete", "Failed to delete role"));
      }
      flash(t("auth.roles.list.success.delete", "Role deleted"), "success");
      setReloadToken((token) => token + 1);
    } catch (error) {
      const message = error instanceof Error ? error.message : t("auth.roles.list.error.delete", "Failed to delete role");
      flash(message, "error");
    }
  }, [confirm, t]);
  const showTenantColumn = React.useMemo(
    () => isSuperAdmin && rows.some((row) => row.tenantName),
    [isSuperAdmin, rows]
  );
  const columns = React.useMemo(() => {
    const base = [
      { accessorKey: "name", header: t("auth.roles.list.columns.role", "Role") },
      { accessorKey: "usersCount", header: t("auth.roles.list.columns.users", "Users") }
    ];
    if (showTenantColumn) {
      base.splice(1, 0, { accessorKey: "tenantName", header: t("auth.roles.list.columns.tenant", "Tenant") });
    }
    return base;
  }, [showTenantColumn, t]);
  return /* @__PURE__ */ jsxs(Page, { children: [
    /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(
      DataTable,
      {
        title: t("auth.roles.list.title", "Roles"),
        actions: /* @__PURE__ */ jsx(Button, { asChild: true, children: /* @__PURE__ */ jsx(Link, { href: "/backend/roles/create", children: t("auth.roles.list.actions.create", "Create") }) }),
        columns,
        data: rows,
        searchValue: search,
        onSearchChange: (v) => {
          setSearch(v);
          setPage(1);
        },
        rowActions: (row) => /* @__PURE__ */ jsx(RowActions, { items: [
          { id: "edit", label: t("common.edit", "Edit"), href: `/backend/roles/${row.id}/edit` },
          { id: "show-users", label: t("auth.roles.list.actions.showUsers", "Show users"), href: `/backend/users?roleId=${encodeURIComponent(row.id)}` },
          { id: "delete", label: t("common.delete", "Delete"), destructive: true, onSelect: () => {
            void handleDelete(row);
          } }
        ] }),
        sortable: true,
        sorting,
        onSortingChange: setSorting,
        perspective: { tableId: "auth.roles.list" },
        emptyState: /* @__PURE__ */ jsx(
          ListEmptyState,
          {
            entityName: t("auth.roles.list.title", "Roles"),
            createHref: "/backend/roles/create",
            createLabel: t("auth.roles.list.actions.create", "Create")
          }
        ),
        pagination: { page, pageSize: 50, total, totalPages, onPageChange: setPage },
        isLoading
      }
    ) }),
    ConfirmDialogElement
  ] });
}
export {
  RolesListPage as default
};
//# sourceMappingURL=page.js.map
