/* eslint-disable class-methods-use-this */
class IframeBridge {
  constructor() {
    console.info('use iframeBridge');
    this.mountEventBus();
  }

  run(data, callbackName) {
    return new Promise(resolve => {
      if (!data.event) {
        resolve(false);
        return;
      }
      console.log('childIframe', data);
      window.parent.postMessage(data, '*');
      !callbackName && resolve(true);
    });
  }

  mountEventBus() {
    window.addEventListener('message', info => {
      // 来源页面地址
      console.log('addEventListenerCallBack', info);
    });
  }

  // async closeBrowser() {
  //   return await this.run({ event: 'closeBrowser' });
  // }

  // async getToken() {
  //   return await this.run({ event: 'getToken' }, 'getTokenSuccess');
  // }

  // async openNewUrl(data: RunData = {}) {
  //   if (!data.event) {
  //     data.event = 'openNewUrl';
  //   }
  //   return await this.run(data);
  // }
}

export default IframeBridge;
