export const CAPTURE_ERROR_CODES = {
    BROWSER_CLAIM_MISMATCH: "browser_claim_mismatch",
    BROWSER_CONTEXT_MISSING: "browser_context_missing",
    PAGE_VIEWER_NOT_OPEN: "page_viewer_not_open",
};
function normalize(value) {
    return value?.trim().toLowerCase() || "";
}
export function getBrowserCaptureClaim(metadata) {
    const raw = metadata?.additionalData?.browserCaptureClaim ||
        metadata
            ?.browserCaptureClaim;
    if (!raw || typeof raw !== "object") {
        return null;
    }
    return raw;
}
export function setBrowserCaptureClaim(metadata, claim) {
    const nextAdditionalData = { ...(metadata?.additionalData || {}) };
    const nextMetadata = {
        ...(metadata || {}),
        additionalData: nextAdditionalData,
    };
    if (claim) {
        nextAdditionalData.browserCaptureClaim = claim;
        nextMetadata.browserCaptureClaim = claim;
    }
    else {
        delete nextAdditionalData.browserCaptureClaim;
        delete nextMetadata.browserCaptureClaim;
    }
    return nextMetadata;
}
export function buildOpenCaptureContext(editContext) {
    const currentItem = editContext?.currentItemDescriptor;
    if (!currentItem && !editContext?.workspaceId) {
        return undefined;
    }
    return {
        itemId: currentItem?.id,
        itemName: editContext?.item?.name,
        itemPath: currentItem?.path,
        language: currentItem?.language,
        version: currentItem?.version,
        activeWorkspace: editContext?.workspaceId,
    };
}
export function validateBrowserBoundCaptureRequest(params) {
    const { request, editContext, scope } = params;
    const requiredContext = resolveEffectiveTargetContext(request, editContext);
    const currentContext = buildOpenCaptureContext(editContext);
    if (request.targetSessionId &&
        editContext?.sessionId &&
        normalize(request.targetSessionId) !== normalize(editContext.sessionId)) {
        return buildCaptureFailure({
            scope,
            errorCode: CAPTURE_ERROR_CODES.BROWSER_CLAIM_MISMATCH,
            error: "This browser is not the claimed browser for the capture request.",
            currentContext,
            requiredContext,
            recommendedAction: "Attach this agent to the current browser or switch back to the claimed browser, then retry capture.",
        });
    }
    if (scope === "editor-ui") {
        return null;
    }
    if (!requiredContext?.itemId) {
        return buildCaptureFailure({
            scope,
            errorCode: CAPTURE_ERROR_CODES.BROWSER_CONTEXT_MISSING,
            error: "The capture request is missing a concrete page target, so it cannot safely run against the currently open page.",
            currentContext,
            requiredContext,
            recommendedAction: "Refresh the agent context or load the intended item first, then retry capture.",
        });
    }
    return null;
}
export function buildPageViewerNotOpenFailure(params) {
    const requiredContext = resolveEffectiveTargetContext(params.request, params.editContext);
    return buildCaptureFailure({
        scope: params.scope,
        errorCode: CAPTURE_ERROR_CODES.PAGE_VIEWER_NOT_OPEN,
        error: "No page viewer or editor surface is open in the claimed browser, so capture cannot run.",
        currentContext: buildOpenCaptureContext(params.editContext),
        requiredContext,
        recommendedAction: buildLoadItemAction(requiredContext),
    });
}
function buildLoadItemAction(targetContext) {
    if (!targetContext?.itemId) {
        return "Open the intended page in the page editor or viewer, then retry capture.";
    }
    const parts = [`Call load-item with itemId "${targetContext.itemId}"`];
    if (targetContext.language) {
        parts.push(`language "${targetContext.language}"`);
    }
    if (targetContext.version != null) {
        parts.push(`version ${targetContext.version}`);
    }
    return `${parts.join(", ")}, then retry capture.`;
}
function resolveEffectiveTargetContext(request, editContext) {
    const requestedContext = request.parameters?.targetContext || undefined;
    if (requestedContext?.itemId) {
        return requestedContext;
    }
    const currentContext = buildOpenCaptureContext(editContext);
    if (!currentContext?.itemId) {
        return requestedContext;
    }
    request.parameters = {
        ...(request.parameters || {}),
        targetContext: currentContext,
    };
    return currentContext;
}
function buildCaptureFailure(params) {
    return {
        success: false,
        scope: params.scope,
        errorCode: params.errorCode,
        error: params.recommendedAction
            ? `${params.error} ${params.recommendedAction}`
            : params.error,
        requiredContext: params.requiredContext,
        currentContext: params.currentContext,
        recommendedAction: params.recommendedAction,
    };
}
//# sourceMappingURL=browserBoundCapture.js.map