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

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

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

    context.onProgress?.({
      phase: "list",
      termsProcessed: 0,
      termsTotal: context.searchTerms.length || 1,
      currentUrl: "https://www.google.com/search?udm=8",
      detail: "Google Jobs: launching browser scrape",
    });

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

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

    context.onProgress?.({
      phase: "list",
      termsProcessed: context.searchTerms.length || 1,
      termsTotal: context.searchTerms.length || 1,
      currentUrl: "https://www.google.com/search?udm=8",
      jobPagesProcessed: result.jobs.length,
      detail: `Google Jobs: ${result.jobs.length} jobs`,
    });

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

export default manifest;
