const PERSISTENT_STORAGE_KEY_PREFIX = 'stytch_sdk_state_';
const getPersistentStorageKey = (publicToken, key)=>{
    return `${PERSISTENT_STORAGE_KEY_PREFIX}${publicToken}${key ? `::${key}` : ''}`;
};
function makeSafeStorage(storage) {
    if (storage == null) {
        return {
            getItem () {
                return null;
            },
            removeItem () {
            // Noop
            },
            setItem () {
            // Noop
            }
        };
    }
    return {
        getItem (publicKey, key) {
            const persistentStorageKey = getPersistentStorageKey(publicKey, key);
            try {
                return storage.getItem(persistentStorageKey);
            } catch  {
                // Swallow error
                return null;
            }
        },
        setItem (publicKey, key, value) {
            const persistentStorageKey = getPersistentStorageKey(publicKey, key);
            try {
                if (value) storage.setItem(persistentStorageKey, value);
            } catch  {
            // Swallow error
            }
        },
        removeItem (publicKey, key) {
            const persistentStorageKey = getPersistentStorageKey(publicKey, key);
            try {
                storage.removeItem(persistentStorageKey);
            } catch  {
            // Swallow error
            }
        }
    };
}
const safeLocalStorage = makeSafeStorage(globalThis.localStorage);
const safeSessionStorage = makeSafeStorage(globalThis.sessionStorage);
function getKeyBoundStorage(storage, publicToken) {
    return {
        getItem (key) {
            return storage.getItem(publicToken, key);
        },
        setItem (key, value) {
            storage.setItem(publicToken, key, value);
        },
        removeItem (key) {
            storage.removeItem(publicToken, key);
        }
    };
}

export { getKeyBoundStorage, getPersistentStorageKey, safeLocalStorage, safeSessionStorage };
//# sourceMappingURL=storage.mjs.map
