import type {
  ExtractorManifest,
  ExtractorRunResult,
} from "@shared/types/extractors";
import { runWellfound } from "./src/run.js";

export const manifest: ExtractorManifest = {
  id: "wellfound",
  displayName: "Wellfound",
  providesSources: ["wellfound"],
  async run(context): Promise<ExtractorRunResult> {
    if (context.shouldCancel?.()) return { success: true, jobs: [] };

    const parsedMax = context.settings.wellfoundMaxJobsPerTerm
      ? Number.parseInt(context.settings.wellfoundMaxJobsPerTerm, 10)
      : Number.NaN;
    const maxJobs = Number.isFinite(parsedMax) ? Math.max(1, parsedMax) : 50;

    context.onProgress?.({
      phase: "list",
      termsProcessed: 0,
      termsTotal: 1,
      currentUrl: "https://wellfound.com/jobs",
      detail: "Wellfound: launching browser scrape",
    });

    const result = await runWellfound({
      searchTerms: context.searchTerms,
      maxJobs,
    });

    if (!result.success) {
      return { success: false, jobs: [], error: result.error };
    }

    context.onProgress?.({
      phase: "list",
      termsProcessed: 1,
      termsTotal: 1,
      currentUrl: "https://wellfound.com/jobs",
      jobPagesProcessed: result.jobs.length,
      detail: `Wellfound: ${result.jobs.length} jobs`,
    });

    return { success: true, jobs: result.jobs };
  },
};

export default manifest;
