import { getConfig } from '../state/store.js'; type FetchOptions = RequestInit & { query?: Record }; function buildUrl(path: string, query?: Record) { const cfg = getConfig(); const base = cfg?.baseApi || ''; const url = new URL(path.startsWith('http') ? path : `${base}${path}`, window.location.origin); if (query) { Object.entries(query).forEach(([k, v]) => { if (v === undefined || v === null) return; url.searchParams.set(k, String(v)); }); } return url.toString(); } async function request(path: string, options: FetchOptions = {}): Promise { const { query, headers, ...rest } = options; const url = buildUrl(path, query); const res = await fetch(url, { ...rest, headers: { 'Content-Type': 'application/json', ...(headers || {}) } }); if (!res.ok) { const text = await res.text().catch(() => res.statusText); throw new Error(`Request failed: ${res.status} ${text}`); } const contentType = res.headers.get('content-type'); if (contentType && contentType.includes('application/json')) { return (await res.json()) as T; } return (await res.text()) as unknown as T; } export const api = { me: () => request<{ user?: { username: string; discriminator: string; isAdmin?: boolean } }>('/me'), guilds: () => request<{ guilds: Array<{ id: string; name: string; icon?: string }> }>('/guilds'), overview: (guildId: string) => request(`/overview`, { query: { guildId } }), settings: (guildId: string) => request(`/settings`, { query: { guildId } }), saveSettings: (payload: Record) => request('/settings', { method: 'POST', body: JSON.stringify(payload) }), modules: (guildId: string) => request(`/modules`, { query: { guildId } }), tickets: (guildId: string) => request(`/tickets`, { query: { guildId } }), pipeline: (guildId: string, filter?: string) => request(`/tickets/pipeline`, { query: { guildId, filter } }), sla: (guildId: string, range?: number) => request(`/tickets/sla`, { query: { guildId, range } }), automations: (guildId: string) => request(`/automations`, { query: { guildId } }), saveAutomation: (payload: Record) => request(payload['id'] ? `/automations/${payload['id']}` : '/automations', { method: payload['id'] ? 'PUT' : 'POST', body: JSON.stringify(payload) }), kb: (guildId: string) => request(`/kb`, { query: { guildId } }), saveKb: (payload: Record) => request(payload['id'] ? `/kb/${payload['id']}` : '/kb', { method: payload['id'] ? 'PUT' : 'POST', body: JSON.stringify(payload) }), reactionRoles: (guildId: string) => request(`/reactionroles`, { query: { guildId } }), saveReactionRole: (payload: Record & { id?: string }) => request(payload.id ? `/reactionroles/${payload.id}` : '/reactionroles', { method: payload.id ? 'PUT' : 'POST', body: JSON.stringify(payload) }), events: (guildId: string) => request(`/events`, { query: { guildId } }), saveEvent: (payload: Record & { id?: string }) => request(payload.id ? `/events/${payload.id}` : '/events', { method: payload.id ? 'PUT' : 'POST', body: JSON.stringify(payload) }), statuspage: (guildId: string) => request(`/statuspage`, { query: { guildId } }), saveStatuspage: (payload: Record) => request('/statuspage', { method: 'POST', body: JSON.stringify(payload) }), serverStats: (guildId: string) => request(`/serverstats`, { query: { guildId } }), saveServerStats: (payload: Record) => request('/serverstats', { method: 'POST', body: JSON.stringify(payload) }), dynamicVoice: (guildId: string) => request(`/dynamicvoice`, { query: { guildId } }), saveDynamicVoice: (payload: Record) => request('/dynamicvoice', { method: 'POST', body: JSON.stringify(payload) }) };