const fetch = require("node-fetch");


const Client = {
    headers: {
        authority: 'www.coupang.com',
        method: 'GET',
        path: '/np/search?q=%ED%8F%AC%EB%8F%84%20%EC%A5%AC%EC%8A%A4%20100%EC%8D%AC%ED%82%A4%EC%8A%A4%ED%8A%B8&channel=auto',
        scheme: 'https',
        accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7',
        cookie: "_ga=GA1.2.1386538425.1560825711; PCID=19550425343340000597538; _fbp=fb.1.1563777394391.2142682331; cto_lwid=ff42c887-0098-46e4-9738-8eee9bc7393e; _gcl_au=1.1.469705637.1571115306; __zlcmid=unicCiEjA7VeWr; sc_vid=A00184338; sc_lid=dj6188; sid=51e0b052813b45fda35c40ba5b01652f134eb030; overrideAbTestGroup=%5B%5D; ak_bmsc=A2A2E91B760CF5C23E61C8150992B9AF173B970621600000E5BCE55DEFFD3154~plOeJYAYUBKVBgxj5oANZ316ZfY8cfbKw34468AguY8j7a8XgPAc9loigFTrSvW52mZM1ldgvbq6ID5HXk3IDrYotWlUTLYleW+m+kcS7SNnNc/IudabInCQE8m1MbWloeeFk9jz/Gj0qw7oVWs+cDrISeUqIPvl2QG6dpnAaiiRI84mKR8a1I5nsfCBkaPEl/LAvn4QN1eyIs08uw81iBv6cRk03M7VTizk2IJzRhO0DY83gwKsrYfZVM8ykV20ojGNi/oXpNZYglQI3Zt64d1NTCg//2tZ/i/WkpJTK5y2sH134lKLnqrMF2vst+go5bjzKEvfOPy8AekfNOxJIdeg==; trac_src=1042016; trac_spec=10304902; trac_addtag=300; trac_ctag=RSA1; trac_lptag=coupang; trac_itime=20191203103955; FUN=\"{'search':[{'reqUrl':'/search.pang','isValid':true}]}\"; bm_sv=52925047624FF700A858905614E5AF1A~jn8BBjMv9M1HmQ0wmZYwH5TG0RcfoTaYqS2d1X29x27U4to8LGD1Pu/V99nHQCm/u3Ghnsg3LA1nWVW+FyII5jIY2xp7EqrB4aja7lQiGix6dDzuuFuS2vBOAWwS8CC6BeKVtMdy8cxZbTbA17YVyAF4MjYg8qgKm+glTT+FJRA=; baby-isWide=wide; searchKeyword=%ED%8F%AC%EB%8F%84%7C%ED%8F%AC%EB%8F%84%20%EC%A5%AC%EC%8A%A4%20100%7C%ED%8F%AC%EB%8F%84%20%EC%A5%AC%EC%8A%A4%20100%EC%8D%AC%ED%82%A4%EC%8A%A4%ED%8A%B8; searchKeywordType=%7B%22%ED%8F%AC%EB%8F%84%22%3A0%7D%7C%7B%22%ED%8F%AC%EB%8F%84%20%EC%A5%AC%EC%8A%A4%20100%22%3A0%7D%7C%7B%22%ED%8F%AC%EB%8F%84%20%EC%A5%AC%EC%8A%A4%20100%EC%8D%AC%ED%82%A4%EC%8A%A4%ED%8A%B8%22%3A0%7D",
        referer: 'https://www.coupang.com/np/search?q=%ED%8F%AC%EB%8F%84%20%EC%A5%AC%EC%8A%A4%20100&channel=auto',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
    },
    request: async (args) => {
        let url = Client[args.call](args)
        Client.headers['path'] = url.replace(args.coupang.data.url, '')
        return fetch(url, { headers: Client.headers }).then(res => res.textConverted());
    },
    search: (args) => {
        let url = args.coupang.data.url + '/np/search?'+Object.keys(args.query).map(key => key + '=' + encodeURIComponent(args.query[key])).join('&');
        console.log('- request product offer listing page url : ' + url);
        return url;
    }    
}

module.exports = Client;