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

    const template_id = this.parseRequired(options.template_id, '*', 'Template ID not specified')
    console.log('Template_id: ' + options.template_id + " => " + template_id)

    const target_appid = this.parseRequired(options.target_appid, '*', 'Target APP ID not specified')
    console.log('target_appid: ' + options.target_appid + " => " + target_appid)

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

    const url = `https://api.onesignal.com/templates/${template_id}/copy_to_app?app_id=${target_appid}`
    console.log(url)
    const os_options = {
        method: 'POST',
        headers: {
            accept: 'application/json',
            'content-type': 'application/json',
            Authorization: `Basic ${process.env.ONESIGNAL_AUTH_KEY}`
        }
    };
    console.log(os_options)
    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));
}

