// Mock data adapter — switch between mock and real API
// Set VITE_USE_MOCK=true in .env to enable mock mode
export const USE_MOCK = import.meta.env.VITE_USE_MOCK !== 'false'

export function delay(ms = 300): Promise<void> {
  return new Promise(resolve => setTimeout(resolve, ms))
}

export function paginate<T>(list: T[], current: number, pageSize: number): { list: T[]; total: number; page: number; pageSize: number } {
  const start = (current - 1) * pageSize
  return {
    list: list.slice(start, start + pageSize),
    total: list.length,
    page: current,
    pageSize,
  }
}

/** Wrap mock data in the standard backend response format: {code, data, msg} */
export function mockResponse<T>(data: T, msg = '操作成功'): { code: number; data: T; msg: string } {
  return { code: 200, data, msg }
}

let _idCounter = 1000
export function mockId(): string {
  return String(++_idCounter)
}

export function mockDate(daysAgo = 0): string {
  const d = new Date()
  d.setDate(d.getDate() - daysAgo)
  return d.toISOString()
}
