"use client";
import { jsx } from "react/jsx-runtime";
import * as React from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { Page, PageBody } from "@open-mercato/ui/backend/Page";
import { DataTable, withDataTableNamespaces } from "@open-mercato/ui/backend/DataTable";
import { ListEmptyState } from "@open-mercato/ui/backend/filters/ListEmptyState";
import { Badge } from "@open-mercato/ui/primitives/badge";
import { Button } from "@open-mercato/ui/primitives/button";
import { readApiResultOrThrow } from "@open-mercato/ui/backend/utils/apiCall";
import { useOrganizationScopeVersion } from "@open-mercato/shared/lib/frontend/useOrganizationScope";
import { useT } from "@open-mercato/shared/lib/i18n/context";
const PAGE_SIZE = 50;
function StaffLeaveRequestsPage() {
  const t = useT();
  const router = useRouter();
  const scopeVersion = useOrganizationScopeVersion();
  const [rows, setRows] = React.useState([]);
  const [page, setPage] = React.useState(1);
  const [total, setTotal] = React.useState(0);
  const [totalPages, setTotalPages] = React.useState(1);
  const [search, setSearch] = React.useState("");
  const [isLoading, setIsLoading] = React.useState(true);
  const [sorting, setSorting] = React.useState([{ id: "startDate", desc: true }]);
  const labels = React.useMemo(() => ({
    title: t("staff.leaveRequests.page.title", "Leave requests"),
    table: {
      member: t("staff.leaveRequests.table.member", "Team member"),
      dates: t("staff.leaveRequests.table.dates", "Dates"),
      status: t("staff.leaveRequests.table.status", "Status"),
      reason: t("staff.leaveRequests.table.reason", "Reason"),
      updatedAt: t("staff.leaveRequests.table.updatedAt", "Updated"),
      empty: t("staff.leaveRequests.table.empty", "No leave requests yet."),
      search: t("staff.leaveRequests.table.search", "Search leave requests...")
    },
    actions: {
      add: t("staff.leaveRequests.actions.add", "New request"),
      refresh: t("staff.leaveRequests.actions.refresh", "Refresh")
    },
    errors: {
      load: t("staff.leaveRequests.errors.load", "Failed to load leave requests.")
    }
  }), [t]);
  const columns = React.useMemo(() => [
    {
      accessorKey: "memberName",
      header: labels.table.member,
      meta: { priority: 1, sticky: true },
      cell: ({ row }) => row.original.memberName ?? "-"
    },
    {
      accessorKey: "startDate",
      header: labels.table.dates,
      meta: { priority: 2 },
      cell: ({ row }) => formatDateRange(row.original.startDate, row.original.endDate)
    },
    {
      accessorKey: "status",
      header: labels.table.status,
      meta: { priority: 3 },
      cell: ({ row }) => /* @__PURE__ */ jsx(Badge, { variant: resolveStatusVariant(row.original.status), children: t(`staff.leaveRequests.status.${row.original.status}`, row.original.status) })
    },
    {
      accessorKey: "reason",
      header: labels.table.reason,
      meta: { priority: 4, truncate: true, maxWidth: "240px" },
      cell: ({ row }) => row.original.reason ?? "-"
    },
    {
      accessorKey: "updatedAt",
      header: labels.table.updatedAt,
      meta: { priority: 5 },
      cell: ({ row }) => formatDateLabel(row.original.updatedAt)
    }
  ], [labels, t]);
  const loadRows = React.useCallback(async () => {
    setIsLoading(true);
    try {
      const params = new URLSearchParams({
        page: String(page),
        pageSize: String(PAGE_SIZE)
      });
      if (search.trim().length) params.set("search", search.trim());
      const activeSort = sorting[0];
      if (activeSort?.id) {
        params.set("sortField", activeSort.id);
        params.set("sortDir", activeSort.desc ? "desc" : "asc");
      }
      const payload = await readApiResultOrThrow(
        `/api/staff/leave-requests?${params.toString()}`,
        void 0,
        { errorMessage: labels.errors.load }
      );
      const items = Array.isArray(payload.items) ? payload.items : [];
      setRows(items.map(mapLeaveRequest));
      setTotal(typeof payload.total === "number" ? payload.total : items.length);
      setTotalPages(typeof payload.totalPages === "number" ? payload.totalPages : 1);
    } catch {
      setRows([]);
      setTotal(0);
      setTotalPages(1);
    } finally {
      setIsLoading(false);
    }
  }, [labels.errors.load, page, search, sorting]);
  React.useEffect(() => {
    void loadRows();
  }, [loadRows, scopeVersion]);
  const handleSearchChange = React.useCallback((value) => {
    setSearch(value);
    setPage(1);
  }, []);
  return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(
    DataTable,
    {
      title: labels.title,
      data: rows,
      columns,
      isLoading,
      searchValue: search,
      onSearchChange: handleSearchChange,
      searchPlaceholder: labels.table.search,
      emptyState: /* @__PURE__ */ jsx(
        ListEmptyState,
        {
          entityName: labels.title,
          createHref: "/backend/staff/leave-requests/create",
          createLabel: labels.actions.add
        }
      ),
      actions: /* @__PURE__ */ jsx(Button, { asChild: true, size: "sm", children: /* @__PURE__ */ jsx(Link, { href: "/backend/staff/leave-requests/create", children: labels.actions.add }) }),
      refreshButton: {
        label: labels.actions.refresh,
        onRefresh: loadRows,
        isRefreshing: isLoading
      },
      sortable: true,
      sorting,
      onSortingChange: setSorting,
      pagination: {
        page,
        pageSize: PAGE_SIZE,
        total,
        totalPages,
        onPageChange: setPage
      },
      onRowClick: (row) => {
        router.push(`/backend/staff/leave-requests/${encodeURIComponent(row.id)}`);
      }
    }
  ) }) });
}
function mapLeaveRequest(item) {
  const id = typeof item.id === "string" ? item.id : "";
  const member = item.member && typeof item.member === "object" ? item.member : null;
  const memberName = typeof member?.displayName === "string" ? member.displayName : null;
  const startDate = typeof item.startDate === "string" ? item.startDate : typeof item.start_date === "string" ? item.start_date : null;
  const endDate = typeof item.endDate === "string" ? item.endDate : typeof item.end_date === "string" ? item.end_date : null;
  const status = item.status === "approved" || item.status === "rejected" ? item.status : "pending";
  const reason = typeof item.unavailabilityReasonValue === "string" ? item.unavailabilityReasonValue : typeof item.unavailability_reason_value === "string" ? item.unavailability_reason_value : null;
  const updatedAt = typeof item.updatedAt === "string" ? item.updatedAt : typeof item.updated_at === "string" ? item.updated_at : null;
  return withDataTableNamespaces({
    id,
    memberName,
    startDate,
    endDate,
    status,
    reason,
    updatedAt
  }, item);
}
function formatDateRange(start, end) {
  const startLabel = formatDateLabel(start);
  const endLabel = formatDateLabel(end);
  if (startLabel && endLabel) return `${startLabel} -> ${endLabel}`;
  return startLabel || endLabel || "-";
}
function formatDateLabel(value) {
  if (!value) return "";
  const date = new Date(value);
  if (Number.isNaN(date.getTime())) return value;
  return date.toLocaleDateString();
}
function resolveStatusVariant(status) {
  if (status === "approved") return "default";
  if (status === "rejected") return "destructive";
  return "secondary";
}
export {
  StaffLeaveRequestsPage as default
};
//# sourceMappingURL=page.js.map
