const cheerio = require("cheerio");

const Parser = {
    categories: body => {
        let data = []
        const $ = cheerio.load(body)
        $(".a-container a").map((k, v) => {
            data.push({
                link: $(v).attr('href'),
                title: $(v).text()
            });
        })
        console.log('- fetching ' + data.length + ' category links completed')
        return data
    },
    subCategories: body => {
        let data = []
        const $ = cheerio.load(body)
        $(".left_nav.browseBox li a").map((k, v) => {
            data.push({
                link: $(v).attr('href'),
                title: $(v).text()
            });
        })
        return data;
    },
    productLinks: body => {
        let data = []
        const $ = cheerio.load(body)
        $("#mainResults a.a-link-normal").map((k, v) => {
            let title = $(v).find('h2').text().trim();
            if (title) {
                data.push({
                    link: $(v).attr('href'),
                    title: title
                });
            }
        })
        // console.log(data)
        amazon.us.data.subcategories = data;
        data.forEach((v, k) => {
            console.log(v.link)
        })
    },

    offerListing: body => {
        const $ = cheerio.load(body)
        let stock = $('#olpOfferList .a-row.olpOffer').length;
        let price = $('#olpOfferList div.a-column.a-span2.olpPriceColumn').last().find('span').text().trim();
        return {stock: stock, price: price}
    }
}

module.exports = Parser