"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import Link from "next/link";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import { Button } from "@open-mercato/ui/primitives/button";
import { MessageObjectPreview } from "./MessageObjectPreview.js";
function resolveActionHref(template, entityId) {
  return template.replace("{entityId}", encodeURIComponent(entityId));
}
function MessageObjectDetail(props) {
  const t = useT();
  const [executingActionId, setExecutingActionId] = React.useState(null);
  const viewAction = props.actions.find((a) => a.id === "view");
  const otherActions = props.actions.filter((a) => a.id !== "view");
  const preview = /* @__PURE__ */ jsx(
    MessageObjectPreview,
    {
      entityId: props.entityId,
      entityModule: props.entityModule,
      entityType: props.entityType,
      snapshot: props.snapshot,
      previewData: props.previewData,
      actionRequired: props.actionRequired,
      actionType: props.actionType,
      actionLabel: props.actionLabel,
      icon: props.icon
    }
  );
  return /* @__PURE__ */ jsxs("div", { className: "space-y-3 rounded border p-3", children: [
    viewAction?.href ? /* @__PURE__ */ jsx(
      Link,
      {
        href: resolveActionHref(viewAction.href, props.entityId),
        className: "block rounded-md transition-opacity hover:opacity-75",
        children: preview
      }
    ) : preview,
    otherActions.length > 0 ? /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-2", children: otherActions.map((action) => {
      if (action.href) {
        return /* @__PURE__ */ jsx(
          Button,
          {
            type: "button",
            size: "sm",
            variant: action.variant ?? "default",
            asChild: true,
            children: /* @__PURE__ */ jsx(Link, { href: resolveActionHref(action.href, props.entityId), children: t(action.labelKey ?? action.id, action.id) })
          },
          action.id
        );
      }
      return /* @__PURE__ */ jsx(
        Button,
        {
          type: "button",
          size: "sm",
          variant: action.variant ?? "default",
          disabled: executingActionId !== null,
          onClick: async () => {
            if (executingActionId) return;
            setExecutingActionId(action.id);
            try {
              await props.onAction(action.id, { id: props.entityId });
            } finally {
              setExecutingActionId(null);
            }
          },
          children: executingActionId === action.id ? t("messages.actions.executing", "Executing...") : t(action.labelKey ?? action.id, action.id)
        },
        action.id
      );
    }) }) : null
  ] });
}
var MessageObjectDetail_default = MessageObjectDetail;
export {
  MessageObjectDetail,
  MessageObjectDetail_default as default
};
//# sourceMappingURL=MessageObjectDetail.js.map
