import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api/index';
import type { CmsProductParameter, CmsProductParameterParam } from './model';


/**
 * 分页查询产品参数
 */
export async function pageCmsProductParameter(params: CmsProductParameterParam) {
  const res = await request.get<ApiResult<PageResult<CmsProductParameter>>>(
    '/cms/cms-product-parameter/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询产品参数列表
 */
export async function listCmsProductParameter(params?: CmsProductParameterParam) {
  const res = await request.get<ApiResult<CmsProductParameter[]>>(
    '/cms/cms-product-parameter',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加产品参数
 */
export async function addCmsProductParameter(data: CmsProductParameter) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-product-parameter',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改产品参数
 */
export async function updateCmsProductParameter(data: CmsProductParameter) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-product-parameter',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

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

/**
 * 根据id查询产品参数
 */
export async function getCmsProductParameter(id: number) {
  const res = await request.get<ApiResult<CmsProductParameter>>(
    '/cms/cms-product-parameter/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
