import { qs } from "../../Libs/Utils"; import ExtraNavigationInterface from "../../Interfaces/ExtraNavigationInterface"; /** * Config passed by the page that uses this API (Setup Wizard or Video Embed). * Supplied via wp_localize_script() and then configureWizardApi() at init. */ export interface WizardApiConfig { apiUrl: string; wpNonce: string; adminUrl?: string; } let apiConfig: WizardApiConfig | null = null; /** * Set the API config so apiFetch() can use the correct nonce and base URL. * Call this once at page init with the object from wp_localize_script() * (e.g. dmProSetupWizard or dmProAdminEmbed). */ export function configureWizardApi(config: WizardApiConfig): void { apiConfig = config; } function getConfig(): WizardApiConfig { if (!apiConfig) { throw new Error( "WizardApi: No API config. Call configureWizardApi() at page init with your localized script data (e.g. dmProSetupWizard or dmProAdminEmbed)." ); } return apiConfig; } /** * Changes the status of a button. */ export function changeButtonStatus(buttonId: string, status: 'loading' | 'success' | 'error' | 'reset', text?: string): void { const button = qs(buttonId); if (!button) return; // Remove all status classes button.classList.remove('loading', 'success', 'error', 'dm-pro--button-processing'); switch (status) { case 'loading': button.disabled = true; button.classList.add('loading', 'dm-pro--button-processing'); button.textContent = text || 'Loading...'; break; case 'success': button.disabled = true; button.classList.add('success'); button.textContent = text || 'Success!'; break; case 'error': button.disabled = false; button.classList.add('error'); button.textContent = text || 'Error. Try Again'; break; case 'reset': button.disabled = false; button.textContent = text || 'Submit'; break; } } /** * Generic API handler to handle fetch and basic response parsing */ async function apiFetch(endpoint: string, method: string = 'GET', data: any = null) { const config = getConfig(); const options: RequestInit = { method, credentials: 'same-origin', headers: { 'Content-Type': 'application/json', 'X-WP-Nonce': config.wpNonce } }; if (data) { options.body = JSON.stringify(data); } const response = await fetch(config.apiUrl + endpoint, options); if (!response.ok) { const errorData = await response.json(); throw errorData; } return await response.json(); } /** * Stores the API credentials. */ export async function storeApi(apiData: { api_key: string, api_secret: string, channel_id: string }) { return await apiFetch("/store-api", 'POST', apiData); } /** * Fetches player IDs (automated setup). */ export async function setupAutomated() { return await apiFetch("/get-player-ids", 'GET'); } /** * Stores the contextual embed settings. */ export async function storeContextualEmbedSettings(settingsData: any) { return await apiFetch("/store-contextual-embed-settings", 'POST', settingsData); } /** * Stores the manual embed settings. */ export async function storeManualEmbedSettings(settingsData: any) { return await apiFetch("/store-manual-embed-settings", 'POST', settingsData); } /** * Updates the current wizard step on the server. */ export async function updateServerStep(step: number, extra: ExtraNavigationInterface = {}) { return await apiFetch("/wizard-step", 'POST', { step, ...extra }); } export async function removeContextualSettings() { return await apiFetch("/destroy-contextual-settings", 'DELETE'); }