import type { StoreAdapter } from "./types.js";
import { partsale } from "../adapters/partsale.js";
import { xparts } from "../adapters/xparts.js";
import { eoltas } from "../adapters/eoltas.js";
import { carparts } from "../adapters/carparts.js";
import { handler } from "../adapters/handler.js";
import { trodo } from "../adapters/trodo.js";
import { rd24 } from "../adapters/rd24.js";
import { euautodalas } from "../adapters/euautodalas.js";
import { autodoc } from "../adapters/autodoc.js";
import { ic24 } from "../adapters/ic24.js";
import { refreshExactProduct } from "./directRefresh.js";

// Two tiers (see WALLED-STORES.md):
// - auto: partsale, xparts, eoltas, carparts, handler — plain HTTP, fetched in parallel
//   on every search.
// - onDemand (routed through the resident headful browser): trodo (JSON API via
//   BrowserHub.fetchJson), rd24 + euautodalas + autodoc (server-rendered HTML via
//   BrowserHub.fetchPageHtml) are Cloudflare-walled — a warm residential session usually
//   passes the managed challenge non-interactively, noVNC solve is the cold fallback. ic24
//   (BrowserHub.fetchOriginHtml) is NOT Cloudflare-walled but only serves prices to a warm
//   session, so it too routes through the browser — it just never asks for a solve.
export const adapters: StoreAdapter[] = [
  partsale,
  xparts,
  eoltas,
  carparts,
  handler,
  trodo,
  rd24,
  euautodalas,
  autodoc,
  ic24,
];

// Every enabled store has a confirmed exact-product path. Store-specific parsing
// remains scoped by adapter id inside directRefresh until it needs its own module.
for (const adapter of adapters) {
  adapter.refreshOffer = (target, ctx) => refreshExactProduct(adapter, target, ctx);
}

export function getAdapter(id: string): StoreAdapter | undefined {
  return adapters.find((a) => a.id === id);
}

export function enabledAdapters(): StoreAdapter[] {
  return adapters.filter((a) => a.enabled);
}
