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


/**
 * 分页查询规格
 */
export async function pageCmsProductSpec(params: CmsProductSpecParam) {
  const res = await request.get<ApiResult<PageResult<CmsProductSpec>>>(
    '/cms/cms-product-spec/page',
    {
      params
    }
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询规格列表
 */
export async function listCmsProductSpec(params?: CmsProductSpecParam) {
  const res = await request.get<ApiResult<CmsProductSpec[]>>(
    '/cms/cms-product-spec',
    {
      params
    }
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加规格
 */
export async function addCmsProductSpec(data: CmsProductSpec) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-product-spec',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改规格
 */
export async function updateCmsProductSpec(data: CmsProductSpec) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-product-spec',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

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

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

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