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

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

LET resultListSelector = 'div.s-result-list'
LET resultItemSelector = 'div.s-result-item'
LET nextBtnSelector = 'ul.a-pagination .a-last a'
LET vendorSelector1 = 'div > div:nth-child(3) > div:nth-child(2) > span:nth-child(2)'
LET vendorSelector2 = 'div > div:nth-child(5) > div:nth-child(2) > span:nth-child(2)'
LET priceWholeSelector = 'span.sx-price-whole'
LET priceFracSelector = 'sup.sx-price-fractional'
LET pagers = ELEMENTS(amazon, 'ul.a-pagination li.a-disabled')
LET pages = LENGTH(pagers) > 0 ? TO_INT(INNER_TEXT(LAST(pagers))) : 0

LET result = (
    FOR pageNum IN 1..pages
        LET clicked = pageNum == 1 ? false : CLICK(amazon, nextBtnSelector)
        LET wait = ELEMENT_EXISTS(amazon, nextBtnSelector) ? WAIT_NAVIGATION(amazon) : false
        LET waitSelector = wait ? WAIT_ELEMENT(amazon, resultListSelector) : false

        PRINT("page:", pageNum, "clicked", clicked)

        LET items = (
            FOR el IN ELEMENTS(amazon, resultItemSelector)
                LET priceWholeTxt = INNER_TEXT(el, priceWholeSelector)
                LET priceFracTxt = INNER_TEXT(el, priceFracSelector)
		LET price = TO_FLOAT(priceWholeTxt + "." + priceFracTxt)
                LET vendor = ELEMENT_EXISTS(el, vendorSelector1) ? INNER_TEXT(el, vendorSelector1) : INNER_TEXT(el, vendorSelector2)

                RETURN {
                    title: INNER_TEXT(el, 'h2'),
                    vendor,
                    price
                }
        )

        RETURN items
)

RETURN FLATTEN(result)
