import type { GetComponentPropsResponse, GetDocsResponse, GetExampleResponse, GetStyleGuideResponse, ListComponentExamplesResponse, ListDocsResponse, ListExamplesResponse, SearchDocsResponse, } from './types.js' const API_BASE = 'https://ark-ui.com/api' const fetchJson = async (path: string, label: string): Promise => { const response = await fetch(`${API_BASE}${path}`) if (!response.ok) { throw new Error(`Failed to fetch ${label}: ${response.status} ${response.statusText}`) } return response.json() as Promise } export const fetchComponentList = (framework: string) => fetchJson(`/types/${framework}`, 'component list') export const listExamples = (framework: string) => fetchJson(`/examples/${framework}`, 'examples') export const listComponentExamples = async ({ framework, component }: { framework: string; component: string }) => { const data = await fetchJson(`/examples/${framework}/${component}`, 'examples') return data.examples.map((example) => example.id) } export const getExample = ({ framework, component, exampleId, }: { framework: string component: string exampleId: string }) => fetchJson(`/examples/${framework}/${component}/${exampleId}`, 'example') export const getStyleGuide = (component: string) => fetchJson(`/style-guide/${component}`, 'style guide') export const getComponentProps = async ({ framework, component, }: { framework: string component: string }): Promise => { const props = await fetchJson>(`/types/${framework}/${component}`, 'component props') return { framework, component, props, } } export const listDocs = () => fetchJson('/docs', 'docs list') export const searchDocs = (query: string) => fetchJson(`/docs?q=${encodeURIComponent(query)}`, 'docs search') export const getDocs = (slug: string) => fetchJson(`/docs/${slug}`, 'docs page')