LET baseURL = 'https://www.amazon.com/'
LET amazon = DOCUMENT(baseURL, { driver: "cdp" })

INPUT(amazon, '#twotabsearchtextbox', @criteria)
CLICK(amazon, '.nav-search-submit input[type="submit"]')
WAIT_NAVIGATION(amazon)

LET resultListSelector = '#s-results-list-atf'
LET resultItemSelector = '[data-component-type="s-search-result"]'
LET nextBtnSelector = 'ul.a-pagination .a-last a'
LET priceWholeSelector = '.a-price-whole'
LET priceFracSelector = '.a-price-fraction'

LET result = (
    FOR pageNum IN PAGINATION(amazon, nextBtnSelector)
        LIMIT @pages

        LET wait = pageNum > 0 ? WAIT_NAVIGATION(amazon, 20000) : false
        LET waitSelector = wait ? WAIT_ELEMENT(amazon, resultListSelector) : false

        LET items = (
            FOR el IN ELEMENTS(amazon, resultItemSelector)
                LET hasPrice = ELEMENT_EXISTS(el, priceWholeSelector)
                LET priceWholeTxt = hasPrice ? FIRST(REGEX_MATCH(INNER_TEXT(el, priceWholeSelector), "[0-9]+")) : "0"
                LET priceFracTxt = hasPrice ? FIRST(REGEX_MATCH(INNER_TEXT(el, priceFracSelector), "[0-9]+")) : "00"
		        LET price = TO_FLOAT(priceWholeTxt + "." + priceFracTxt)
		        LET anchor = ELEMENT(el, "a")

		        PRINT(priceWholeTxt, priceFracTxt)

                RETURN {
                    url: baseURL + anchor.attributes.href,
                    title: INNER_TEXT(el, 'h2'),
                    price
                }
        )

        RETURN items
)

RETURN FLATTEN(result)