import request from '~/utils/request';
import type { ApiResult } from '@/api';
import type { Dict, DictParam } from './model';
import { SERVER_API_URL } from '@/config';

/**
 * 查询字典列表
 */
export async function listDictionaries(params?: DictParam) {
  const res = await request.get<ApiResult<Dict[]>>(
    SERVER_API_URL + '/system/dict',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加字典
 */
export async function addDict(data: Dict) {
  const res = await request.post<ApiResult<unknown>>(
    SERVER_API_URL + '/system/dict',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改字典
 */
export async function updateDict(data: Dict) {
  const res = await request.put<ApiResult<unknown>>(
    SERVER_API_URL + '/system/dict',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除字典
 */
export async function removeDict(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    SERVER_API_URL + '/system/dict/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
