import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { FileCode, Globe, Lock } from "lucide-react";
import { IconButton } from "../../primitives/icon-button.js";
import { Input } from "../../primitives/input.js";
import { Label } from "../../primitives/label.js";
import { Switch } from "../../primitives/switch.js";
import { AttachmentsSection } from "../detail/AttachmentsSection.js";
import { SwitchableMarkdownInput } from "../inputs/SwitchableMarkdownInput.js";
import { TagsInput } from "../inputs/TagsInput.js";
import { MessagePrioritySelector } from "./MessagePrioritySelector.js";
function RecipientTagsInput({ compose }) {
  return /* @__PURE__ */ jsx(
    TagsInput,
    {
      value: compose.recipientIds,
      onChange: compose.setRecipientIds,
      selectedOptions: compose.selectedRecipientOptions,
      resolveLabel: compose.resolveRecipientLabel,
      loadSuggestions: compose.loadRecipientSuggestions,
      placeholder: compose.t("messages.placeholders.recipients", "Search recipients..."),
      allowCustomValues: false,
      showSuggestionsOnFocus: false
    }
  );
}
function VisibilitySelector({ compose }) {
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(Label, { children: compose.t("messages.visibility", "Visibility") }),
    /* @__PURE__ */ jsxs(
      "div",
      {
        className: "inline-flex items-center gap-1 rounded-md border bg-background p-1",
        role: "radiogroup",
        "aria-label": compose.t("messages.visibility", "Visibility"),
        children: [
          /* @__PURE__ */ jsx(
            IconButton,
            {
              type: "button",
              size: "xs",
              variant: compose.visibility === "internal" ? "outline" : "ghost",
              role: "radio",
              "aria-checked": compose.visibility === "internal",
              "aria-label": compose.t("messages.visibilityInternal", "Internal"),
              title: compose.t("messages.visibilityInternal", "Internal"),
              className: "h-7 w-7",
              onClick: () => compose.setVisibility("internal"),
              children: /* @__PURE__ */ jsx(Lock, { className: "h-3.5 w-3.5" })
            }
          ),
          /* @__PURE__ */ jsx(
            IconButton,
            {
              type: "button",
              size: "xs",
              variant: compose.visibility === "public" ? "outline" : "ghost",
              role: "radio",
              "aria-checked": compose.visibility === "public",
              "aria-label": compose.t("messages.visibilityPublic", "Public"),
              title: compose.t("messages.visibilityPublic", "Public"),
              className: "h-7 w-7",
              onClick: () => compose.setVisibility("public"),
              children: /* @__PURE__ */ jsx(Globe, { className: "h-3.5 w-3.5" })
            }
          )
        ]
      }
    ),
    /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: compose.visibility === "public" ? compose.t("messages.visibilityPublicHint", "Public messages are sent to external email only.") : compose.t("messages.visibilityInternalHint", "Internal messages are sent to selected system users.") })
  ] });
}
function ContextActionsSection({ compose }) {
  if (!compose.shouldShowContextActions) return null;
  return /* @__PURE__ */ jsxs("div", { className: "grid gap-3 sm:grid-cols-2", children: [
    compose.normalizedRequiredActionMode === "optional" ? /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between rounded border px-3 py-2 sm:col-span-2", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: compose.t("messages.composer.objectPicker.actionRequiredLabel", "Action required") }),
        /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: compose.t("messages.composer.objectPicker.actionRequiredHint", "Mark this object as requiring recipient action.") })
      ] }),
      /* @__PURE__ */ jsx(Switch, { checked: compose.contextActionRequired, onCheckedChange: compose.setContextActionRequired })
    ] }) : null,
    compose.normalizedRequiredActionMode === "required" || compose.contextActionRequired ? /* @__PURE__ */ jsxs("div", { className: "space-y-2 sm:col-span-2", children: [
      /* @__PURE__ */ jsx(Label, { htmlFor: "messages-compose-context-action-type", children: compose.t("messages.composer.objectPicker.actionTypeLabel", "Action type") }),
      /* @__PURE__ */ jsxs(
        "select",
        {
          id: "messages-compose-context-action-type",
          value: compose.contextActionType,
          onChange: (event) => compose.setContextActionType(event.target.value),
          className: "h-9 w-full rounded-md border bg-background px-3 text-sm",
          children: [
            /* @__PURE__ */ jsx("option", { value: "", children: compose.t("messages.composer.objectPicker.actionTypePlaceholder", "Select action") }),
            compose.contextActionOptions.map((option) => /* @__PURE__ */ jsx("option", { value: option.id, children: option.label }, option.id))
          ]
        }
      )
    ] }) : null
  ] });
}
function PrioritySelector({ compose }) {
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(Label, { children: compose.t("messages.priority", "Priority") }),
    /* @__PURE__ */ jsx(
      MessagePrioritySelector,
      {
        value: compose.priority,
        onChange: compose.setPriority,
        t: compose.t
      }
    )
  ] });
}
function MarkdownBodySection({
  compose,
  label,
  placeholder,
  inputId,
  rows,
  textareaClassName
}) {
  return /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between gap-2", children: [
      /* @__PURE__ */ jsx(Label, { htmlFor: inputId, children: label }),
      /* @__PURE__ */ jsx(
        IconButton,
        {
          type: "button",
          size: "sm",
          variant: compose.bodyFormat === "markdown" ? "outline" : "ghost",
          "aria-pressed": compose.bodyFormat === "markdown",
          onClick: () => compose.setBodyFormat((previousValue) => previousValue === "markdown" ? "text" : "markdown"),
          title: compose.t("messages.bodyFormat.toggle", "Toggle markdown"),
          children: /* @__PURE__ */ jsx(FileCode, { className: "h-4 w-4" })
        }
      )
    ] }),
    /* @__PURE__ */ jsx("div", { id: inputId, children: /* @__PURE__ */ jsx(
      SwitchableMarkdownInput,
      {
        value: compose.body,
        onChange: compose.setBody,
        isMarkdownEnabled: compose.bodyFormat === "markdown",
        rows,
        placeholder,
        textareaClassName
      }
    ) })
  ] });
}
function ComposeModeFields({ compose }) {
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsxs("div", { className: "space-y-3", children: [
      /* @__PURE__ */ jsxs("div", { className: "grid gap-3 sm:grid-cols-2", children: [
        /* @__PURE__ */ jsx("div", { className: "space-y-2", children: compose.visibility === "public" ? /* @__PURE__ */ jsxs(Fragment, { children: [
          /* @__PURE__ */ jsx(Label, { htmlFor: "messages-compose-external-email", children: compose.t("messages.externalEmail", "External email") }),
          /* @__PURE__ */ jsx(
            Input,
            {
              id: "messages-compose-external-email",
              type: "email",
              value: compose.externalEmail,
              onChange: (event) => compose.setExternalEmail(event.target.value),
              placeholder: compose.t("messages.placeholders.externalEmail", "name@example.com")
            }
          )
        ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
          /* @__PURE__ */ jsx(Label, { htmlFor: "messages-compose-recipients", children: compose.t("messages.to", "To") }),
          /* @__PURE__ */ jsx(RecipientTagsInput, { compose })
        ] }) }),
        /* @__PURE__ */ jsx("div", { className: "space-y-2", children: /* @__PURE__ */ jsx(VisibilitySelector, { compose }) })
      ] }),
      /* @__PURE__ */ jsx(ContextActionsSection, { compose })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "grid gap-3 sm:grid-cols-2", children: [
      /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
        /* @__PURE__ */ jsx(Label, { htmlFor: "messages-compose-subject", children: compose.t("messages.subject", "Subject") }),
        /* @__PURE__ */ jsx(
          Input,
          {
            id: "messages-compose-subject",
            value: compose.subject,
            onChange: (event) => compose.setSubject(event.target.value),
            placeholder: compose.t("messages.placeholders.subject", "Enter subject...")
          }
        )
      ] }),
      /* @__PURE__ */ jsx("div", { className: "space-y-2", children: /* @__PURE__ */ jsx(PrioritySelector, { compose }) })
    ] }),
    /* @__PURE__ */ jsx(
      MarkdownBodySection,
      {
        compose,
        label: compose.t("messages.body", "Message"),
        placeholder: compose.t("messages.placeholders.body", "Write your message..."),
        inputId: "messages-compose-body",
        rows: 8,
        textareaClassName: "min-h-[180px] w-full rounded-md border bg-background px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary"
      }
    ),
    /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
      /* @__PURE__ */ jsx(Label, { children: compose.t("messages.attachedFiles", "Attachments") }),
      /* @__PURE__ */ jsx(
        AttachmentsSection,
        {
          entityId: compose.attachmentEntityId,
          recordId: compose.attachmentRecordId,
          showHeader: false,
          compact: true,
          onChanged: () => {
            void compose.loadAttachmentIds().catch(() => null);
          }
        }
      )
    ] })
  ] });
}
function ReplyModeFields({ compose }) {
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(
      MarkdownBodySection,
      {
        compose,
        label: compose.t("messages.replyBody", "Reply"),
        placeholder: compose.t("messages.placeholders.replyBody", "Write your reply..."),
        inputId: "messages-compose-body",
        rows: 8,
        textareaClassName: "min-h-[180px] w-full rounded-md border bg-background px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary"
      }
    ),
    /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
      /* @__PURE__ */ jsx(Label, { children: compose.t("messages.attachedFiles", "Attachments") }),
      /* @__PURE__ */ jsx(
        AttachmentsSection,
        {
          entityId: compose.attachmentEntityId,
          recordId: compose.attachmentRecordId,
          showHeader: false,
          compact: true,
          onChanged: () => {
            void compose.loadAttachmentIds().catch(() => null);
          }
        }
      )
    ] })
  ] });
}
function ForwardModeFields({ compose }) {
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsxs("div", { className: "space-y-2", children: [
      /* @__PURE__ */ jsx(Label, { htmlFor: "messages-compose-recipients", children: compose.t("messages.to", "To") }),
      /* @__PURE__ */ jsx(RecipientTagsInput, { compose })
    ] }),
    /* @__PURE__ */ jsx(
      MarkdownBodySection,
      {
        compose,
        label: compose.t("messages.forwardContent", "Forwarded content"),
        placeholder: compose.t("messages.placeholders.forwardContent", "Review and edit forwarded content..."),
        inputId: "messages-forward-note",
        rows: 6,
        textareaClassName: "min-h-[140px] w-full rounded-md border bg-background px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary"
      }
    )
  ] });
}
function EmailDeliverySection({ compose }) {
  if (compose.variant === "reply" || compose.variant === "forward") {
    return null;
  }
  if (compose.isComposePublicVisibility) {
    return /* @__PURE__ */ jsxs("div", { className: "rounded border px-3 py-2", children: [
      /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: compose.t("messages.sendViaEmail", "Also send via email") }),
      /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: compose.t("messages.sendViaEmailForcedPublic", "For public visibility, email delivery is always enabled.") })
    ] });
  }
  return /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between rounded border px-3 py-2", children: [
    /* @__PURE__ */ jsxs("div", { children: [
      /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: compose.t("messages.sendViaEmail", "Also send via email") }),
      /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: compose.t("messages.sendViaEmailHint", "Recipients will receive an email copy with a secure link.") })
    ] }),
    /* @__PURE__ */ jsx(Switch, { checked: compose.sendViaEmail, onCheckedChange: compose.setSendViaEmail })
  ] });
}
function ReplyForwardOptionsRow({ compose }) {
  if (compose.variant !== "reply" && compose.variant !== "forward") {
    return null;
  }
  return /* @__PURE__ */ jsxs("div", { className: "grid gap-3 sm:grid-cols-2", children: [
    compose.variant === "forward" ? /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between rounded border px-3 py-2", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: compose.t("messages.includeAttachments", "Include attachments") }),
        /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: compose.t("messages.includeAttachmentsHint", "Carry over attachments from the original message.") })
      ] }),
      /* @__PURE__ */ jsx(Switch, { checked: compose.includeAttachments, onCheckedChange: compose.setIncludeAttachments })
    ] }) : /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between rounded border px-3 py-2", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: compose.t("messages.replyAll", "Reply all") }),
        /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: compose.t("messages.replyAllHint", "Include all original recipients.") })
      ] }),
      /* @__PURE__ */ jsx(Switch, { checked: compose.replyAll, onCheckedChange: compose.setReplyAll })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between rounded border px-3 py-2", children: [
      /* @__PURE__ */ jsxs("div", { children: [
        /* @__PURE__ */ jsx("p", { className: "text-sm font-medium", children: compose.t("messages.sendViaEmail", "Also send via email") }),
        /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: compose.t("messages.sendViaEmailHint", "Recipients will receive an email copy with a secure link.") })
      ] }),
      /* @__PURE__ */ jsx(Switch, { checked: compose.sendViaEmail, onCheckedChange: compose.setSendViaEmail })
    ] })
  ] });
}
function MessageComposeFormBody({ compose }) {
  return /* @__PURE__ */ jsxs("div", { className: "space-y-4", onKeyDown: compose.handleKeyDown, children: [
    compose.contextPreview ? /* @__PURE__ */ jsx("div", { className: "rounded border bg-muted/30 p-3 text-sm", children: compose.contextPreview }) : null,
    compose.variant === "compose" ? /* @__PURE__ */ jsx(ComposeModeFields, { compose }) : null,
    compose.variant === "reply" ? /* @__PURE__ */ jsx(ReplyModeFields, { compose }) : null,
    compose.variant === "forward" ? /* @__PURE__ */ jsx(ForwardModeFields, { compose }) : null,
    /* @__PURE__ */ jsx(ReplyForwardOptionsRow, { compose }),
    /* @__PURE__ */ jsx(EmailDeliverySection, { compose }),
    compose.submitError ? /* @__PURE__ */ jsx("p", { className: "text-sm text-destructive", children: compose.submitError }) : null
  ] });
}
function createMessageComposeFormGroups(compose) {
  return [{
    id: "composer",
    label: "",
    type: "custom",
    component: () => /* @__PURE__ */ jsx(MessageComposeFormBody, { compose })
  }];
}
export {
  createMessageComposeFormGroups
};
//# sourceMappingURL=message-compose-form-groups.js.map
