import { buildPageViewerNotOpenFailure, validateBrowserBoundCaptureRequest, } from "./browserBoundCapture";
function getActiveEditorIframe(editContext) {
    return editContext.getActiveSlotContext?.()?.primaryPageViewContext
        .editorIframe;
}
export async function capturePageDom(request, _params, editContext) {
    if (!editContext) {
        return buildPageViewerNotOpenFailure({
            request,
            editContext,
            scope: "page",
        });
    }
    const iframe = getActiveEditorIframe(editContext);
    if (!iframe) {
        return buildPageViewerNotOpenFailure({
            request,
            editContext,
            scope: "page",
        });
    }
    const validationFailure = validateBrowserBoundCaptureRequest({
        request,
        editContext,
        scope: "page",
    });
    if (validationFailure) {
        return validationFailure;
    }
    let iframeDocument = null;
    let html = "";
    try {
        iframeDocument = iframe.contentDocument;
        html = iframeDocument?.documentElement?.outerHTML ?? "";
    }
    catch {
        return {
            success: false,
            scope: "page",
            error: "The current page iframe is not accessible from the editor, so DOM capture is unavailable.",
        };
    }
    if (!iframeDocument?.documentElement) {
        return {
            success: false,
            scope: "page",
            error: "The current page has not finished loading inside the editor iframe.",
        };
    }
    if (!html.trim()) {
        return {
            success: false,
            scope: "page",
            error: "The current page DOM is empty.",
        };
    }
    return {
        success: true,
        scope: "page",
        html,
        length: html.length,
        documentTitle: iframeDocument.title || undefined,
    };
}
//# sourceMappingURL=capturePageDom.js.map