import { BridgePlugin } from '../bridge';
import { getPlatform, Platform } from 'para-utils';

interface IAppInfoItem {
  name: string;
}

interface IReadInstalledAppInfoResult {
  packageList: IAppInfoItem[];
}

export class ReadInstalledAppInfo<K> extends BridgePlugin<void, K>{
  protected get ntvMethodName() {
    return '_ntv_util_read_installed_app_info';
  }
  protected get availableMinVersion() {
    return '1.0.0';
  }

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

/**
 * 读取当前手机已安装的App信息 (仅限Android)
 */
export const readInstalledAppInfo = () => {
  // FOR IOS.
  if (getPlatform() === Platform.FacIosWebview) {
    return Promise.resolve({
      packageList: [],
    });
  }
  return new ReadInstalledAppInfo<IReadInstalledAppInfoResult>().invokeOnce().then(result => {
    return result.cbData;
  });
}
