import http from './http'
import type { ConfigEnvelope, ConfigSection } from '@/types/config'
import { mockGetConfig, mockPutConfig } from '@/mock/server'

// 见 auth.ts 中关于 VITE_USE_MOCK 的说明：生产默认走 Platform.Server `/api/config/*`，
// 仅当 `.env.development` 显式声明 `VITE_USE_MOCK=true` 时才回到前端假数据。
const MOCK = import.meta.env.VITE_USE_MOCK === 'true'

export async function getConfig<T = unknown>(section: ConfigSection): Promise<ConfigEnvelope<T>> {
  if (MOCK) return mockGetConfig<T>(section)
  const { data } = await http.get<ConfigEnvelope<T>>(`/config/${section}`)
  return data
}

export async function putConfig<T = unknown>(
  section: ConfigSection,
  payload: T
): Promise<ConfigEnvelope<T>> {
  if (MOCK) return mockPutConfig<T>(section, payload)
  const { data } = await http.put<ConfigEnvelope<T>>(`/config/${section}`, payload)
  return data
}
