import { readFile } from "node:fs/promises";

export const catalog = JSON.parse(await readFile(new URL("../generated/catalog.json", import.meta.url), "utf8"));
export const starterKits = catalog.starterKits;
export const developerSearchIndex = catalog.searchIndex;
export const integrationManifests = catalog.integrationManifests;
export const integrationManifestOptions = catalog.integrationManifestOptions;
export const integrationManifestSummary = catalog.integrationManifestSummary;

export function resolveStarter(identity) {
  const candidate = String(identity ?? "").trim().toLowerCase();
  if (!candidate) return undefined;
  return starterKits.find((kit) => [kit.slug, kit.quickstartSlug, kit.platform, `${kit.platform}-starter`].some((value) => value.toLowerCase() === candidate));
}

export function resolveIntegrationManifest({ useCaseSlug, platform, mode }) {
  const normalizedPlatform = String(platform ?? "").trim().toLowerCase();
  return integrationManifests.find((manifest) => manifest.selection.useCaseSlug === useCaseSlug && manifest.selection.primaryPlatform.toLowerCase() === normalizedPlatform && manifest.selection.operatingMode === mode);
}
