"use client";
import * as React from "react";
import { apiCallOrThrow } from "../../backend/utils/apiCall.js";
const TERMINAL_STATUSES = [
  "confirmed",
  "cancelled",
  "failed",
  "expired"
];
function isTerminal(status) {
  if (!status) return false;
  return TERMINAL_STATUSES.includes(status);
}
async function fetchPendingAction(pendingActionId, endpoint) {
  const url = `${endpoint}/${encodeURIComponent(pendingActionId)}`;
  const call = await apiCallOrThrow(url, { method: "GET" });
  const body = call.result;
  if (body && typeof body === "object") {
    if (body.pendingAction) {
      return { pendingAction: body.pendingAction, error: null };
    }
    if (typeof body.id === "string" && typeof body.status === "string") {
      return { pendingAction: body, error: null };
    }
    if (body.error) {
      return {
        pendingAction: null,
        error: { message: body.error, code: body.code }
      };
    }
  }
  return { pendingAction: null, error: null };
}
function useAiPendingActionPolling(options) {
  const {
    pendingActionId,
    intervalMs = 3e3,
    endpoint = "/api/ai_assistant/ai/actions",
    disabled = false
  } = options;
  const [action, setAction] = React.useState(null);
  const [error, setError] = React.useState(null);
  const [isPolling, setIsPolling] = React.useState(!disabled);
  const mountedRef = React.useRef(true);
  const timerRef = React.useRef(null);
  const statusRef = React.useRef(null);
  const clearTimer = React.useCallback(() => {
    if (timerRef.current) {
      clearTimeout(timerRef.current);
      timerRef.current = null;
    }
  }, []);
  const refresh = React.useCallback(async () => {
    if (!pendingActionId) return null;
    try {
      const result = await fetchPendingAction(pendingActionId, endpoint);
      if (!mountedRef.current) return result.pendingAction ?? null;
      if (result.error) {
        setError(result.error);
      } else {
        setError(null);
      }
      if (result.pendingAction) {
        setAction(result.pendingAction);
        statusRef.current = result.pendingAction.status;
      }
      return result.pendingAction;
    } catch (err) {
      if (!mountedRef.current) return null;
      const message = err instanceof Error ? err.message : "Failed to load pending action.";
      setError({ message });
      return null;
    }
  }, [endpoint, pendingActionId]);
  const scheduleNext = React.useCallback(() => {
    clearTimer();
    if (!mountedRef.current) return;
    if (disabled) {
      setIsPolling(false);
      return;
    }
    if (isTerminal(statusRef.current)) {
      setIsPolling(false);
      return;
    }
    setIsPolling(true);
    timerRef.current = setTimeout(async () => {
      await refresh();
      scheduleNext();
    }, intervalMs);
  }, [clearTimer, disabled, intervalMs, refresh]);
  React.useEffect(() => {
    mountedRef.current = true;
    statusRef.current = null;
    if (disabled) {
      setIsPolling(false);
      return () => {
        mountedRef.current = false;
        clearTimer();
      };
    }
    setIsPolling(true);
    void refresh().then(() => {
      if (!mountedRef.current) return;
      scheduleNext();
    });
    return () => {
      mountedRef.current = false;
      clearTimer();
    };
  }, [pendingActionId, endpoint, intervalMs, disabled]);
  const status = action?.status ?? null;
  return {
    action,
    status,
    isPolling: isPolling && !isTerminal(status),
    error,
    refresh
  };
}
var useAiPendingActionPolling_default = useAiPendingActionPolling;
export {
  useAiPendingActionPolling_default as default,
  useAiPendingActionPolling
};
//# sourceMappingURL=useAiPendingActionPolling.js.map
