function catchError(e: any) {
    console.error(e)
}

function get(key: string) {
    let value: string | null = null

    try {
        // #ifdef APP-PLUS
        value = plus.storage.getItem(key)
        // #endif

        // #ifndef APP-PLUS
        value = uni.getStorageSync(key)
        // #endif
    } catch (e) {
        catchError(e)
    }

    return value
}

function set(key: string, value: string) {
    try {
        // #ifdef APP-PLUS
        plus.storage.setItem(key, value)
        // #endif

        // #ifndef APP-PLUS
        uni.setStorageSync(key, value)
        // #endif
    } catch (e) {
        catchError(e)
    }
}

function remove(key: string) {
    try {
        // #ifdef APP-PLUS
        plus.storage.removeItem(key)
        // #endif

        // #ifndef APP-PLUS
        uni.removeStorageSync(key)
        // #endif
    } catch (e) {
        catchError(e)
    }
}

function clear() {
    try {
        // #ifdef APP-PLUS
        plus.storage.clear()
        // #endif

        // #ifndef APP-PLUS
        uni.clearStorageSync()
        // #endif
    } catch (e) {
        catchError(e)
    }
}

function keys() {
    let keys = []

    try {
        // #ifdef APP-PLUS
        keys = plus.storage.getAllKeys()
        // #endif

        // #ifndef APP-PLUS
        keys = uni.getStorageInfoSync().keys
        // #endif
    } catch (e) {
        catchError(e)
    }

    return keys
}

/**
 * 兼容 uni-app 和 plus 的持久化 Storage
 */
export const Storage = {
    get,
    set,
    remove,
    clear,
    keys,
}
