/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
import jwt_decode from 'jwt-decode';
import { getCookie } from '../../utils/index.js';
import { ANONYMOUS_USER, ANONYMOUS_USER_TOKEN } from '../../models/index.js';
/**
 * Check if browser localStorage is available and functional.
 * Result is memoized to avoid repeated set/remove probes.
 */
let _localStorageAvailable;
function isLocalStorageAvailable() {
    if (_localStorageAvailable !== undefined) {
        return _localStorageAvailable;
    }
    try {
        if (typeof window === 'undefined' || !window.localStorage) {
            _localStorageAvailable = false;
            return false;
        }
        const testKey = '__datalayer_ls_test__';
        window.localStorage.setItem(testKey, 'test');
        window.localStorage.removeItem(testKey);
        _localStorageAvailable = true;
        return true;
    }
    catch {
        _localStorageAvailable = false;
        return false;
    }
}
export const JWT_DATALAYER_ISSUER = 'https://id.datalayer.run';
export const DATALAYER_IAM_USER_KEY = '@datalayer/iam:user';
export const DATALAYER_IAM_TOKEN_KEY = '@datalayer/iam:token';
export const JWT_REFRESH_TOKEN_COOKIE_NAME = 'refresh_token';
/**
 * Return the user from the local storage.
 */
export const getStoredUser = () => {
    if (!isLocalStorageAvailable()) {
        return ANONYMOUS_USER;
    }
    const user = window.localStorage.getItem(DATALAYER_IAM_USER_KEY);
    if (user) {
        try {
            const u = JSON.parse(user);
            u.joinDate = new Date(u.joinDate);
            return u;
        }
        catch (e) {
            return ANONYMOUS_USER;
        }
    }
    return ANONYMOUS_USER;
};
/**
 * Set the user in the local storage.
 */
export const storeUser = (user) => {
    if (!isLocalStorageAvailable()) {
        return;
    }
    if (user) {
        window.localStorage.setItem(DATALAYER_IAM_USER_KEY, JSON.stringify(user));
    }
    else {
        window.localStorage.removeItem(DATALAYER_IAM_USER_KEY);
    }
};
/**
 * Return the IAM token from the local storage.
 */
export const getStoredToken = () => {
    if (!isLocalStorageAvailable()) {
        return ANONYMOUS_USER_TOKEN;
    }
    const token = window.localStorage.getItem(DATALAYER_IAM_TOKEN_KEY);
    if (token) {
        return token;
    }
    return ANONYMOUS_USER_TOKEN;
};
/**
 * Set the IAM token in the local storage.
 */
export const storeToken = (token) => {
    if (!isLocalStorageAvailable()) {
        return;
    }
    if (token) {
        if (token !== ANONYMOUS_USER_TOKEN) {
            try {
                const jwt = jwt_decode(token);
                console.debug('JWT Token', jwt);
            }
            catch (error) {
                console.error('Error while decoding JWT Token.', error);
                throw error;
            }
        }
        window.localStorage.setItem(DATALAYER_IAM_TOKEN_KEY, token);
    }
    else {
        window.localStorage.removeItem(DATALAYER_IAM_TOKEN_KEY);
    }
};
/**
 * Load a JWT refresh token stored in a cookie.
 */
export function loadRefreshTokenFromCookie() {
    return getCookie(JWT_REFRESH_TOKEN_COOKIE_NAME);
}
