"use client";
import { jsx, jsxs } 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 { 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 StaffMyLeaveRequestsPage() {
  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 [memberId, setMemberId] = React.useState(null);
  const [canSend, setCanSend] = React.useState(false);
  const labels = React.useMemo(() => ({
    title: t("staff.leaveRequests.my.title", "My leave requests"),
    table: {
      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"),
      createProfile: t("staff.leaveRequests.actions.createProfile", "Create my profile")
    },
    errors: {
      load: t("staff.leaveRequests.errors.load", "Failed to load leave requests.")
    }
  }), [t]);
  const columns = React.useMemo(() => [
    {
      accessorKey: "startDate",
      header: labels.table.dates,
      meta: { priority: 1, sticky: true },
      cell: ({ row }) => formatDateRange(row.original.startDate, row.original.endDate)
    },
    {
      accessorKey: "status",
      header: labels.table.status,
      meta: { priority: 2 },
      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: 3, truncate: true, maxWidth: "240px" },
      cell: ({ row }) => row.original.reason ?? "-"
    },
    {
      accessorKey: "updatedAt",
      header: labels.table.updatedAt,
      meta: { priority: 4 },
      cell: ({ row }) => formatDateLabel(row.original.updatedAt)
    }
  ], [labels.table, 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);
      const viewerMemberId = typeof payload.viewer?.memberId === "string" ? payload.viewer.memberId : null;
      setMemberId(viewerMemberId);
      setCanSend(payload.viewer?.canSend === true);
    } catch {
      setRows([]);
      setTotal(0);
      setTotalPages(1);
      setMemberId(null);
      setCanSend(false);
    } finally {
      setIsLoading(false);
    }
  }, [labels.errors.load, page, search, sorting]);
  React.useEffect(() => {
    void loadRows();
  }, [loadRows, scopeVersion]);
  const handleSearchChange = React.useCallback((value) => {
    setSearch(value);
    setPage(1);
  }, []);
  const emptyState = memberId ? /* @__PURE__ */ jsx("p", { className: "py-8 text-center text-sm text-muted-foreground", children: labels.table.empty }) : /* @__PURE__ */ jsxs("div", { className: "py-8 text-center text-sm text-muted-foreground space-y-3", children: [
    /* @__PURE__ */ jsx("p", { children: t("staff.leaveRequests.empty.profileRequired", "Create your team member profile to submit leave requests.") }),
    /* @__PURE__ */ jsx(Button, { asChild: true, size: "sm", variant: "outline", children: /* @__PURE__ */ jsx(Link, { href: "/backend/staff/profile/create", children: labels.actions.createProfile }) })
  ] });
  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,
      actions: memberId && canSend ? /* @__PURE__ */ jsx(Button, { asChild: true, size: "sm", children: /* @__PURE__ */ jsx(Link, { href: "/backend/staff/my-leave-requests/create", children: labels.actions.add }) }) : null,
      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/my-leave-requests/${encodeURIComponent(row.id)}`);
      }
    }
  ) }) });
}
function mapLeaveRequest(item) {
  const id = typeof item.id === "string" ? item.id : "";
  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,
    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 {
  StaffMyLeaveRequestsPage as default
};
//# sourceMappingURL=page.js.map
