import applicationConfig from '@/constants/application-config';
import { getISSCookie } from '@/helpers/cookie-helper.js';

/*
    Method to determine if our analytics session has timed out or not.
    Amount used for timeout is configurable in application-config.js
*/
export function isAnalyticsSessionStillActive() {
    if (getISSCookie() !== null) {
        const lastTouchedValue = getISSCookie().LastTouched;
        const timeoutAmount = applicationConfig.ANALYTICS_SESSION_TIMEOUT_MINUTES;

        const isMoreThanHalfHourAgo = (new Date() - new Date(lastTouchedValue)) / 60000 > timeoutAmount;

        if (isMoreThanHalfHourAgo) {
            return false;
        }

        return true;
    }
    return false;
}

/*
    Method to determine if the users 'saved' session is still active.
    When user state is created, there is a date that is saved into state
    this method checks against that date.

    Note: That time for saved session timeout is configurable in application-config.js
*/
export function isSavedSessionStillActive() {
    if (getISSCookie() !== null) {
        const savedSessionTimeStamp = new Date(getISSCookie().SavedSessionTimeoutDate);
        const isSavedSessionTimedOut = new Date(new Date().toUTCString()) > savedSessionTimeStamp;

        return !isSavedSessionTimedOut;
    }
    return false;
}

/*
Function to calculate the date for the saved session timeout.
*/

export function getDateForSavedSessionTimeout() {
    const currentDate = new Date(new Date().toUTCString());
    currentDate.setDate(currentDate.getDate() + applicationConfig.SAVED_SESSION_TIMEOUT_DAYS);
    return currentDate.toUTCString();
}
