"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import { useState } from "react";
import { useTranslations } from "next-intl";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle
} from "../ui/dialog";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { Label } from "../ui/label";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "../ui/select";
import { useTeamMembers } from "../../hooks/useTeamMembers";
import { toast } from "sonner";
import { APP_CONFIG_MERGED } from "../../lib/config/config-sync";
import { getInvitableRoles } from "../../lib/teams/permissions";
function getAvailableRolesForInvite(userRole) {
  const hierarchy = APP_CONFIG_MERGED.teamRoles.hierarchy;
  const userLevel = hierarchy[userRole] ?? 0;
  const invitableRoles = getInvitableRoles();
  return invitableRoles.filter((role) => {
    const roleLevel = hierarchy[role] ?? 0;
    return roleLevel <= userLevel;
  });
}
function InviteMemberDialog({ open, onOpenChange, teamId, currentUserRole = "owner" }) {
  const t = useTranslations("teams");
  const { inviteMemberAsync, isInviting } = useTeamMembers({ teamId });
  const [email, setEmail] = useState("");
  const availableRoles = getAvailableRolesForInvite(currentUserRole);
  const defaultRole = availableRoles.includes("admin") ? "admin" : availableRoles[0];
  const [role, setRole] = useState(defaultRole);
  const handleSubmit = async (e) => {
    e.preventDefault();
    try {
      await inviteMemberAsync({ email, role });
      toast.success(t("messages.memberInvited"));
      onOpenChange(false);
      setEmail("");
      setRole(defaultRole);
    } catch (error) {
      toast.error(error instanceof Error ? error.message : t("errors.unauthorized"));
    }
  };
  return /* @__PURE__ */ jsx(Dialog, { open, onOpenChange, children: /* @__PURE__ */ jsx(DialogContent, { "data-cy": "invite-member-dialog", children: /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, children: [
    /* @__PURE__ */ jsxs(DialogHeader, { children: [
      /* @__PURE__ */ jsx(DialogTitle, { children: t("actions.invite") }),
      /* @__PURE__ */ jsx(DialogDescription, { children: t("entity.description") })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "grid gap-4 py-4", children: [
      /* @__PURE__ */ jsxs("div", { className: "grid gap-2", children: [
        /* @__PURE__ */ jsx(Label, { htmlFor: "member-email", children: t("fields.email") }),
        /* @__PURE__ */ jsx(
          Input,
          {
            id: "member-email",
            name: "email",
            type: "email",
            value: email,
            onChange: (e) => setEmail(e.target.value),
            placeholder: "colleague@example.com",
            required: true,
            "data-cy": "member-email-input"
          }
        )
      ] }),
      /* @__PURE__ */ jsxs("div", { className: "grid gap-2", children: [
        /* @__PURE__ */ jsx(Label, { htmlFor: "member-role", children: t("fields.role") }),
        /* @__PURE__ */ jsxs(
          Select,
          {
            name: "role",
            value: role,
            onValueChange: (value) => setRole(value),
            children: [
              /* @__PURE__ */ jsx(SelectTrigger, { id: "member-role", "data-cy": "member-role-select", children: /* @__PURE__ */ jsx(SelectValue, {}) }),
              /* @__PURE__ */ jsx(SelectContent, { children: availableRoles.map((r) => /* @__PURE__ */ jsx(SelectItem, { value: r, "data-cy": `role-option-${r}`, children: t(`roles.${r}`) }, r)) })
            ]
          }
        ),
        /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: t(`roles.${role}`) })
      ] })
    ] }),
    /* @__PURE__ */ jsxs(DialogFooter, { children: [
      /* @__PURE__ */ jsx(
        Button,
        {
          type: "button",
          variant: "outline",
          onClick: () => onOpenChange(false),
          disabled: isInviting,
          "data-cy": "cancel-invite-member",
          children: t("actions.cancel")
        }
      ),
      /* @__PURE__ */ jsx(
        Button,
        {
          type: "submit",
          disabled: isInviting,
          "data-cy": "submit-invite-member",
          children: isInviting ? t("actions.inviting") : t("actions.invite")
        }
      )
    ] })
  ] }) }) });
}
export {
  InviteMemberDialog
};
