import { CHATGPT_URL } from "../browser/constants.js";
import { buildConversationUrl } from "../browser/reattachHelpers.js";
import { resolveRecoveryUrl } from "../browser/recoverConversation.js";
import { isRecoverableChatGptConversationUrl } from "../browser/reattachability.js";
import { DEFAULT_MODEL } from "../oracle/config.js";
/**
 * Resolve the ChatGPT conversation URL to reopen for a browser follow-up.
 *
 * Reuses the same recoverable-URL gate as conversation recovery
 * (`resolveRecoveryUrl`): prefer the post-harvest URL, fall back to the
 * runtime tab URL, and reject home / project-shell / external URLs via
 * `isRecoverableChatGptConversationUrl`. Only when neither candidate is a
 * recoverable `chatgpt.com/c/<id>` URL do we rebuild from a stored
 * `conversationId` against the session's ChatGPT base — and that rebuilt URL is
 * gated too. This prevents a stale or attacker-controlled URL in session
 * metadata from navigating the signed-in browser profile somewhere unintended.
 */
export function resolveBrowserResumeConversationUrl(metadata, fallbackBaseUrl = CHATGPT_URL) {
    const gatedUrl = resolveRecoveryUrl(metadata);
    if (gatedUrl) {
        return gatedUrl;
    }
    const conversationId = metadata.browser?.runtime?.conversationId?.trim();
    if (!conversationId) {
        return null;
    }
    const baseUrl = metadata.browser?.config?.url ?? fallbackBaseUrl;
    const built = buildConversationUrl({ conversationId }, baseUrl);
    if (built && isRecoverableChatGptConversationUrl(built)) {
        return built;
    }
    return null;
}
export async function resolveBrowserFollowupReference(value, store) {
    const trimmed = value.trim();
    if (!trimmed || trimmed.startsWith("resp_")) {
        return null;
    }
    const metadata = await store.readSession(trimmed);
    if (!metadata) {
        return null;
    }
    const mode = metadata.mode ?? metadata.options?.mode;
    const hasBrowserMetadata = Boolean(metadata.browser?.runtime || metadata.browser?.config || metadata.options?.browserConfig);
    if (mode !== "browser" && !hasBrowserMetadata) {
        return null;
    }
    const resumeConversationUrl = resolveBrowserResumeConversationUrl(metadata);
    if (!resumeConversationUrl) {
        throw new Error(`Session ${trimmed} is a browser session but does not contain a ChatGPT conversation URL. Run "oracle status --hours 72 --limit 20" to list recent sessions.`);
    }
    const parentBrowserConfig = metadata.options?.browserConfig ?? metadata.browser?.config;
    if (!parentBrowserConfig) {
        throw new Error(`Session ${trimmed} is missing its stored browser configuration.`);
    }
    const storedModel = metadata.options?.model ?? metadata.model;
    const model = typeof storedModel === "string" && storedModel.startsWith("gpt-")
        ? storedModel
        : DEFAULT_MODEL;
    return {
        sessionId: metadata.id,
        resumeConversationUrl,
        model,
        browserConfig: {
            ...parentBrowserConfig,
            browserTabRef: null,
            resumeConversationUrl,
            researchMode: "off",
            archiveConversations: "never",
        },
    };
}
