"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Forward, Reply, Send } from "lucide-react";
import { CrudForm } from "../CrudForm.js";
import { Button } from "../../primitives/button.js";
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "../../primitives/dialog.js";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { getMessageUiComponentRegistry } from "@open-mercato/core/modules/messages/components/utils/typeUiRegistry";
import { getMessageObjectType } from "@open-mercato/core/modules/messages/lib/message-objects-registry";
import { createMessageComposeFormGroups } from "./message-compose-form-groups.js";
import { useMessageCompose } from "./useMessageCompose.js";
function ContextObjectPreview({ contextObject }) {
  const t = useT();
  const registry = getMessageUiComponentRegistry();
  const previewComponentKey = `${contextObject.entityModule}:${contextObject.entityType}`;
  const PreviewComponent = registry.objectPreviewComponents[previewComponentKey] ?? registry.objectPreviewComponents["messages:default"];
  const objectType = getMessageObjectType(contextObject.entityModule, contextObject.entityType);
  if (PreviewComponent) {
    return /* @__PURE__ */ jsx(
      PreviewComponent,
      {
        entityId: contextObject.entityId,
        entityModule: contextObject.entityModule,
        entityType: contextObject.entityType,
        actionRequired: contextObject.actionRequired,
        actionType: contextObject.actionType,
        actionLabel: contextObject.actionLabel,
        previewData: contextObject.previewData ?? void 0,
        icon: objectType?.icon
      }
    );
  }
  return /* @__PURE__ */ jsxs("div", { className: "space-y-1", children: [
    /* @__PURE__ */ jsx("p", { className: "text-xs font-medium uppercase tracking-wide text-muted-foreground", children: t("messages.composer.contextPreview.title", "Context object") }),
    /* @__PURE__ */ jsxs("p", { className: "text-sm font-medium", children: [
      contextObject.entityModule,
      ":",
      contextObject.entityType
    ] }),
    /* @__PURE__ */ jsx("p", { className: "text-xs font-mono text-muted-foreground", children: contextObject.entityId })
  ] });
}
function MessageComposer(props) {
  const compose = useMessageCompose(props);
  const inlineBackHref = props.inlineBackHref;
  const SubmitIcon = compose.variant === "reply" ? Reply : compose.variant === "forward" ? Forward : Send;
  const inlineExtraActions = compose.inline ? /* @__PURE__ */ jsxs(Fragment, { children: [
    compose.variant === "compose" ? /* @__PURE__ */ jsx(
      Button,
      {
        type: "button",
        variant: "outline",
        onClick: compose.handleSaveDraft,
        disabled: compose.submitting,
        children: compose.t("messages.saveDraft", "Save draft")
      }
    ) : null,
    compose.variant !== "compose" ? /* @__PURE__ */ jsx(
      Button,
      {
        type: "button",
        variant: "outline",
        onClick: compose.handleBack,
        disabled: compose.submitting,
        children: compose.t("ui.forms.actions.cancel", "Cancel")
      }
    ) : null
  ] }) : null;
  const dialogExtraActions = !compose.inline ? /* @__PURE__ */ jsxs(Fragment, { children: [
    compose.variant === "compose" ? /* @__PURE__ */ jsx(
      Button,
      {
        type: "button",
        variant: "outline",
        onClick: compose.handleSaveDraft,
        disabled: compose.submitting,
        children: compose.t("messages.saveDraft", "Save draft")
      }
    ) : null,
    /* @__PURE__ */ jsx(
      Button,
      {
        type: "button",
        variant: "outline",
        onClick: compose.handleBack,
        disabled: compose.submitting,
        children: compose.t("ui.forms.actions.cancel", "Cancel")
      }
    )
  ] }) : null;
  const backHref = compose.inline ? inlineBackHref === void 0 ? "/backend/messages" : inlineBackHref ?? void 0 : void 0;
  const fallbackContextPreview = React.useMemo(() => {
    if (compose.contextPreview) return compose.contextPreview;
    if (!props.contextObject) return null;
    return /* @__PURE__ */ jsx(ContextObjectPreview, { contextObject: props.contextObject });
  }, [compose.contextPreview, props.contextObject]);
  const composeWithContextPreview = React.useMemo(
    () => ({ ...compose, contextPreview: fallbackContextPreview }),
    [compose, fallbackContextPreview]
  );
  const composePanel = /* @__PURE__ */ jsx(
    CrudForm,
    {
      backHref,
      title: composeWithContextPreview.composerTitle,
      fields: createMessageComposeFormGroups(composeWithContextPreview),
      initialValues: {},
      submitLabel: composeWithContextPreview.submitLabel,
      submitIcon: SubmitIcon,
      extraActions: composeWithContextPreview.inline ? inlineExtraActions : dialogExtraActions,
      hideFooterActions: true,
      onSubmit: async () => {
        await composeWithContextPreview.handleSubmit();
      }
    }
  );
  if (compose.inline) {
    return /* @__PURE__ */ jsx("div", { className: "space-y-4", children: composePanel });
  }
  return /* @__PURE__ */ jsx(Dialog, { open: Boolean(compose.open), onOpenChange: compose.handleDialogOpenChange, children: /* @__PURE__ */ jsxs(DialogContent, { className: "sm:max-w-3xl [&>button]:hidden", children: [
    /* @__PURE__ */ jsxs(DialogHeader, { className: "sr-only", children: [
      /* @__PURE__ */ jsx(DialogTitle, { children: compose.composerTitle }),
      /* @__PURE__ */ jsx(DialogDescription, { children: compose.t("messages.composer.dialogDescription", "Compose and send a message.") })
    ] }),
    composePanel,
    /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-2 border-t pt-4", children: [
      /* @__PURE__ */ jsx(
        Button,
        {
          type: "button",
          variant: "outline",
          onClick: compose.handleBack,
          disabled: compose.submitting,
          children: compose.t("ui.forms.actions.cancel", "Cancel")
        }
      ),
      /* @__PURE__ */ jsxs(
        Button,
        {
          type: "button",
          onClick: () => {
            void compose.handleSubmit();
          },
          disabled: compose.submitting,
          children: [
            /* @__PURE__ */ jsx(SubmitIcon, { className: "mr-2 size-4" }),
            compose.submitLabel
          ]
        }
      )
    ] })
  ] }) });
}
export {
  MessageComposer
};
//# sourceMappingURL=MessageComposer.js.map
