import { BridgePlugin } from '../bridge';
import { get } from 'para-utils';

interface IGetWebviewCacheParam {
  // 客户端统一的项目标志
  // projectTag: 'loan-mark'
  projectTag: string;
  // 需要获取缓存数据的主键列表
  // keys: ['isShowRedPackageTips', 'true']
  keys: string[];
}

type cachesType = {
  [index: string]: string;
}

interface IGetWebviewCacheResult {
  // 状态 0：不支持 | 1：成功
  status: '0' | '1';
  // 返回的缓存数据，key为传入的查下主键，value为取到的内容
  caches: cachesType;
}

export class GetWebviewCache<K> extends BridgePlugin<IGetWebviewCacheParam, K>{
  protected get ntvPluginName() {
    return "_ntv_util_scan_identity_card";
  }

  protected get availableMinVersion() {
    return "1.0.1";
  }

  protected bridgeExecChrome(pluginName: string | undefined): void {
    // 可针对不同运行环境实现特定的调用逻辑来MOCK，弥补平台差异性.
    throw new Error('GetWebviewCache() 未针对当前环境实现');
  }
}

/**
 * 获取客户端缓存信息
 */
export const getWebviewCache = (param: IGetWebviewCacheParam) => {
  return new GetWebviewCache<IGetWebviewCacheResult>(param).invokeOnce().then(result => {
    const status = get(result.cbData, 'status', '0');
    const caches = get(result.cbData, 'caches', {});
    return (status === '1' ? caches : {}) as cachesType;
  });
}