/* eslint-disable no-undef */

export const PLUGIN_MEMBER_CARD = '2021001107697072';

const PLUGIN_LIST = [
  {
    pluginId: PLUGIN_MEMBER_CARD,
    version: '*',
    name: '支付宝卡包插件',
    url: 'https://app.alipay.com/commodity/v2/ant/merchandise/merchandiseDetail.htm?merchandiseId=AM010401000000053393',
  },
];


export async function dynamicLoad(pluginId, version = '*') {
  const info = PLUGIN_LIST.find(x => x.pluginId == pluginId) || { pluginId, version, name: `${pluginId}@${version}` };

  const plugin = `${info.pluginId}@${info.version}`;

  return new Promise((resolve, reject) => {
    my.loadPlugin({
      plugin,
      success: (data) => {
        const p = requirePlugin(`dynamic-plugin://${info.pluginId}`);
        console.log('>>>> loadPlugin success, ', info, data);
        resolve(p);
      },
      fail: (e) => {
        console.log('>>>> loadPlugin fail, ', e);
        my.alert({
          title: '插件加载失败',
          content: `插件名称：${info.name} \n\n插件id: ${plugin} \n\n订购地址: ${info.url}\n\n异常:${JSON.stringify(e)}`,
        });
        reject(e);
      },
    });
  });
}
