// JavaScript Document
//v1.0.0
// may return status 202 as deletion is not always immediate
exports.onesignal_delete_subscription = async function (options) {
    // get appid
    const appid = this.parseOptional(options.appid, '*', process.env.ONESIGNAL_APP_ID)
    console.log(options.appid + " => " + appid)

    const subscription_id = this.parseRequired(options.subscription_id, '*', 'Subscription ID not specified')
    console.log('Subscription_id: ' + options.subscription_id + " => " + subscription_id)
    const fetch = require('node-fetch');

    const url = `https://api.onesignal.com/apps/${appid}/subscriptions/${subscription_id}`;
    const os_options = { method: 'DELETE', headers: { accept: 'application/json', Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}` } };

    return fetch(url, os_options)
        .then(res => {

            return res.json().then(json => {
                json.status = res.status; // Add status to json response
                return json;
            });
        })
        .catch(err => console.error('Error:', err));
}
