
// JavaScript Document
exports.onesignal_delete_user_alias = async function (options) {
    // get appid
    const appid = this.parseOptional(options.appid, '*', process.env.ONESIGNAL_APP_ID)
    console.log(options.appid + " => " + appid)

    const alias_id = this.parseRequired(options.alias_id, '*', 'No Alias ID specified')
    console.log(options.alias_id + " => " + alias_id)

    const alias_label = this.parseRequired(options.alias_label, '*', 'No Alias Label specified')
    console.log(options.alias_label + " => " + alias_label)

    const alias_label_to_delete = this.parseRequired(options.alias_label_to_delete, '*', 'No Alias Label to delete specified')
    console.log(options.alias_label_to_delete + " => " + alias_label_to_delete)

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

    const url = `https://api.onesignal.com/apps/${appid}/users/by/${alias_label}/${alias_id}/identity/${alias_label_to_delete}`;
    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));
}

