
import messager from '../common/messager';
import prepareHUYA from '../common/prepareHUYA';
import utils from '../common/utils';

// 存储相关
export default {
  async getItem(key) {
    const { HUYA } = await prepareHUYA();
    const req = new HUYA.GetStorageKeyReq();
    req.dataKey = key;
    const { dataValue } = await messager.dispatchTaf('getStorageKey', req, HUYA.GetStorageKeyResp, 'extCapabilityUI') || {};
    return utils.unescapeHTML(dataValue);
  },

  async setItem(key, value) {
    const { HUYA } = await prepareHUYA();
    const req = new HUYA.SetStorageReq();
    req.dataKey = key;
    req.dataValue = value;
    return await messager.dispatchTaf('setStorage', req, HUYA.SetStorageResp, 'extCapabilityUI');
  },

  async getKeys() {
    const { HUYA } = await prepareHUYA();
    const req = new HUYA.GetStorageAllKeysReq();
    const { dataKeys } = await messager.dispatchTaf('getStorageAllKeys', req, HUYA.GetStorageAllKeysResp, 'extCapabilityUI') || {};
    const { value } = dataKeys || {};
    return value;
  },

  async removeItem(key) {
    const { HUYA } = await prepareHUYA();
    const req = new HUYA.DelStorageKeyReq();
    req.dataKey = key;
    return await messager.dispatchTaf('delStorageKey', req, HUYA.DelStorageKeyResp, 'extCapabilityUI');
  },
}
