export class WebBrowser {
    browser = null;
    context = null;
    page = null;
    async launch(options = {}) {
        const { persistent = false, userDataDir, ...launchOptions } = options;
        if (persistent && userDataDir) {
            // For persistent contexts, use launchPersistentContext which returns a context directly
            this.context = await this.launchPersistentContext(userDataDir, launchOptions);
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            this.page = await this.context.newPage();
        }
        else {
            // For regular launch, CloakBrowser's launch() returns a Browser
            const { launch } = await import('cloakbrowser');
            this.browser = await launch(launchOptions);
            // Create a context from the browser (CloakBrowser extends Playwright API)
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            this.context = await this.browser.newContext();
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            this.page = await this.context.newPage();
        }
        return this;
    }
    async launchPersistentContext(userDataDir, options) {
        const { launchPersistentContext } = await import('cloakbrowser');
        // cloakbrowser's launchPersistentContext(userDataDir, options) takes options as second param
        return launchPersistentContext(userDataDir, options);
    }
    async goto(url) {
        if (!this.page)
            throw new Error('Browser not initialized');
        const response = await this.page['goto'](url);
        if (!response)
            return '0';
        // CloakBrowser/Playwright: status() is a method, not a property
        const status = typeof response.status === 'function' ? response.status() : response.status;
        return String(status || '0');
    }
    async getContent() {
        if (!this.page)
            throw new Error('Browser not initialized');
        return await this.page['content']();
    }
    async getText(selector) {
        if (!this.page)
            throw new Error('Browser not initialized');
        const element = await this.page['locator'](selector);
        return await element['textContent']();
    }
    async screenshot(path) {
        if (!this.page)
            throw new Error('Browser not initialized');
        await this.page['screenshot']({ path });
    }
    async evaluate(script) {
        if (!this.page)
            throw new Error('Browser not initialized');
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const page = this.page;
        // CloakBrowser requires a function, not a string
        // If a string is passed, wrap it in a function
        const func = typeof script === 'string' ? new Function('return (' + script + ')()') : script;
        return await page.evaluate(func);
    }
    async waitForLoadState(state = 'load') {
        if (!this.page)
            throw new Error('Browser not initialized');
        await this.page['waitForLoadState'](state);
    }
    async close() {
        if (this.context) {
            await this.context['close']();
        }
        else if (this.browser) {
            await this.browser['close']();
        }
        this.browser = null;
        this.context = null;
        this.page = null;
    }
    async findElements(selector) {
        if (!this.page)
            throw new Error('Browser not initialized');
        const elements = await this.page['locator'](selector)['all']();
        return (elements?.map((el) => ({
            textContent: el['textContent'](),
            exists: el['count'](),
        })) || []);
    }
}
export const createWebBrowser = () => new WebBrowser();
//# sourceMappingURL=web-browser.js.map