import { createAstratoApp } from '@astrato-io/app-runtime'; import { createVisualizationAvailableFields } from '@astrato-io/visualizations/definitions'; import type { DemoView } from './demo-shell.js'; import { createDemoDashboard } from './demo-shell.js'; /** Astrato-hosted adapter. Externally hosted apps supply their own authenticated client. */ export async function startDemo( root: HTMLElement, plan: { title: string; scope: string; buildViews: (fields: ReturnType) => readonly DemoView[]; }, ) { root.textContent = 'Preparing your app…'; let app: Awaited> | undefined; try { const liveApp = await createAstratoApp(); app = liveApp; const load = liveApp.client.services.engine.fields.getFieldMetadata; if (!load) throw new Error('Field metadata is unavailable.'); const fieldMetadata = await load({ ...liveApp.dataBinding }); const dashboard = await createDemoDashboard({ root, title: plan.title, scope: plan.scope, client: liveApp.client, dataBinding: { ...liveApp.dataBinding }, fieldMetadata, views: plan.buildViews(createVisualizationAvailableFields(fieldMetadata)), }); return { setEmpty: dashboard.setEmpty, async destroy() { await dashboard.destroy(); liveApp.destroy(); }, }; } catch (error) { app?.destroy(); root.textContent = error instanceof Error ? error.message : 'App initialization failed.'; throw error; } }