import json
import os
import time

from agent_amory_core.browser.browser import Browser, BrowserConfig


async def test_process_dom():
    browser = Browser(config=BrowserConfig(headless=False))

    async with await browser.new_context() as context:
        page = await context.get_current_page()
        await page.goto('https://kayak.com/flights')
        # await page.goto('https://google.com/flights')
        # await page.goto('https://immobilienscout24.de')
        # await page.goto('https://seleniumbase.io/w3schools/iframes')

        time.sleep(3)

        with open('agent_amory_core/dom/process_dom.js', 'r') as f:
            js_code = f.read()

        start = time.time()
        dom_tree = await page.evaluate(js_code)
        end = time.time()

        # print(dom_tree)
        print(f'Time: {end - start:.2f}s')

        os.makedirs('./tmp', exist_ok=True)
        with open('./tmp/dom.json', 'w') as f:
            json.dump(dom_tree, f, indent=1)

        # both of these work for immobilienscout24.de
        # await page.click('.sc-dcJsrY.ezjNCe')
        # await page.click(
        # 	'div > div:nth-of-type(2) > div > div:nth-of-type(2) > div > div:nth-of-type(2) > div > div > div > button:nth-of-type(2)'
        # )

        input('Press Enter to continue...')
