const Product = require('./Product')
    , parser = require('./Parser')
    , client = require('./Client');



const amazon = {
    us: {
        data: {
            url: 'https://www.amazon.com',
            categories: {},
            subcategories: {}
        },
        utils: {
            fakeTitle: (length, calls, leadingTxt) => {
                let result = '';
                let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
                let charactersLength = characters.length;
                for (var i = 0; i < length; i++) {
                    result += characters.charAt(Math.floor(Math.random() * charactersLength));
                }
                let lengthRand = length //Math.ceil(Math.random(3, length))
                return --calls ? amazon.us.utils.fakeTitle(lengthRand, calls, leadingTxt + result + '-') : leadingTxt + result;
            }
        },
        task: {
            categories: () => {
                return client.request({ amazon: amazon, call: 'categories' }).then(parser.categories);
            },
            subCategories: (category) => {
                return client.request({ amazon: amazon, category: category, call: 'subCategories' }).then(parser.subCategories);
            },

            productLink: (link) => {
                return client.request({ amazon: amazon, link: link, call: 'productLinks' }).then(parser.productLinks);
            },

            productDetail: (asin) => {
                let product = new Product()
                return product.fetch(amazon, asin)
            },

            offerListing: (asin) => {
                return client.request({ amazon: amazon, asin:asin, call: 'offerListing' }).then(parser.offerListing);
            }
        }
    }

}

module.exports = amazon;