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


/**
 * 分页查询域名
 */
export async function pageCmsProductUrl(params: CmsProductUrlParam) {
  const res = await request.get<ApiResult<PageResult<CmsProductUrl>>>(
    '/cms/cms-product-url/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询域名列表
 */
export async function listCmsProductUrl(params?: CmsProductUrlParam) {
  const res = await request.get<ApiResult<CmsProductUrl[]>>(
    '/cms/cms-product-url',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加域名
 */
export async function addCmsProductUrl(data: CmsProductUrl) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-product-url',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改域名
 */
export async function updateCmsProductUrl(data: CmsProductUrl) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-product-url',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除域名
 */
export async function removeCmsProductUrl(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-product-url/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除域名
 */
export async function removeBatchCmsProductUrl(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-product-url/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询域名
 */
export async function getCmsProductUrl(id: number) {
  const res = await request.get<ApiResult<CmsProductUrl>>(
    '/cms/cms-product-url/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
