import type { ExcludedContentItem, ExcludedContentResponse, } from "./types"; function getConfig(): { apiUrl: string; nonce: string } { return window.flexibleChatExcludedContent; } function getApiUrl(endpoint: string): string { const config = getConfig(); return `${config.apiUrl}${endpoint}`; } function getHeaders(): HeadersInit { const config = getConfig(); return { "Content-Type": "application/json", "X-WP-Nonce": config.nonce, }; } export async function fetchExcludedContent( search: string, signal?: AbortSignal, ): Promise { const params = new URLSearchParams(); if (search.trim() !== "") { params.set("search", search.trim()); } const query = params.toString(); const response = await fetch( getApiUrl(`/excluded-content${query ? `?${query}` : ""}`), { headers: getHeaders(), signal, }, ); if (!response.ok) { throw new Error(`Failed to fetch excluded content: ${response.status}`); } return response.json(); } export async function removeExcludedContent( item: ExcludedContentItem, ): Promise { const response = await fetch( getApiUrl(`/excluded-content/${item.kind}/${item.id}`), { method: "DELETE", headers: getHeaders(), }, ); if (!response.ok) { throw new Error(`Failed to remove exclusion: ${response.status}`); } }