
// import { StoreEvent } from 'state-manager-utility';
// import { GetCookie } from './cookie.utils';
// import GAService from './googleAnalytic.service';

import { message } from 'antd';


var deferredPrompt;
let promptAllowed = false;

function ListenA2HSPrompt() {
    window.addEventListener('beforeinstallprompt', function (e) {
        // Prevent Chrome 67 and earlier from automatically showing the prompt
        e.preventDefault();
        // Stash the event so it can be triggered later.
        deferredPrompt = e;
        // track how many users were shown the a2hs popup
        // GAService.trackNumberOfTimesA2HSPopupWasShown(true)


        if (!promptAllowed) {
            promptAllowed = true;
            // StoreEvent({ eventName: 'canShowA2HSBanner', data: promptAllowed });
        }

    });
}

ListenA2HSPrompt();

/**
 * Returns boolean value to convey if A2HS can be prompted
 */
export function CanShowPrompt() {
    return true;
    // const dontShowAddAppNotification = GetCookie('dontShowAddAppNotification');
    // if (dontShowAddAppNotification) {
    //     return false;
    // }
    // else {
    //     return promptAllowed;
    // }

}


/* displays native A2HS prompt & stores results */
export function TriggerNativePrompt() {

    if (!deferredPrompt.prompt) {

        message.info('Not installed well');


    } else {

        return deferredPrompt.prompt()
            .then(function (evt) {
                // Wait for the user to respond to the prompt
                return deferredPrompt.userChoice;

            })
            .then(function (choiceResult) {
                if (choiceResult.outcome === "accepted") {
                    promptAllowed = false;
                    // StoreEvent({ eventName: 'canShowA2HSBanner', data: promptAllowed });
                    // alert("User accepted the A2HS prompt, Please pass this info to GA");
                    // @todo can take some action based on user choice
                    // track how many users added PWA to home screen
                    // GAService.trackNumberOfTimesUsersAddedPWAToHomeScreen(true)
                } else {
                    // alert("User dismissed the A2HS prompt, Please pass this info to GA");
                    // @todo can take some action based on user choice
                }
                if (typeof window.ga == 'function') {
                    window.ga('send', 'event', 'A2H', choiceResult.outcome);
                }
            })
            .catch(function (err) {

                alert(err.message);


            });

    }
}
