// @ts-ignore
/* eslint-disable */
import { AdminAPI } from '@/constants/api';
import { EmployType, JobType } from '@/constants/job';
import { request } from '@umijs/max';
import { SortOrder } from 'antd/es/table/interface';

function transformPageParams(params: API.PageParams & Record<string, any>) {
  params.page = params.current;
  delete params.current;
  Object.keys(params).forEach((key: string) => {
    if (typeof params[key] === 'string' && !params[key]) {
      delete params[key];
    }
  });
  return params;
}

function transformSort(sort: Record<string, SortOrder>) {
  if (!sort) {
    return {};
  }
  const sortField = Object.keys(sort)[0];
  if (!sort[sortField]) {
    return {};
  }
  const asc = sort[sortField] === 'ascend';
  return { sortField, asc };
}

function sortTableList<T extends { created: number; updated: number }>(
  response: API.TableList<T>,
  { sortField, asc }: ReturnType<typeof transformSort>,
): API.TableList<T> {
  if (sortField === 'created' || sortField === 'updated') {
    response.data.sort((itemA, itemB) => {
      const valueA = Number(itemA[sortField]);
      const valueB = Number(itemB[sortField]);
      return asc ? valueA - valueB : valueB - valueA;
    });
  }

  return response;
}

export async function getJobList(params: API.PageParams & Partial<API.JobListItem>, options?: {
  [key: string]: any
}) {
  if (!params.category) {
    params.category = JobType.All;
  }
  if (!params.cityCode) {
    params.cityCode = 'ALL';
  }
  if (!params.employType) {
    params.employType = EmployType.All;
  }
  const result = await request<API.TableList<API.JobListItem>>(AdminAPI.JOB_LIST, {
    method: 'POST',
    data: {
      ...transformPageParams(params),
      ...(options || {}),
    },
  });
  result.success = true;
  return result;
}

/**
 * 更新通告接口，必须传 数据库 id 和 jobId
 */
export async function updateJobInfo(options: API.UpdateJobParams) {
  return request<API.JobListItem>(AdminAPI.JOB_UPDATE, {
    method: 'POST',
    data: {
      ...(options || {}),
    },
  });
}

export async function getGroupList(
  params: API.PageParams & Partial<API.GroupListItem>,
  options?: { [key: string]: any },
) {
  const result = await request<API.TableList<API.GroupListItem>>(AdminAPI.GROUP_LIST, {
    method: 'POST',
    data: {
      ...transformPageParams(params),
      ...(options || {}),
    },
  });
  result.success = true;
  return result;
}

/**
 * 更新群信息，必须传 数据库 id
 */
export async function updateGroupInfo(options: API.UpdateGroupParams) {
  return request<API.GroupListItem>(AdminAPI.GROUP_UPDATE, {
    method: 'POST',
    data: {
      ...(options || {}),
    },
  });
}

export async function getAnchorGroupList(
  params: API.PageParams & Partial<API.AnchorGroupListItem>,
  options?: { [key: string]: any },
) {
  const result = await request<API.TableList<API.AnchorGroupListItem>>(AdminAPI.ANCHOR_GROUP_LIST, {
    method: 'POST',
    data: {
      ...transformPageParams(params),
      ...(options || {}),
    },
  });
  return result;
}

export async function addAnchorGroup(options?: { [key: string]: any }) {
  return request<API.GroupListItem>(AdminAPI.ADD_ANCHOR_GROUP, {
    method: 'POST',
    data: {
      ...(options || {}),
    },
  });
}

export async function getPublisherList(
  params: API.PageParams & Partial<API.PublisherListItem>,
  sort: Record<string, SortOrder>,
) {
  const url = params.hasDeclareOrder ? AdminAPI.PUBLISHER_DECLARATION_LIST : AdminAPI.PUBLISHER_LIST;
  const result = await request<API.TableList<API.PublisherListItem>>(url, {
    method: 'POST',
    data: {
      ...transformPageParams(params),
      ...transformSort(sort),
    },
  });
  return result;
}

export async function updatePublisherInfo(options: API.UpdatePublisherParams) {
  return request<API.PublisherListItem>(AdminAPI.PUBLISHER_UPDATE, {
    method: 'POST',
    data: {
      ...(options || {}),
    },
  });
}

export async function getAnchorList(
  params: API.PageParams & Partial<API.AnchorListItem>,
  options?: { [key: string]: any },
) {
  const result = await request<API.TableList<API.AnchorListItem>>(AdminAPI.ANCHOR_LIST, {
    method: 'POST',
    data: {
      ...transformPageParams(params),
      ...(options || {}),
    },
  });
  return result;
}

export async function updateAnchorInfo(options: API.UpdateAnchorParams) {
  return request<API.DeclarationListItem>(AdminAPI.ANCHOR_UPDATE, {
    method: 'POST',
    data: {
      ...(options || {}),
    },
  });
}

export async function getMaterialList(
  params: API.PageParams & Partial<API.MaterialListItem>,
  sort: Record<string, SortOrder>,
) {
  const formatedSort = transformSort(sort);
  const result = await request<API.TableList<API.MaterialListItem>>(AdminAPI.MATERIAL_LIST, {
    method: 'POST',
    data: {
      ...transformPageParams(params),
      ...formatedSort,
    },
  });

  return sortTableList<API.MaterialListItem>(result, formatedSort);
}

export async function updateMaterialInfo(options: API.UpdateMaterialParams) {
  return request<API.DeclarationListItem>(AdminAPI.MATERIAL_UPDATE, {
    method: 'POST',
    data: {
      ...(options || {}),
    },
  });
}

export async function getDeclarationList(
  params: API.PageParams & Partial<API.DeclarationListItem>,
  options?: { [key: string]: any },
) {
  const result = await request<API.TableList<API.DeclarationListItem>>(AdminAPI.DECLARATION_LIST, {
    method: 'POST',
    data: {
      ...transformPageParams(params),
      ...(options || {}),
    },
  });
  return result;
}

export async function updateDeclarationInfo(options: API.UpdateDeclarationParams) {
  return request<API.DeclarationListItem>(AdminAPI.DECLARATION_UPDATE, {
    method: 'POST',
    data: {
      ...(options || {}),
    },
  });
}
