"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 { Button } from "@open-mercato/ui/primitives/button";
import { createCrud } from "@open-mercato/ui/backend/utils/crud";
import { LoadingMessage, ErrorMessage } from "@open-mercato/ui/backend/detail";
import { readApiResultOrThrow } from "@open-mercato/ui/backend/utils/apiCall";
import { LeaveRequestForm, buildLeaveRequestPayload } from "@open-mercato/core/modules/staff/components/LeaveRequestForm";
import { useT } from "@open-mercato/shared/lib/i18n/context";
function StaffMyLeaveRequestCreatePage() {
  const t = useT();
  const router = useRouter();
  const [member, setMember] = React.useState(null);
  const [isLoading, setIsLoading] = React.useState(true);
  const [error, setError] = React.useState(null);
  React.useEffect(() => {
    let cancelled = false;
    async function load() {
      setIsLoading(true);
      setError(null);
      try {
        const payload = await readApiResultOrThrow(
          "/api/staff/team-members/self",
          void 0,
          { errorMessage: t("staff.leaveRequests.errors.profileLoad", "Failed to load your profile.") }
        );
        const entry = payload.member;
        if (!entry?.id || !entry.displayName) {
          if (!cancelled) setMember(null);
          return;
        }
        if (!cancelled) setMember({ id: entry.id, displayName: entry.displayName });
      } catch (err) {
        if (!cancelled) {
          const message = err instanceof Error ? err.message : t("staff.leaveRequests.errors.profileLoad", "Failed to load your profile.");
          setError(message);
        }
      } finally {
        if (!cancelled) setIsLoading(false);
      }
    }
    void load();
    return () => {
      cancelled = true;
    };
  }, [t]);
  const handleSubmit = React.useCallback(async (values) => {
    if (!member?.id) return;
    const payload = buildLeaveRequestPayload({ ...values, memberId: member.id });
    const { result } = await createCrud("staff/leave-requests", payload, {
      errorMessage: t("staff.leaveRequests.form.errors.create", "Failed to create leave request.")
    });
    const requestId = typeof result?.id === "string" ? result.id : null;
    if (requestId) {
      router.push(`/backend/staff/my-leave-requests/${encodeURIComponent(requestId)}`);
      return;
    }
    router.push("/backend/staff/my-leave-requests");
  }, [member?.id, router, t]);
  if (isLoading) {
    return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(LoadingMessage, { label: t("staff.leaveRequests.form.loading", "Loading leave request...") }) }) });
  }
  if (error) {
    return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(ErrorMessage, { label: error }) }) });
  }
  if (!member) {
    return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsxs("div", { className: "space-y-3 rounded-lg border bg-card p-6 text-center text-sm text-muted-foreground", 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", children: /* @__PURE__ */ jsx(Link, { href: "/backend/staff/profile/create", children: t("staff.leaveRequests.actions.createProfile", "Create my profile") }) })
    ] }) }) });
  }
  const initialValues = {
    memberId: member.id,
    memberLabel: member.displayName,
    startDate: null,
    endDate: null,
    timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
  };
  return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(
    LeaveRequestForm,
    {
      title: t("staff.leaveRequests.form.createTitle", "Create leave request"),
      submitLabel: t("staff.leaveRequests.form.actions.create", "Create"),
      backHref: "/backend/staff/my-leave-requests",
      cancelHref: "/backend/staff/my-leave-requests",
      initialValues,
      onSubmit: handleSubmit,
      allowMemberSelect: false,
      memberLabel: member.displayName
    }
  ) }) });
}
export {
  StaffMyLeaveRequestCreatePage as default
};
//# sourceMappingURL=page.js.map
