import { ILeadSource } from "./types";
import { MockSource } from "./mock";
import { GooglePlacesSource } from "./google-places";
import { WebsiteContactSource } from "./website-contact";
import { OverpassSource } from "./overpass";
import { FoursquareSource } from "./foursquare";
import { ApifySource } from "./apify";

class SourceRegistry {
  private sources: Map<string, ILeadSource> = new Map();

  constructor() {
    // Register initial sources
    this.register(new OverpassSource());
    this.register(new GooglePlacesSource());
    this.register(new FoursquareSource());
    this.register(new ApifySource());
    this.register(new WebsiteContactSource());
    this.register(new MockSource());
  }

  register(source: ILeadSource) {
    this.sources.set(source.id, source);
  }

  getSource(id: string): ILeadSource | undefined {
    return this.sources.get(id);
  }

  getAllSources(): ILeadSource[] {
    return Array.from(this.sources.values());
  }

  async getAvailableSources(): Promise<ILeadSource[]> {
    const all = this.getAllSources();
    const availability = await Promise.all(all.map(async s => ({
      source: s,
      available: await s.isAvailable()
    })));
    return availability.filter(a => a.available).map(a => a.source);
  }
}

export const sourceRegistry = new SourceRegistry();
