import { adapters } from "../adapters/index.ts";
import type { SavingsLedger } from "./SavingsLedger.ts";

export class SavingsPoller {
  private timer: ReturnType<typeof setInterval> | null = null;
  constructor(private readonly getLedger: () => SavingsLedger, private readonly intervalMs = 30_000) {}
  start(): void {
    if (this.timer) return;
    this.timer = setInterval(() => this.tick(), this.intervalMs);
    this.timer.unref();
  }
  private tick(): void {
    try { const ledger = this.getLedger(); for (const a of adapters) ledger.pollAdapter(a); } catch {}
  }
  stop(): void {
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
      this.tick();
    }
  }
}
