// Node integration with wappler.io
// V1.0.0
exports.onesignal_getnotifications = async function (options) {

   // get appid parsed as options.appid, throw error is missing
   const appid = this.parseRequired(options.appid, '*', 'No app ID entered')
   // log before and after vlaues
   console.log(options.appid + " => " + appid)
   // get limit, if omitted set default of 50
   var limit = this.parseOptional(options.limit, '*', 50)
   // ensure it is positive
   limit = Math.abs(limit)
   // ensure not exceeeding 50
   limit = Math.min(limit, 50)
   // log before and after values
   console.log(options.limit + " => " + limit)
   // get offset, if omitted set default of 0
   const offset = this.parseOptional(options.offset, '*', 0)
   // log before and after values
   console.log(options.offset + " => " + offset)
   // get offset, if omitted set default of null
   const kind = this.parseOptional(options.kind, '*', null)
   console.log(options.kind + " => " + kind)
   // log before and after values
   const fetch = require('node-fetch');
   // set url
   const url = `https://api.onesignal.com/notifications?app_id=${appid}&limit=${limit}&offset=${offset}&kind=${kind}`;
   const os_options = {
      method: 'GET',
      headers: {
         accept: 'application/json',
         Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}`
      }
   };
   //log url to console
   console.log(os_options.headers)
   console.log(url)
   // log timestamp
   console.log("Timestamp: " + new Date().getTime());
   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));
}