"use client";
import { apiCall } from "../../backend/utils/apiCall.js";
const POST_JSON_TIMEOUT_MS = 6e4;
async function postJson(url, body) {
  const controller = new AbortController();
  const timer = typeof window !== "undefined" ? window.setTimeout(() => controller.abort(), POST_JSON_TIMEOUT_MS) : null;
  try {
    const call = await apiCall(url, {
      method: "POST",
      headers: body ? { "Content-Type": "application/json" } : void 0,
      body: body ? JSON.stringify(body) : void 0,
      signal: controller.signal
    });
    if (call.ok) {
      const data = call.result;
      return { ok: true, data };
    }
    const raw = call.result ?? {};
    const errorMessage = typeof raw.error === "string" && raw.error.length > 0 ? raw.error : `Request failed (${call.status}).`;
    const code = typeof raw.code === "string" ? raw.code : void 0;
    const { error: _err, code: _code, ...extra } = raw;
    return {
      ok: false,
      error: {
        status: call.status,
        code,
        message: errorMessage,
        extra: Object.keys(extra).length > 0 ? extra : void 0
      }
    };
  } catch (err) {
    const aborted = err?.name === "AbortError" || controller.signal.aborted;
    return {
      ok: false,
      error: {
        status: aborted ? 408 : 0,
        code: aborted ? "request_timeout" : "network_error",
        message: aborted ? `Request timed out after ${Math.round(POST_JSON_TIMEOUT_MS / 1e3)}s.` : err instanceof Error ? err.message : "Network error contacting the AI dispatcher."
      }
    };
  } finally {
    if (timer !== null) window.clearTimeout(timer);
  }
}
async function confirmPendingAction(pendingActionId, options) {
  const base = options?.endpoint ?? "/api/ai_assistant/ai/actions";
  const url = `${base}/${encodeURIComponent(pendingActionId)}/confirm`;
  return postJson(url);
}
async function cancelPendingAction(pendingActionId, options) {
  const base = options?.endpoint ?? "/api/ai_assistant/ai/actions";
  const url = `${base}/${encodeURIComponent(pendingActionId)}/cancel`;
  const body = options?.reason ? { reason: options.reason } : void 0;
  return postJson(url, body);
}
export {
  cancelPendingAction,
  confirmPendingAction
};
//# sourceMappingURL=pending-action-api.js.map
