/**
 * Created by liangwensen on 3/14/16.
 */

import checkType from'zero-lang/type';
import Store from'./base';

let webStorage;
try {
    webStorage = window.localStorage;
} catch (e) {
    try {
        webStorage = window.sessionStorage;
    } catch (e) {
        webStorage = {};
    }
}

export default new Store({
    storage: webStorage,
    get(name, defaultValue) {
        let value = this.storage[name];
        if (checkType.isString(value)) {
            return JSON.parse(value);
        }

        return defaultValue;
    },

    set(name, value) {
        this.storage[name] = JSON.stringify(value);
    },

    del(name) {
        delete this.storage.removeItem(name);
    },
});
