import { promisify } from '../../../../utils';

// eslint-disable-next-line import/prefer-default-export
export async function loadPlugin({ index, componentData }) {
  try {
    if (my.loadPlugin) {
      const { componentExtInfo: { version, pluginId } } = componentData;
      const _pluginId = version ? `${pluginId}@${version}` : `${pluginId}@*`;
      const [, { load }] = await promisify(my.loadPlugin)({ pluginId });
      console.log('loadPlugin =====', _pluginId, load);
      return {
        index,
        isReady: load === 'success',
      };
    }
    return {
      index,
      isReady: false,
    };
  } catch (e) {
    return {
      index,
      isReady: false,
    };
  }
}
