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

    let limit = this.parseOptional(options.limit, '*', '300')
    console.log(options.limit + " => " + limit)

    let offset = this.parseOptional(options.offset, '*', '0')
    console.log(options.offset + " => " + offset)

    let url = `https://api.onesignal.com/apps/${appid}/segments?offset=${offset}&limit=${limit}`;

    // &channel=${channel}
    console.log(url)
    const os_options = {
        method: 'GET',
        headers: {
            accept: 'application/json',
            Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}`,
            'Content-Type': 'application/json; charset=utf-8'
        }
    };

    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));

}