import type { ModuleRuntimeConfig, ProductionDebugResponse, SitemapDefinition, SitemapSourceResolved } from './types' import { appFetch } from 'nuxtseo-layer-devtools/composables/rpc' import { isProductionMode, productionUrl, refreshTime } from 'nuxtseo-layer-devtools/composables/state' import { ref, watch } from 'vue' export const data = ref<{ nitroOrigin: string globalSources: SitemapSourceResolved[] sitemaps: SitemapDefinition[] runtimeConfig: ModuleRuntimeConfig siteConfig?: { url?: string } } | null>(null) // Production debug data from the remote /__sitemap__/debug.json (requires debug: true in production) export const productionRemoteDebugData = ref(null) export const productionData = ref(null) export const productionLoading = ref(false) export async function refreshSources() { if (!appFetch.value) return data.value = await appFetch.value('/__sitemap__/debug.json').catch((err) => { console.error('Failed to fetch sitemap debug data:', err) return null }) as typeof data.value } export async function refreshProductionData() { if (!appFetch.value || !productionUrl.value) return productionLoading.value = true productionRemoteDebugData.value = null // Try fetching the full debug endpoint from production first (proxied through local server) const remoteDebug = await appFetch.value('/__sitemap__/debug-production.json', { query: { url: productionUrl.value, mode: 'debug' }, }).catch(() => null) as (typeof data.value & { error?: string }) | null if (remoteDebug && !remoteDebug.error && remoteDebug.sitemaps && !Array.isArray(remoteDebug.sitemaps)) { // Response has object sitemaps (debug.json format) rather than array (XML fallback format) productionRemoteDebugData.value = remoteDebug productionLoading.value = false return } // Fall back to XML-based validation productionData.value = await appFetch.value('/__sitemap__/debug-production.json', { query: { url: productionUrl.value }, }).catch((err: Error) => { console.error('Failed to fetch production sitemap data:', err) return null }) as ProductionDebugResponse | null productionLoading.value = false } // Re-fetch the (global) debug data when the host connects or a manual refresh fires watch([appFetch, refreshTime], () => { refreshSources() }) // Sync production URL from siteConfig when debug data loads watch(data, (val) => { if (val?.siteConfig?.url) productionUrl.value = val.siteConfig.url }, { immediate: true }) // Fetch production data when switching to production mode watch(isProductionMode, (isProd) => { if (isProd && !productionData.value && !productionRemoteDebugData.value) refreshProductionData() })