/**
 * @format
 */
import React from 'react';
import { AppRegistry, Platform } from 'react-native';
import App from './App';
import { name as appName } from './app.json';
import codePush from 'react-native-code-push';
import moment from 'moment';
import messaging from '@react-native-firebase/messaging';
import AsyncStorage from '@react-native-community/async-storage';
var CheckisHeadless = true
messaging().setBackgroundMessageHandler(async remoteMessage => {
    var notificationRecord = {
        notifications: [],
        unread: 0,
    }
    const data = {
        id: remoteMessage.messageId,
        date: moment().valueOf(),
        title: remoteMessage.notification.title,
        message: remoteMessage.notification.body,
        read: false,
    }
    console.log(data)
    try {
        const jsonValue = await AsyncStorage.getItem('notification')
        if (jsonValue != null) {
            notificationRecord = JSON.parse(jsonValue)
        }
        console.log(notificationRecord)

    } catch (e) {
        console.log(e)
    }

    await notificationRecord.notifications.push(data);
    notificationRecord.unread += 1;
    console.log(notificationRecord)
    try {
        const saveItem = JSON.stringify(notificationRecord)
        await AsyncStorage.setItem('notification', saveItem)
    } catch (e) {
        console.log(e)
    }

});



function HeadlessCheck({ isHeadless }) {
    console.log(isHeadless)
    if (isHeadless) {
        // App has been launched in the background by iOS, ignore
        return null;
    }

    return <App />
}
// HeadlessCheck()=> {
//     return codePush(App);
// }
function RealApp() {
    return codePush(App);
}

AppRegistry.registerComponent(appName, () => RealApp());
