import { useState, useCallback } from 'react';

const tryParseJson = (value)=>{
    try {
        if (value) {
            return JSON.parse(value);
        }
    } catch  {
    // no-op; assume data was malformed
    }
    return undefined;
};
/**
 * Low level wrapper around browser Storage (localStorage/sessionStorage). Do not use this hook directly, use
 * one of the useLocalStorage/useSessionStorage hook from inside ui and adminPortal instead.
 */ const useInternalStorage = (storage, publicToken, key, defaultValue)=>{
    const [cachedValue, setCachedValue] = useState(()=>{
        const storedValue = storage.getItem(publicToken, key);
        return storedValue ? tryParseJson(storedValue) : defaultValue;
    });
    const setValue = useCallback((value)=>{
        setCachedValue(value);
        storage.setItem(publicToken, key, JSON.stringify(value));
    }, [
        publicToken,
        key,
        storage
    ]);
    return [
        cachedValue,
        setValue
    ];
};

export { useInternalStorage };
//# sourceMappingURL=useInternalStorage.mjs.map
