import '../../../chipper/js/browser/initialize-globals.js';
import '../../../globals.js';
import joist from '../joist.js';
let preferencesStorage = null;
const PREFERENCES_KEY = 'PREFERENCES:';
class PreferencesStorage {
    constructor() {
        this.enabled = true;
        // for debugging
        this.registedProperties = [];
        try {
            // Always store the line indices just in case they want to be used by the next run.
            window.localStorage.setItem('test', 'test');
        }
        catch (e) {
            this.enabled = false; // can't use localStorage with browser settings
            if (e instanceof Error) {
                const safari = window.navigator.userAgent.includes('Safari') && !window.navigator.userAgent.includes('Chrome');
                if (safari && e.message.includes('QuotaExceededError')) {
                    console.log('It looks like you are browsing with private mode in Safari. ' +
                        'Please turn that setting off if you want to use PreferencesStorage');
                }
                else {
                    throw e;
                }
            }
        }
    }
    registerToLocalStorage(property, name) {
        const key = `${PREFERENCES_KEY}${name}`;
        if (window.localStorage.getItem(key)) {
            property.value = JSON.parse(window.localStorage.getItem(key));
        }
        property.link((value) => {
            window.localStorage.setItem(key, JSON.stringify(value));
        });
        this.registedProperties.push(property);
    }
    static register(property, name) {
        if (!phet.chipper.queryParameters.preferencesStorage) {
            return property;
        }
        if (!preferencesStorage) {
            preferencesStorage = new PreferencesStorage();
        }
        if (preferencesStorage.enabled) {
            preferencesStorage.registerToLocalStorage(property, name);
        }
        return property;
    }
}
joist.register('PreferencesStorage', PreferencesStorage);
export default PreferencesStorage;
//# sourceMappingURL=PreferencesStorage.js.map