"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 { Button } from "@open-mercato/ui/primitives/button";
import { LoadingMessage, ErrorMessage } from "@open-mercato/ui/backend/detail";
import { apiCall } from "@open-mercato/ui/backend/utils/apiCall";
import { AvailabilityRulesEditor } from "@open-mercato/core/modules/planner/components/AvailabilityRulesEditor";
import { buildMemberScheduleItems } from "@open-mercato/core/modules/staff/lib/memberSchedule";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { SendObjectMessageDialog } from "@open-mercato/ui/backend/messages";
function StaffMyAvailabilityPage() {
  const t = useT();
  const [member, setMember] = React.useState(null);
  const [isLoading, setIsLoading] = React.useState(true);
  const [error, setError] = React.useState(null);
  const [canManageAvailability, setCanManageAvailability] = React.useState(false);
  const [canManageUnavailability, setCanManageUnavailability] = React.useState(false);
  React.useEffect(() => {
    let cancelled = false;
    async function load() {
      setIsLoading(true);
      setError(null);
      try {
        const memberCall = await apiCall("/api/staff/team-members/self");
        const featureCall = await apiCall("/api/auth/feature-check", {
          method: "POST",
          headers: { "content-type": "application/json" },
          body: JSON.stringify({
            features: [
              "planner.manage_availability",
              "staff.my_availability.manage",
              "staff.my_availability.unavailability"
            ]
          })
        });
        if (!cancelled) {
          setMember(memberCall.result?.member ?? null);
          const granted = Array.isArray(featureCall.result?.granted) ? featureCall.result?.granted ?? [] : [];
          const hasPlannerManage = granted.includes("planner.manage_availability");
          const hasSelfManage = granted.includes("staff.my_availability.manage");
          const hasSelfUnavailability = granted.includes("staff.my_availability.unavailability");
          const canManage = hasPlannerManage || hasSelfManage;
          setCanManageAvailability(canManage);
          setCanManageUnavailability(canManage && (hasPlannerManage || hasSelfUnavailability));
        }
      } catch (err) {
        if (!cancelled) {
          setError(t("staff.myAvailability.errors.load", "Failed to load availability."));
        }
      } finally {
        if (!cancelled) setIsLoading(false);
      }
    }
    void load();
    return () => {
      cancelled = true;
    };
  }, [t]);
  if (isLoading) {
    return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(LoadingMessage, { label: t("staff.myAvailability.loading", "Loading availability...") }) }) });
  }
  if (error) {
    return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(ErrorMessage, { label: error }) }) });
  }
  if (!member?.id) {
    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.myAvailability.empty.profileRequired", "Create your team member profile to manage availability.") }),
      /* @__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") }) })
    ] }) }) });
  }
  return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsxs("div", { className: "space-y-4", children: [
    member.availabilityRuleSetId ? /* @__PURE__ */ jsx("div", { className: "flex justify-end", children: /* @__PURE__ */ jsx(
      SendObjectMessageDialog,
      {
        object: {
          entityModule: "staff",
          entityType: "my_availability",
          entityId: member.availabilityRuleSetId,
          previewData: { title: member.displayName ?? t("staff.myAvailability.title", "My Availability") }
        },
        viewHref: "/backend/staff/my-availability"
      }
    ) }) : null,
    !canManageAvailability ? /* @__PURE__ */ jsxs("div", { className: "space-y-2 rounded-lg border bg-card p-4 text-sm text-muted-foreground", children: [
      /* @__PURE__ */ jsx("p", { className: "font-medium text-foreground", children: t("staff.myAvailability.readOnly.title", "Only an administrator can manage your availability.") }),
      /* @__PURE__ */ jsx("p", { children: t("staff.myAvailability.readOnly.body", "Use leave requests to request changes.") }),
      /* @__PURE__ */ jsx(Button, { asChild: true, size: "sm", variant: "outline", children: /* @__PURE__ */ jsx(Link, { href: "/backend/staff/my-leave-requests", children: t("staff.leaveRequests.my.title", "My leave requests") }) })
    ] }) : null,
    /* @__PURE__ */ jsx(
      AvailabilityRulesEditor,
      {
        subjectType: "member",
        subjectId: member.id,
        labelPrefix: "staff.teamMembers",
        mode: "availability",
        rulesetId: member.availabilityRuleSetId ?? null,
        buildScheduleItems: ({ availabilityRules, translate }) => buildMemberScheduleItems({ availabilityRules, translate }),
        readOnly: !canManageAvailability,
        allowUnavailability: canManageUnavailability
      }
    )
  ] }) }) });
}
export {
  StaffMyAvailabilityPage as default
};
//# sourceMappingURL=page.js.map
