import createClient from 'openapi-fetch'
import { apiFetch } from '../services/api'
import type { paths } from './generated/schema'

// openapi-fetch constructs a Request before invoking our authenticated fetch
// wrapper. A concrete same-origin base keeps that construction valid in Node
// contract tests while preserving same-origin requests in the browser.
const apiBaseUrl = typeof window !== 'undefined' && window.location?.origin
  ? window.location.origin
  : 'http://localhost'

export const apiClient = createClient<paths>({
  baseUrl: apiBaseUrl,
  fetch: apiFetch,
})
