import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { CmsNavigation, CmsNavigationParam } from './model';
import type {CmsArticle} from "@/api/cms/cmsArticle/model";

/**
 * 分页查询网站导航记录表
 */
export async function pageCmsNavigation(params: CmsNavigationParam) {
  const res = await request.get<ApiResult<PageResult<CmsNavigation>>>(
    '/cms/cms-navigation/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询网站导航记录表列表
 */
export async function listCmsNavigation(params?: CmsNavigationParam) {
  const res = await request.get<ApiResult<CmsNavigation[]>>(
    '/cms/cms-navigation',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询导航列表（树形结构）
 */
export async function treeNavigation(params?: CmsNavigationParam) {
  const res = await request.get<ApiResult<CmsNavigation[]>>(
    '/cms/cms-navigation/tree',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
/**
 * 添加网站导航记录表
 */
export async function addCmsNavigation(data: CmsNavigation) {
  const res = await request.post<ApiResult<unknown>>(
    '/cms/cms-navigation',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改网站导航记录表
 */
export async function updateCmsNavigation(data: CmsNavigation) {
  const res = await request.put<ApiResult<unknown>>(
    '/cms/cms-navigation',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除网站导航记录表
 */
export async function removeCmsNavigation(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-navigation/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除网站导航记录表
 */
export async function removeBatchCmsNavigation(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/cms/cms-navigation/batch',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询网站导航记录表
 */
export async function getCmsNavigation(id: number) {
  const res = await request.get<ApiResult<CmsNavigation>>(
    '/cms/cms-navigation/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

export async function getNavigationByPath(params: CmsNavigationParam) {
  const res = await request.get<ApiResult<CmsNavigation>>(
      '/cms/cms-navigation/getNavigationByPath',
      params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据code查询导航
 */
export async function getByCode(code: string) {
  const res = await request.get<ApiResult<CmsArticle>>(
    '/cms/cms-navigation/getByCode/' + code
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
