import { acceptAgentDialog, approveToolCall, assignAgentSkill, cancelAgent, cancelAgentDialog, claimAgentBrowser, createOperationAllowance, deleteQueuedPrompt, getAgent, getAgentAvailableTools, getAgentDiagnostics, getAgentOperationAllowances, getLastPayload, getPendingPrompts, getAgentSkillCatalog, getAgentTriggerSubscriptions, persistDraftAgent, pushQueuedPrompt, rejectToolCall, releaseAgentBrowser, replayAgentDialog, revokeAgentSkill, startAgent, submitAgentDialogResponse, updateAgentContext, updateAgentCostLimit, updateAgentSettings, } from "../../services/agentService";
import { subscribeAgent } from "../../services/agentSubscriptionRegistry";
import { getAgentHistory } from "../../services/editService";
import { getBrowserCaptureClaim, setBrowserCaptureClaim, } from "../dialogs/browserBoundCapture";
import { clearVisibleDialogEntriesForInstance, isElectedDialogReceiver, registerMountedInstance, setVisibleDialogEntry, unregisterMountedInstance, updateInstanceFocus, } from "../agentDialogRegistry";
const STOP_STALE_RUNNING_LOAD_SUPPRESSION_MS = 5000;
function getStaleRunningLoadSuppressionMap() {
    const globalScope = globalThis;
    globalScope.__parheliaAgentStopSuppressionByAgentId ??= new Map();
    return globalScope.__parheliaAgentStopSuppressionByAgentId;
}
export function markStaleRunningLoadSuppressed(agentId) {
    if (!agentId)
        return;
    getStaleRunningLoadSuppressionMap().set(agentId, Date.now() + STOP_STALE_RUNNING_LOAD_SUPPRESSION_MS);
}
export function clearStaleRunningLoadSuppression(agentId) {
    if (!agentId)
        return;
    getStaleRunningLoadSuppressionMap().delete(agentId);
}
export function hasStaleRunningLoadSuppression(agentId) {
    if (!agentId)
        return false;
    const suppressionMap = getStaleRunningLoadSuppressionMap();
    const suppressUntil = suppressionMap.get(agentId);
    if (!suppressUntil)
        return false;
    if (suppressUntil <= Date.now()) {
        suppressionMap.delete(agentId);
        return false;
    }
    return true;
}
export function normalizeAgentSessionInitialPrompt(value) {
    const trimmed = value?.trim();
    return trimmed ? trimmed : null;
}
export async function loadAgentSessionSnapshot(agentId) {
    const [agent, queuedPrompts] = await Promise.all([
        getAgent(agentId),
        loadPendingQueuedPrompts(agentId),
    ]);
    return { agent, queuedPrompts };
}
export async function refreshAgentAssignedSkillIds(agentId) {
    const agent = await getAgent(agentId);
    const assignedSkillIds = agent?.assignedSkillIds;
    return Array.isArray(assignedSkillIds) ? assignedSkillIds : null;
}
export function loadAgentRunDiagnostics(agentId, sessionId) {
    return getAgentDiagnostics(agentId, sessionId);
}
export function getAgentSessionBrowserCaptureClaim(metadata) {
    return getBrowserCaptureClaim(metadata);
}
export function setAgentSessionBrowserCaptureClaim(metadata, claim) {
    return setBrowserCaptureClaim(metadata, claim);
}
export function loadAgentSessionOperationHistory(agentId, limit) {
    return getAgentHistory(agentId, limit);
}
export function subscribeAgentSessionStream(input) {
    const releaseSubscription = subscribeAgent(input.agentId);
    const unsubscribeSocket = subscribeAgentSessionSocketMessages({
        addSocketMessageListener: input.addSocketMessageListener,
        onMessage: input.onMessage,
    });
    return () => {
        releaseSubscription();
        unsubscribeSocket();
    };
}
export function subscribeAgentSessionSocketMessages(input) {
    if (!input.addSocketMessageListener) {
        return () => { };
    }
    const unsubscribe = input.addSocketMessageListener(input.onMessage);
    return typeof unsubscribe === "function" ? unsubscribe : () => { };
}
export function registerAgentSessionDialogInstance(agentId, instanceId) {
    registerMountedInstance(agentId, instanceId);
}
export function unregisterAgentSessionDialogInstance(agentId, instanceId) {
    unregisterMountedInstance(agentId, instanceId);
}
export function setAgentSessionVisibleDialogEntry(input) {
    setVisibleDialogEntry(input.agentId, input.instanceId, input.callbackId, input.isFocused);
}
export function clearAgentSessionVisibleDialogEntries(agentIds, instanceId) {
    clearVisibleDialogEntriesForInstance(agentIds, instanceId);
}
export function updateAgentSessionDialogFocus(agentIds, instanceId, isFocused) {
    updateInstanceFocus(agentIds, instanceId, isFocused);
}
export function isAgentSessionDialogReceiverElected(agentId, instanceId) {
    return isElectedDialogReceiver(agentId, instanceId);
}
export function subscribeAgentSessionDialogEvents(handler) {
    window.addEventListener("agent:dialog:show", handler);
    return () => {
        window.removeEventListener("agent:dialog:show", handler);
    };
}
export function startAgentSessionRun(request) {
    return startAgent(request);
}
export function pushQueuedPromptSessionRun(request) {
    return pushQueuedPrompt(request);
}
export function deleteQueuedPromptSessionEntry(input) {
    return deleteQueuedPrompt(input);
}
export function cancelAgentSessionRun(agentId) {
    return cancelAgent(agentId);
}
export function claimAgentSessionBrowser(input) {
    return claimAgentBrowser(input);
}
export function releaseAgentSessionBrowser(input) {
    return releaseAgentBrowser(input);
}
export function acceptAgentSessionDialog(input) {
    return acceptAgentDialog(input);
}
export function cancelAgentSessionDialog(input) {
    return cancelAgentDialog(input);
}
export function submitAgentSessionDialogResponse(input) {
    return submitAgentDialogResponse(input);
}
export function replayAgentSessionDialog(input) {
    return replayAgentDialog(input);
}
export function rejectAgentSessionToolCall(input) {
    return rejectToolCall(input);
}
export function approveAgentSessionToolCall(input) {
    return approveToolCall(input);
}
export function updateAgentSessionSettings(agentId, updates) {
    return updateAgentSettings(agentId, updates);
}
export function updateAgentSessionContext(agentId, context) {
    return updateAgentContext(agentId, context);
}
export function updateAgentSessionCostLimit(agentId, action) {
    return updateAgentCostLimit(agentId, action);
}
export function persistDraftAgentSession(params) {
    return persistDraftAgent(params);
}
export function assignAgentSessionSkill(params) {
    return assignAgentSkill(params);
}
export function revokeAgentSessionSkill(params) {
    return revokeAgentSkill(params);
}
export function loadAgentSessionSkillCatalog(includeDisabled) {
    return getAgentSkillCatalog(includeDisabled);
}
export function loadAgentSessionAvailableTools(agentId) {
    return getAgentAvailableTools(agentId);
}
export function loadAgentSessionOperationAllowances(agentId) {
    return getAgentOperationAllowances(agentId);
}
export function loadAgentSessionLastPayload(agentId) {
    return getLastPayload(agentId);
}
export function loadAgentSessionTriggerSubscriptions(agentId) {
    return getAgentTriggerSubscriptions(agentId);
}
export function createAgentSessionOperationAllowance(input) {
    return createOperationAllowance(input);
}
async function loadPendingQueuedPrompts(agentId) {
    try {
        return await getPendingPrompts(agentId);
    }
    catch (error) {
        if (!isExpectedQueuedPromptsUnavailableError(error)) {
            console.error("Failed to fetch initial queued prompts:", error);
        }
        return [];
    }
}
function isExpectedQueuedPromptsUnavailableError(error) {
    const message = error instanceof Error ? error.message : String(error ?? "");
    return message.includes("404") || message.toLowerCase().includes("not found");
}
//# sourceMappingURL=useAgentSessionSync.js.map