import { Manager } from "socket.io-client";
import modes from "../modes.js";
import events from "../events.js";

export { default as events } from "../events.js";
export { default as modes } from "../modes.js";

export default class Viewer {
  constructor(url, options = {}) {
    this.url = url;
    this.mode = modes.VIEW;
    this.onLog = options.onLog ?? console.log;
    this.onClose = options.onClose ?? (() => {});
  }

  viewJob(jobId, onLog, onClose) {
    const mgr = new Manager(this.url, {
      query: { mode: this.mode, jobId },
    });
    onLog = onLog ?? this.onLog.bind(this);
    onClose = onClose ?? this.onClose.bind(this);
    const sk = mgr.socket("/");
    sk.on(events.JOB_LOG, onLog);
    sk.on(events.JOB_CLS, onClose);
    return sk;
  }
}
