import fs from 'node:fs';
import path from 'node:path';

/**
 * The page half of core `createSchemaContext`'s input, from Astro: site URL (else `fallbackSiteUrl`), base path,
 * page path, and `fileExists` on `public/`.
 */
export function astroSchemaInput(astro: { site?: URL; url: URL }, fallbackSiteUrl: string, inLanguage?: string) {
  return {
    siteUrl: astro.site?.href || fallbackSiteUrl,
    basePath: import.meta.env.BASE_URL.replace(/\/$/, ''),
    pagePath: astro.url.pathname,
    fileExists: (p: string) => fs.existsSync(path.join(process.cwd(), 'public', p)),
    ...(inLanguage ? { inLanguage } : {}),
  };
}
