import { FakeJamClient } from './fakeJamClient.js';

export class FakeJamClientProxy {
  constructor(app, p2pMessageFactory) {
    this.app = app;
    this.p2pMessageFactory = p2pMessageFactory;

    this.handler = {
      get: (target, prop, receiver) => {
         return function (...args) {
          return new Promise((resolve, reject) => {
            try {
              
              if(target[prop]){
                const result = target[prop].apply(target, args);
                resolve(result);
              }else{
                console.error('[FakeJamClientProxy] No such method: ', prop);
                reject(`No such method in FakeJamClient: ${prop}`);
              }
              
            } catch (error) {
              console.error('[FakeJamClientProxy] error:', prop, error);
              reject(error);
            }
          });
         }
      }
    }
  }
  

  init() {
    const jamClient = new FakeJamClient(this.app, this.p2pMessageFactory);
    return new Proxy(jamClient, this.handler);
  }
}
