"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Sparkles } from "lucide-react";
import { Button } from "../../primitives/button.js";
import { apiCall } from "../utils/apiCall.js";
import { flash } from "../FlashMessages.js";
import { useT } from "@open-mercato/shared/lib/i18n/context";
const upgradeActionsEnabled = process.env.NEXT_PUBLIC_UPGRADE_ACTIONS_ENABLED === "true" || process.env.UPGRADE_ACTIONS_ENABLED === "true";
function UpgradeActionBanner() {
  const t = useT();
  const [action, setAction] = React.useState(null);
  const [loading, setLoading] = React.useState(false);
  const cancelledRef = React.useRef(false);
  const loadNextAction = React.useCallback(async () => {
    if (!upgradeActionsEnabled) return;
    if (typeof window === "undefined" || typeof fetch === "undefined") return;
    const call = await apiCall("/api/configs/upgrade-actions");
    if (cancelledRef.current) return;
    if (!call.ok || !call.result || !Array.isArray(call.result.actions) || !call.result.actions.length) {
      setAction(null);
      return;
    }
    setAction(call.result.actions[0]);
  }, []);
  React.useEffect(() => {
    cancelledRef.current = false;
    void loadNextAction();
    return () => {
      cancelledRef.current = true;
    };
  }, [loadNextAction]);
  if (!upgradeActionsEnabled || !action) return null;
  async function handleRun() {
    if (!upgradeActionsEnabled || !action || loading) return;
    setLoading(true);
    try {
      const response = await apiCall("/api/configs/upgrade-actions", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ actionId: action.id })
      });
      if (!response.ok) {
        const baseError = response.result && typeof response.result.error === "string" && response.result.error || t("upgrades.runFailed", "We could not run this upgrade action.");
        const detail = response.result && typeof response.result.details === "string" ? response.result.details : null;
        const errorMessage = detail ? `${baseError} (${detail})` : baseError;
        flash(errorMessage, "error");
        return;
      }
      const message = response.result?.message || action.successMessage || t("upgrades.v034.success", "Example catalog products and categories installed.");
      flash(message, "success");
      setAction(null);
      await loadNextAction();
    } catch (error) {
      const message = error instanceof Error ? error.message : t("upgrades.runFailed", "We could not run this upgrade action.");
      flash(message, "error");
    } finally {
      setLoading(false);
    }
  }
  const loadingLabel = action.loadingLabel || t("upgrades.v034.loading", "Installing\u2026");
  const title = action.ctaLabel || action.message;
  const description = action.message && action.message !== title ? action.message : null;
  return /* @__PURE__ */ jsxs("div", { className: "mb-4 flex flex-col gap-3 rounded-md border border-amber-300 bg-amber-50 px-3 py-3 text-sm text-amber-900 md:flex-row md:items-center md:justify-between", children: [
    /* @__PURE__ */ jsxs("div", { className: "flex items-start gap-2 text-sm", children: [
      /* @__PURE__ */ jsx(Sparkles, { className: "mt-0.5 size-4 text-amber-700", "aria-hidden": "true" }),
      /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
        /* @__PURE__ */ jsx("div", { className: "font-medium text-amber-950", children: title }),
        description ? /* @__PURE__ */ jsx("div", { className: "text-xs text-amber-900/80", children: description }) : null,
        /* @__PURE__ */ jsx("div", { className: "text-xs text-amber-900/80", children: t("upgrades.versionLabel", { version: action.version }) })
      ] })
    ] }),
    /* @__PURE__ */ jsx("div", { className: "flex flex-wrap items-center gap-2", children: /* @__PURE__ */ jsx(
      Button,
      {
        variant: "outline",
        size: "sm",
        onClick: () => {
          void handleRun();
        },
        disabled: loading,
        className: "border-amber-300 text-amber-900 hover:bg-amber-100",
        children: loading ? loadingLabel : action.ctaLabel
      }
    ) })
  ] });
}
export {
  UpgradeActionBanner
};
//# sourceMappingURL=UpgradeActionBanner.js.map
