/* eslint-disable no-undef */
// @ts-nocheck
import EventHub from '../../utils/eventEmitter';
import qs from 'qs';
EventHub.addEventListener('onNavigateTo', payload => {
  if (payload) {
    my.navigateTo({
      url: payload
    });
  }
});
Component({
  data: {
    isReady: false,
    pluginAppId: '',
    pluginPageUrl: '',
    pluginVersion: '',
    tabbarInfo: {}
  },

  didMount() {
    const {
      componentData: {
        componentExtInfo,
        tabbarInfo
      }
    } = this.props;
    const {
      pluginAppId,
      pluginPageUrl,
      pluginVersion
    } = componentExtInfo;
    this.setData({
      pluginAppId,
      pluginPageUrl,
      pluginVersion,
      tabbarInfo
    });
    this.loadPlugin();
  },

  methods: {
    loadPlugin() {
      const {
        pluginAppId,
        pluginPageUrl,
        pluginVersion,
        tabbarInfo
      } = this.data;
      let {
        query
      } = my.getLaunchOptionsSync() || {};
      query = { ...query,
        tabbarInfo
      };
      const that = this;
      my.loadPlugin({
        plugin: `${pluginAppId}@${pluginVersion}`,
        success: () => {
          that.setData({
            isReady: true
          });
          const plugin = requirePlugin(`dynamic-plugin://${pluginAppId}`); // eslint-disable-next-line no-mixed-operators

          const fmcg = query && qs.stringify(query) || ''; // 注入发布订阅实例

          plugin.setEventHub(EventHub);
          my.reLaunch({
            url: `dynamic-plugin://${pluginAppId}${pluginPageUrl}${fmcg ? `?${fmcg}` : ''}`
          });
        }
      });
    }

  }
});