/*
** 获取storage
*/

const _getStorage = (type, key) => {
    return window[type+'Storage'].getItem(key);
};

/*
** 存入storage
*/
const _setStorage = (type, key, value) => {
    window[type+'Storage'].setItem(key, value);
};

/*
** 根据key删除storage
*/
const _removeStorage = (type, key) => {
    window[type+'Storage'].removeItem(key);
};

/*
** 清空storage
*/
const _clearStorage = (type) => {
    window[type+'Storage'].clear();
};


const setSession = (key, value) => {
    _setStorage('session', key, value);
};
const setSesionJSON = (key, value) => {
    const jsonStr = JSON.stringify(value);
    _setStorage('session', key, jsonStr);
};
const setLocal = (key, value) => {
    _setStorage('local', key, value);
};
const setLocalJSON = (key, value) => {
    const jsonStr = JSON.stringify(value);
    let suffix = window.projectSuffix || "";
    key += suffix;
    _setStorage('local', key, jsonStr);
};
const getSession = key => {
    return _getStorage('session', key);
};
const getSessionJSON = key => {
    const value = _getStorage('session', key);
    return JSON.parse(value);
};
const getLocal = key => {
    return _getStorage('local', key);
};
const getLocalJSON = key => {
    let suffix = window.projectSuffix || "";
    key += suffix;
    const value = _getStorage('local', key);
    return JSON.parse(value);
};
const removeSession = key => {
    _removeStorage('session', key);
};
const removeLocal = key => {
    let suffix = window.projectSuffix || "";
    key += suffix;
    _removeStorage('local', key);
};
const clearSession = key => {
    _clearStorage('session');
};
const clearLocal = key => {
    _clearStorage('local');
};

export {
    setSession,
    setSesionJSON,
    setLocal,
    setLocalJSON,
    getSession,
    getSessionJSON,
    getLocal,
    getLocalJSON,
    removeSession,
    removeLocal,
    clearSession,
    clearLocal
}

export default {
    setSession,
    setSesionJSON,
    setLocal,
    setLocalJSON,
    getSession,
    getSessionJSON,
    getLocal,
    getLocalJSON,
    removeSession,
    removeLocal,
    clearSession,
    clearLocal
}
