import request from '~/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { CmsArticleCategory, CmsArticleCategoryParam } from './model';


/**
 * 分页查询文章分类表
 */
export async function pageCmsArticleCategory(params: CmsArticleCategoryParam) {
  const res = await request.get<ApiResult<PageResult<CmsArticleCategory>>>(
    '/cms/cms-article-category/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询文章分类表列表
 */
export async function listCmsArticleCategory(params?: CmsArticleCategoryParam) {
  const res = await request.get<ApiResult<CmsArticleCategory[]>>(
    '/cms/cms-article-category',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加文章分类表
 */
export async function addCmsArticleCategory(data: CmsArticleCategory) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-article-category',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改文章分类表
 */
export async function updateCmsArticleCategory(data: CmsArticleCategory) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-article-category',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除文章分类表
 */
export async function removeCmsArticleCategory(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-article-category/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除文章分类表
 */
export async function removeBatchCmsArticleCategory(
  data: (number | undefined)[]
) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-article-category/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询文章分类表
 */
export async function getCmsArticleCategory(id: number) {
  const res = await request.get<ApiResult<CmsArticleCategory>>(
    '/cms/cms-article-category/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
