"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Undo2 } from "lucide-react";
import { useRouter } from "next/navigation";
import { Button } from "../../primitives/button.js";
import { apiCall } from "../utils/apiCall.js";
import { flash } from "../FlashMessages.js";
import { useLastOperation, markUndoSuccess } from "./store.js";
import { useT } from "@open-mercato/shared/lib/i18n/context";
function LastOperationBanner() {
  const t = useT();
  const operation = useLastOperation();
  const [pendingToken, setPendingToken] = React.useState(null);
  const router = useRouter();
  if (!operation) return null;
  const rawLabel = operation.actionLabel ?? operation.commandId;
  const translatedLabel = t(rawLabel);
  const label = translatedLabel === rawLabel ? rawLabel : translatedLabel;
  const isPending = pendingToken === operation.undoToken;
  async function handleUndo() {
    const undoToken = operation?.undoToken;
    if (!undoToken || isPending) return;
    setPendingToken(undoToken);
    try {
      const call = await apiCall("/api/audit_logs/audit-logs/actions/undo", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ undoToken })
      });
      if (!call.ok) {
        const message = call.result && typeof call.result.error === "string" && call.result.error || "";
        throw new Error(message || t("audit_logs.banner.undo_failed", "Failed to undo"));
      }
      markUndoSuccess(undoToken);
      flash(t("audit_logs.banner.undo_success"), "success");
      router.refresh();
      if (typeof window !== "undefined") {
        try {
          const isJSDOM = typeof navigator !== "undefined" && typeof navigator.userAgent === "string" ? navigator.userAgent.toLowerCase().includes("jsdom") : false;
          if (!isJSDOM && typeof window.location?.reload === "function") {
            window.location.reload();
          }
        } catch {
        }
      }
    } catch (err) {
      const message = err instanceof Error && err.message ? err.message : t("audit_logs.banner.undo_error");
      flash(message, "error");
    } finally {
      setPendingToken(null);
    }
  }
  return /* @__PURE__ */ jsxs("div", { className: "mb-4 flex items-center justify-between gap-3 rounded-md border border-amber-200/80 bg-amber-50/95 pl-3 pr-2 py-2 text-sm text-amber-900 shadow-xs sm:pr-3", children: [
    /* @__PURE__ */ jsxs("div", { className: "min-w-0 truncate", children: [
      /* @__PURE__ */ jsx("span", { className: "font-medium text-amber-950", children: t("audit_logs.banner.last_operation") }),
      /* @__PURE__ */ jsx("span", { className: "ml-2 truncate text-amber-900", children: label })
    ] }),
    /* @__PURE__ */ jsxs(
      Button,
      {
        variant: "outline",
        size: "sm",
        onClick: () => {
          void handleUndo();
        },
        disabled: isPending,
        className: "border-amber-200/80 bg-amber-50 text-amber-900 hover:bg-amber-100 hover:text-amber-900 px-2.5 sm:px-3",
        children: [
          /* @__PURE__ */ jsx(Undo2, { className: "mr-1 size-4", "aria-hidden": "true" }),
          isPending ? t("audit_logs.actions.undoing") : t("audit_logs.banner.undo")
        ]
      }
    )
  ] });
}
export {
  LastOperationBanner
};
//# sourceMappingURL=LastOperationBanner.js.map
