const axios = require('axios')
// const { datacatalog } = require('googleapis/build/src/apis/datacatalog')

module.exports = function registerWebhooks(shopifyApiKey,shopifySecretKey,store) {
    return new Promise((resolve,reject) => {
        
        axios.get(`https://${shopifyApiKey}:${shopifySecretKey}@${store}/admin/api/2024-04/webhooks/count.json`)
            .then(response => {
                
                const webhookData = response.data;
                if (webhookData.count == 0) {
                    let webhookUrl = `https://${shopifyApiKey}:${shopifySecretKey}@${store}/admin/api/2024-04/webhooks.json`;
    
                    // const webhookData = {
                    // webhook: {
                    //     topic: 'customers/create',
                    //     address: `${process.env.APP_URL}/sscapi/webhook/customer_create`,
                    //     format: 'json',
                    // },
                    // };
                    
                    const webhookData = {
                        webhook: {
                            topic: 'user/getdata',
                            address: `${process.env.APP_URL}/shopperrds/getMerchant`,
                            format: 'json',
                        },
                    };
                    
                    axios.post(webhookUrl, webhookData)
                    .then(customerWebhookData => {
                        console.log(customerWebhookData)
                        const webhookData2 = {
                            webhook: {
                                topic: 'orders/create',
                                address: `${process.env.APP_URL}/shopperrds/webhook/orders_create`,
                                format: 'json',
                            },
                        };
                        axios.post(webhookUrl, webhookData2)
                        .then(orderWebhookData => {
                            const webhookData3 = {
                                webhook: {
                                    topic: 'products/update',
                                    address: `${process.env.APP_URL}/shopperrds/webhookUpdate`,
                                    format: 'json'
                                },
                            };
                            axios.post(webhookUrl, webhookData3)
                                .then(productdata => {
                                    resolve(orderWebhookData.data)
                                })
                        })
                        .catch(err => {
                            
                            reject(err)
                        })
                    })
                    .catch(err => {
                        
                        reject(err)
                    })
                } else {
                    // axios.delete(`https://${shopifyApiKey}:${shopifySecretKey}@${store}/admin/api/2020-07/webhooks/${1016615108800}.json`)
                    //     .then(data => {
                    //         

                    //     })
                    axios.get(`https://${shopifyApiKey}:${shopifySecretKey}@${store}/admin/api/2020-07/webhooks.json`)
                    .then(result => {
                        
                        resolve(result.data)
                    })
                }
    
            })
            .catch(err => {
                console.log(err)
            })
    })
}