import request from '@/utils/request';
import type { ApiResult } from '@/api/index';
import type { Dictionary, DictionaryParam } from './model';
import {SERVER_API_URL} from "@/utils/server";

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

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

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

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