export class Storage {

    static saveLocalModel(model: any, modelName: string = 'model') {
        if (typeof window !== 'undefined' && window.localStorage) {
            // window.localStorage.setItem(modelName , btoa(JSON.stringify(model)));
            window.localStorage.setItem(modelName , JSON.stringify(model));
            window.localStorage.setItem(modelName + '-changed', String(Math.round(+new Date() / 1000)));
        } else {
            console.error('localStorage not available: not running in a browser');
        }
    };

    static restoreLocalModel(modelName: string = 'model') {
        if (typeof window !== 'undefined' && window.localStorage) {
            if (localStorage.getItem(modelName)) {
                // return JSON.parse(atob(localStorage.getItem(modelName)));
                return JSON.parse(localStorage.getItem(modelName));
            }
        } else {
            console.error('localStorage not available: not running in a browser');
        }
    };

    static flushLocalModel(modelName: string = 'model') {
        if (typeof window !== 'undefined' && window.localStorage) {
            if (localStorage.getItem(modelName)) {
                localStorage.removeItem(modelName);
            }
            if (localStorage.getItem(modelName + '-changed')) {
                localStorage.removeItem(modelName + '-changed');
            }
        } else {
            console.error('localStorage not available: not running in a browser');
        }
    };

}

