// JavaScript Document
// V1.0.0 
exports.onesignal_makeapp = async function (options) {



   const orgid = this.parseOptional(options.orgid, '*', null)
   console.log(options.orgid + " => " + orgid)

   const chrome_web_default_notification_icon = this.parseOptional(options.chrome_web_default_notification_icon, '*', null)
   console.log(options.chrome_web_default_notification_icon + " => " + chrome_web_default_notification_icon)

   const chrome_web_origin = this.parseOptional(options.chrome_web_origin, '*', process.env.ONESIGNAL_SITE_URL)
   console.log(options.chrome_web_origin + " => " + chrome_web_origin)

   const appname = this.parseRequired(options.app_name, '*', 'You must specify an app name')
   console.log(options.app_name + " => " + appname)

   const safari_site_origin = this.parseOptional(options.safari_site_origin, '*', process.env.ONESIGNAL_SITE_URL)
   console.log(options.safari_site_origin + " => " + safari_site_origin)

   const site_name = this.parseOptional(options.site_name, '*', null)
   console.log(options.site_name + " => " + site_name)

   const safari_apns_p12 = this.parseOptional(options.safari_apns_p12, '*', null)
   console.log(options.safari_apns_p12 + " => " + safari_apns_p12)

   const safari_apns_p12_password = this.parseOptional(options.safari_apns_p12_password, '*', null)
   console.log(options.safari_apns_p12_password + " => " + safari_apns_p12_password)

   const safari_icon_256_256 = this.parseOptional(options.safari_icon_256_256, '*', null)
   console.log(options.safari_icon_256_256 + " => " + safari_icon_256_256)

   const fcm_v1_service_account_json = this.parseOptional(options.fcm_v1_service_account_json, '*', null)
   console.log(options.fcm_v1_service_account_json + " => " + fcm_v1_service_account_json)

   const apns_p12 = this.parseOptional(options.apns_p12, '*', null)
   console.log(options.apns_p12 + " => " + apns_p12)

   const apns_p12_password = this.parseOptional(options.apns_p12_password, '*', null)
   console.log(options.apns_p12_password + " => " + apns_p12_password)

   const apns_env = this.parseOptional(options.apns_env, '*', null)
   console.log(options.apns_env + " => " + apns_env)

   const apns_key_id = this.parseOptional(options.apns_key_id, '*', null)
   console.log(options.apns_key_id + " => " + apns_key_id)

   const apns_team_id = this.parseOptional(options.apns_team_id, '*', null)
   console.log(options.apns_team_id + " => " + apns_team_id)

   const apns_bundle_id = this.parseOptional(options.apns_bundle_id, '*', null)
   console.log(options.apns_bundle_id + " => " + apns_bundle_id)

   const apns_p8 = this.parseOptional(options.apns_p8, '*', null)
   console.log(options.aapns_p8 + " => " + apns_p8)

   const additional_data_is_root_payload = this.parseOptional(options.additional_data_is_root_payload, '*', false)
   console.log(options.additional_data_is_root_payload + " => " + additional_data_is_root_payload)

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

   const url = `https://api.onesignal.com/apps/`
   const os_options = {
      method: 'POST',
      headers: {
         accept: 'text/plain',
         'Content-Type': 'application/json',
         Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}`,
      },
      body: JSON.stringify({
         name: appname,
         organization_id: orgid,
         fcm_v1_service_account_json: fcm_v1_service_account_json,
         chrome_web_origin: chrome_web_origin,
         chrome_web_default_notification_icon: chrome_web_default_notification_icon,
         site_name: safari_site_origin,
         safari_site_origin: safari_site_origin,
         safari_apns_p12: safari_apns_p12,
         safari_apns_p12_password: safari_apns_p12_password,
         safari_icon_256_256: safari_icon_256_256,
         apns_p12: apns_p12,
         apns_p12_password: apns_p12_password,
         apns_env: apns_env,
         apns_key_id: apns_key_id,
         apns_team_id: apns_team_id,
         apns_bundle_id: apns_bundle_id,
         apns_p8: apns_p8,
         additional_data_is_root_payload: false
      })
   };
   console.log('Method: ' + os_options.method)
   console.log('Headers: ' + os_options.headers)
   console.log('Body: ' + os_options.body)
   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));



}
