type LogLevel = 'INFO' | 'WARN' | 'ERROR';
type LogSink = (entry: { level: LogLevel; message: string; timestamp: number }) => void;

let sink: LogSink | null = null;

export const logger = {
  info: (msg: string) => {
    const entry = { level: 'INFO' as LogLevel, message: msg, timestamp: Date.now() };
    if (sink) sink(entry);
    console.log(`[INFO] ${msg}`);
  },
  warn: (msg: string) => {
    const entry = { level: 'WARN' as LogLevel, message: msg, timestamp: Date.now() };
    if (sink) sink(entry);
    console.warn(`[WARN] ${msg}`);
  },
  error: (msg: string, err?: unknown) => {
    const entry = { level: 'ERROR' as LogLevel, message: msg, timestamp: Date.now() };
    if (sink) sink(entry);
    console.error(`[ERROR] ${msg}`, err);
  },
  setSink: (fn: LogSink | null) => {
    sink = fn;
  }
};
