import { GadgetComponent } from "./component.ts";
import { GadgetLogLevel } from "./log.ts";
import { GadgetLogTransport } from "./log-transport.ts";

export type LogEmitFunction = (
  event: string,
  timestamp: Date,
  component: GadgetComponent,
  level: GadgetLogLevel,
  message: string,
  metadata?: unknown,
) => void;

export class GadgetLogTransportSocket implements GadgetLogTransport {
  private emit: LogEmitFunction;

  constructor(emit: LogEmitFunction) {
    this.emit = emit;
  }

  async writeLog(
    timestamp: Date,
    component: GadgetComponent,
    level: GadgetLogLevel,
    message: string,
    metadata?: unknown,
  ): Promise<void> {
    this.emit("log", timestamp, component, level, message, metadata);
  }
}
