import { post } from '@/lib/request'
import { USE_MOCK, delay, paginate } from '@/mock'
import type { DictInfo } from '../system'
import type { PageResult } from '@/lib/request'

const BASE_URL = '/sys/dict'

const mockDicts: DictInfo[] = [
  { id: '1', type: 'gender', label: '男', value: '1', sort: 1, desc: '性别-男' },
  { id: '2', type: 'gender', label: '女', value: '2', sort: 2, desc: '性别-女' },
  { id: '3', type: 'gender', label: '未知', value: '0', sort: 3, desc: '性别-未知' },
]

export async function getDictList(params: { current: number; pageSize: number; type?: string }) {
  if (USE_MOCK) {
    await delay()
    let list = [...mockDicts]
    if (params.type) list = list.filter(d => d.type === params.type)
    return paginate(list, params.current, params.pageSize)
  }
  return post<PageResult<DictInfo>>(`${BASE_URL}/list`, params)
}

export async function createDict(data: Partial<DictInfo>) {
  if (USE_MOCK) { await delay(); return null }
  return post<null>(`${BASE_URL}/create`, data)
}

export async function updateDict(data: Partial<DictInfo> & { id: string }) {
  if (USE_MOCK) { await delay(); return null }
  return post<null>(`${BASE_URL}/update`, data)
}

export async function deleteDict(ids: string[]) {
  if (USE_MOCK) { await delay(); return null }
  return post<null>(`${BASE_URL}/delete`, { ids })
}
