"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const PropertySymbol = __importStar(require("../../PropertySymbol.cjs"));
const BrowserFrameFactory_js_1 = __importDefault(require("./BrowserFrameFactory.cjs"));
const BrowserFrameURL_js_1 = __importDefault(require("./BrowserFrameURL.cjs"));
const BrowserFrameValidator_js_1 = __importDefault(require("./BrowserFrameValidator.cjs"));
const AsyncTaskManager_js_1 = __importDefault(require("../../async-task-manager/AsyncTaskManager.cjs"));
const HistoryScrollRestorationEnum_js_1 = __importDefault(require("../../history/HistoryScrollRestorationEnum.cjs"));
/**
 * Browser frame navigation utility.
 */
class BrowserFrameNavigator {
    /**
     * Navigates to a page.
     *
     * @throws Error if the request can't be resolved (because of SSL error or similar). It will not throw if the response is not ok.
     * @param options Options.
     * @param options.windowClass Window class.
     * @param options.frame Frame.
     * @param options.url URL.
     * @param [options.goToOptions] Go to options.
     * @param [options.method] Method.
     * @param [options.formData] Form data.
     * @param [options.disableHistory] Disables adding the navigation to the history.
     * @returns Response.
     */
    static async navigate(options) {
        const { windowClass, frame, url, formData, method, goToOptions, disableHistory } = options;
        const exceptionObserver = frame.page.context.browser[PropertySymbol.exceptionObserver];
        const referrer = goToOptions?.referrer || frame.window.location.origin;
        const targetURL = BrowserFrameURL_js_1.default.getRelativeURL(frame, url);
        const resolveNavigationListeners = () => {
            const listeners = frame[PropertySymbol.listeners].navigation;
            frame[PropertySymbol.listeners].navigation = [];
            for (const listener of listeners) {
                listener();
            }
        };
        if (!frame.window) {
            throw new Error('The frame has been destroyed, the "window" property is not set.');
        }
        // Javascript protocol
        if (targetURL.protocol === 'javascript:') {
            if (frame && !frame.page.context.browser.settings.disableJavaScriptEvaluation) {
                const readyStateManager = frame.window[PropertySymbol.readyStateManager];
                readyStateManager.startTask();
                const code = '//# sourceURL=' + frame.url + '\n' + targetURL.href.replace('javascript:', '');
                // The browser will wait for the next tick before executing the script.
                // Fixes issue where evaluating the response can throw an error.
                // By using requestAnimationFrame() the error will not reject the promise.
                // The error will be caught by process error level listener or a try and catch in the requestAnimationFrame().
                await new Promise((resolve) => {
                    frame.window.requestAnimationFrame(() => {
                        frame.window.requestAnimationFrame(resolve);
                        frame.window.eval(code);
                    });
                });
                readyStateManager.endTask();
                resolveNavigationListeners();
            }
            return null;
        }
        // Validate navigation
        if (!BrowserFrameValidator_js_1.default.validateCrossOriginPolicy(frame, targetURL)) {
            return null;
        }
        if (!BrowserFrameValidator_js_1.default.validateFrameNavigation(frame)) {
            if (!frame.page.context.browser.settings.navigation.disableFallbackToSetURL) {
                frame.window.location[PropertySymbol.setURL](frame, targetURL.href);
            }
            return null;
        }
        // History management.
        if (!disableHistory) {
            const history = frame[PropertySymbol.history];
            for (let i = history.length - 1; i >= 0; i--) {
                if (history[i].isCurrent) {
                    history[i].isCurrent = false;
                    // We need to remove all history items after the current one.
                    history.length = i + 1;
                    break;
                }
            }
            history.push({
                title: '',
                href: targetURL.href,
                state: null,
                scrollRestoration: HistoryScrollRestorationEnum_js_1.default.auto,
                method: method || (formData ? 'POST' : 'GET'),
                formData: formData || null,
                isCurrent: true
            });
        }
        // Store current Window state
        const previousWindow = frame.window;
        const previousAsyncTaskManager = frame[PropertySymbol.asyncTaskManager];
        const width = previousWindow.innerWidth;
        const height = previousWindow.innerHeight;
        const devicePixelRatio = previousWindow.devicePixelRatio;
        const parentWindow = frame.parentFrame ? frame.parentFrame.window : frame.page.mainFrame.window;
        const topWindow = frame.page.mainFrame.window;
        // Create new Window
        frame[PropertySymbol.asyncTaskManager] = new AsyncTaskManager_js_1.default(frame);
        frame.window = new windowClass(frame, { url: targetURL.href, width, height });
        frame.window[PropertySymbol.parent] = parentWindow;
        frame.window[PropertySymbol.top] = topWindow;
        frame.window.devicePixelRatio = devicePixelRatio;
        if (exceptionObserver) {
            exceptionObserver.observe(frame.window);
        }
        if (referrer) {
            frame.window.document[PropertySymbol.referrer] = referrer;
        }
        // Destroy child frames and Window
        const destroyTaskID = frame[PropertySymbol.asyncTaskManager].startTask();
        const destroyWindowAndAsyncTaskManager = () => {
            previousAsyncTaskManager.destroy().then(() => {
                if (exceptionObserver) {
                    exceptionObserver.disconnect(previousWindow);
                }
                frame[PropertySymbol.asyncTaskManager].endTask(destroyTaskID);
            });
            previousWindow[PropertySymbol.destroy]();
        };
        if (frame.childFrames.length) {
            Promise.all(frame.childFrames.map((childFrame) => BrowserFrameFactory_js_1.default.destroyFrame(childFrame))).then(destroyWindowAndAsyncTaskManager);
        }
        else {
            destroyWindowAndAsyncTaskManager();
        }
        // About protocol
        if (targetURL.protocol === 'about:') {
            await new Promise((resolve) => frame.page.mainFrame.window.requestAnimationFrame(resolve));
            resolveNavigationListeners();
            return null;
        }
        // Start navigation
        const readyStateManager = frame.window[PropertySymbol.readyStateManager];
        const abortController = new frame.window.AbortController();
        const timeout = frame.window.setTimeout(() => abortController.abort(new Error('Request timed out.')), goToOptions?.timeout ?? 30000);
        const finalize = () => {
            frame.window.clearTimeout(timeout);
            readyStateManager.endTask();
            resolveNavigationListeners();
        };
        let response;
        let responseText;
        readyStateManager.startTask();
        try {
            response = await frame.window.fetch(targetURL.href, {
                referrer,
                referrerPolicy: goToOptions?.referrerPolicy || 'origin',
                signal: abortController.signal,
                method: method || (formData ? 'POST' : 'GET'),
                headers: goToOptions?.hard ? { 'Cache-Control': 'no-cache' } : undefined,
                body: formData
            });
            // Handles the "X-Frame-Options" header for child frames.
            if (frame.parentFrame) {
                const originURL = frame.parentFrame.window.location;
                const xFrameOptions = response.headers?.get('X-Frame-Options')?.toLowerCase();
                const isSameOrigin = originURL.origin === targetURL.origin || targetURL.origin === 'null';
                if (xFrameOptions === 'deny' || (xFrameOptions === 'sameorigin' && !isSameOrigin)) {
                    throw new Error(`Refused to display '${url}' in a frame because it set 'X-Frame-Options' to '${xFrameOptions}'.`);
                }
            }
            responseText = await response.text();
        }
        catch (error) {
            finalize();
            throw error;
        }
        if (!response.ok) {
            frame.page.console.error(`GET ${targetURL.href} ${response.status} (${response.statusText})`);
        }
        // The frame may be destroyed during teardown.
        if (!frame.window) {
            return;
        }
        // Fixes issue where evaluating the response can throw an error.
        // By using requestAnimationFrame() the error will not reject the promise.
        // The error will be caught by process error level listener or a try and catch in the requestAnimationFrame().
        await new Promise((resolve) => {
            frame.window.requestAnimationFrame(() => {
                frame.window.requestAnimationFrame(resolve);
                frame.content = responseText;
            });
        });
        finalize();
        return response;
    }
    /**
     * Navigates back in history.
     *
     * @param options Options.
     * @param options.windowClass Window class.
     * @param options.frame Frame.
     * @param [options.goToOptions] Go to options.
     */
    static navigateBack(options) {
        const { windowClass, frame, goToOptions } = options;
        const history = frame[PropertySymbol.history];
        let historyItem;
        for (let i = history.length - 1; i >= 0; i--) {
            if (history[i].isCurrent) {
                if (i > 0) {
                    history[i].isCurrent = false;
                    historyItem = history[i - 1];
                }
                break;
            }
        }
        if (!historyItem) {
            return new Promise((resolve) => {
                frame.window.requestAnimationFrame(() => {
                    const listeners = frame[PropertySymbol.listeners].navigation;
                    frame[PropertySymbol.listeners].navigation = [];
                    for (const listener of listeners) {
                        listener();
                    }
                    resolve(null);
                });
            });
        }
        historyItem.isCurrent = true;
        return BrowserFrameNavigator.navigate({
            windowClass,
            frame,
            goToOptions: {
                ...goToOptions,
                referrer: frame.url
            },
            url: historyItem.href,
            method: historyItem.method,
            formData: historyItem.formData,
            disableHistory: true
        });
    }
    /**
     * Navigates forward in history.
     *
     * @param options Options.
     * @param options.windowClass Window class.
     * @param options.frame Frame.
     * @param [options.goToOptions] Go to options.
     */
    static navigateForward(options) {
        const { windowClass, frame, goToOptions } = options;
        const history = frame[PropertySymbol.history];
        let historyItem;
        for (let i = history.length - 1; i >= 0; i--) {
            if (history[i].isCurrent) {
                if (i < history.length - 1) {
                    history[i].isCurrent = false;
                    historyItem = history[i + 1];
                }
                break;
            }
        }
        if (!historyItem) {
            return new Promise((resolve) => {
                frame.window.requestAnimationFrame(() => {
                    const listeners = frame[PropertySymbol.listeners].navigation;
                    frame[PropertySymbol.listeners].navigation = [];
                    for (const listener of listeners) {
                        listener();
                    }
                    resolve(null);
                });
            });
        }
        historyItem.isCurrent = true;
        return BrowserFrameNavigator.navigate({
            windowClass,
            frame,
            goToOptions: {
                ...goToOptions,
                referrer: frame.url
            },
            url: historyItem.href,
            method: historyItem.method,
            formData: historyItem.formData,
            disableHistory: true
        });
    }
    /**
     * Navigates steps in history.
     *
     * @param options Options.
     * @param options.windowClass Window class.
     * @param options.frame Frame.
     * @param options.goToOptions Go to options.
     * @param options.steps Steps.
     */
    static navigateSteps(options) {
        if (!options.steps) {
            return this.reload(options);
        }
        const { windowClass, frame, goToOptions, steps } = options;
        const history = frame[PropertySymbol.history];
        let historyItem;
        for (let i = history.length - 1; i >= 0; i--) {
            if (history[i].isCurrent) {
                if (history[i + steps]) {
                    history[i].isCurrent = false;
                    historyItem = history[i + steps];
                }
                break;
            }
        }
        if (!historyItem) {
            return new Promise((resolve) => {
                frame.window.requestAnimationFrame(() => {
                    const listeners = frame[PropertySymbol.listeners].navigation;
                    frame[PropertySymbol.listeners].navigation = [];
                    for (const listener of listeners) {
                        listener();
                    }
                    resolve(null);
                });
            });
        }
        historyItem.isCurrent = true;
        return BrowserFrameNavigator.navigate({
            windowClass,
            frame,
            goToOptions: {
                ...goToOptions,
                referrer: frame.url
            },
            url: historyItem.href,
            method: historyItem.method,
            formData: historyItem.formData,
            disableHistory: true
        });
    }
    /**
     * Reloads the current history item.
     *
     * @param options Options.
     * @param options.windowClass Window class.
     * @param options.frame Frame.
     * @param options.goToOptions Go to options.
     */
    static reload(options) {
        const { windowClass, frame, goToOptions } = options;
        const history = frame[PropertySymbol.history];
        let historyItem;
        for (let i = history.length - 1; i >= 0; i--) {
            if (history[i].isCurrent) {
                historyItem = history[i];
                break;
            }
        }
        if (!historyItem) {
            return new Promise((resolve) => {
                frame.window.requestAnimationFrame(() => {
                    const listeners = frame[PropertySymbol.listeners].navigation;
                    frame[PropertySymbol.listeners].navigation = [];
                    for (const listener of listeners) {
                        listener();
                    }
                    resolve(null);
                });
            });
        }
        return BrowserFrameNavigator.navigate({
            windowClass,
            frame,
            goToOptions: {
                ...goToOptions,
                referrer: frame.url
            },
            url: historyItem.href,
            method: historyItem.method,
            formData: historyItem.formData,
            disableHistory: true
        });
    }
}
exports.default = BrowserFrameNavigator;
//# sourceMappingURL=BrowserFrameNavigator.cjs.map