/**
 * API mode feature-flag.
 *
 * Set NEXT_PUBLIC_API_MODE=live in .env.local to switch from static mock data
 * to real RouterOS REST API calls.
 *
 * Default: "mock"
 */

export const API_MODE = (
  typeof process !== "undefined"
    ? (process.env.NEXT_PUBLIC_API_MODE ?? "mock")
    : "mock"
) as "mock" | "live"

export const IS_MOCK = API_MODE !== "live"

/**
 * Wrap an async data-fetch so it transparently returns mock data in "mock" mode
 * or calls the live resolver in "live" mode.
 *
 * Example:
 *   export const fetchServers = apiOr(
 *     () => servers,                         // mock
 *     async (cfg) => apiClient(cfg).getAll() // live
 *   )
 */
export function apiOr<T, Args extends unknown[]>(
  mock: (...args: Args) => T | Promise<T>,
  live: (...args: Args) => T | Promise<T>,
): (...args: Args) => Promise<T> {
  return async (...args: Args): Promise<T> => {
    if (IS_MOCK) return mock(...args)
    return live(...args)
  }
}
