import Pusher from 'pusher-js';
import * as PusherPushNotifications from "@pusher/push-notifications-web";

// export const beamsClient = new PusherPushNotifications.Client({
//     instanceId: '38817d22-5493-468d-939e-b1c39c1c43a3',
// });


/**
 * Initializarion of the instances for beams an pusher
 *
 */
const {
    REACT_APP_BEAMS_INSTANCE: BEAMS_INSTANCE ,
    REACT_APP_PUSHER_KEY: PUSHER_KEY,
} = process.env



let beamsClient:PusherPushNotifications.Client|null = null;


try{
        if(!BEAMS_INSTANCE) { console.warn('missing beams instance id')}
        beamsClient = new PusherPushNotifications.Client({
            instanceId: BEAMS_INSTANCE as string,
        });
}
catch (e){
    beamsClient = null
}

// function stopClient(){
//     if(!beamsClient){
//         return
//     }
//     beamsClient.stop()
// }

// function subscribeToBeams(userId:number)


export {beamsClient};

let pusher:Pusher|null = null;

if(PUSHER_KEY) {
    pusher = new Pusher(PUSHER_KEY as string, {
        cluster: 'eu',
        // @ts-ignore
        encrypted: true,
        forceTLS: false,
    });
}
else{
    console.warn('missing pusher key')
}
export {pusher};



export const unsubscribeFromAllChannels = () => {
    if(!pusher) return
    pusher.allChannels().forEach(ch=>{
        ch.unsubscribe()
    })
}

