/* eslint-disable no-underscore-dangle,no-console */
/**
 * THIS IS AN AUTO-GENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
 */
import type { ReactText } from 'react';
import type { SortOrder } from 'antd/lib/table/interface';
import {
  requestList,
  requestInfo,
  requestCreate,
  requestUpdate,
  requestDelete,
  requestManyCreate,
  requestManyUpdate,
  requestManyDelete,
  requestAggregate,
  fieldsToGql,
  fragmentsToGql,
  aggregateFieldsToGql,
} from './utils';
import type { FieldsType, FragmentsType, AggregateFieldsType } from './utils';
import { getFields } from '@/generated/services/utils';

{{each tables type table}}
export async function create{{type}}(
  params: API.CreateParams<API.{{type}}>,
  fields?: FieldsType,
  fragments?: FragmentsType,
) {
  const table = '{{table}}';
  const defaultFields = getFields(table);
  const fieldsGql = fieldsToGql(fields || defaultFields);
  const fragmentsGql = fragmentsToGql(fragments);
  return requestCreate(table, params, fieldsGql, fragmentsGql);
}

export async function request{{type}}Info(id: string | number, fields?: FieldsType, fragments?: FragmentsType) {
  const table = '{{table}}';
  const defaultFields = getFields(table);
  const fieldsGql = fieldsToGql(fields || defaultFields);
  const fragmentsGql = fragmentsToGql(fragments);
  {{if ['Int', 'BigInt'].includes(doc[type].id.dbType)}}
  return requestInfo(table, Number(id), fieldsGql, fragmentsGql);
  {{else}}
  return requestInfo(table, String(id), fieldsGql, fragmentsGql);
  {{/if}}
}

export async function request{{type}}List(
  params: API.QueryParams<API.{{type}}>,
  sorter?: Record<string, SortOrder>,
  filter?: Record<string, ReactText[] | null>,
  fields?: FieldsType,
  fragments?: FragmentsType,
) {
  const table = '{{table}}';
  const defaultFields = getFields(table);
  const fieldsGql = fieldsToGql(fields || defaultFields);
  const fragmentsGql = fragmentsToGql(fragments);
  return requestList(table, fieldsGql, params, sorter, filter, fragmentsGql);
}

export async function update{{type}}(
  params: API.UpdateParams<API.{{type}}>, 
  originValue?: API.UpdateParams<API.{{type}}>,
  fields?: FieldsType,
  fragments?: FragmentsType,
) {
  const table = '{{table}}';
  const defaultFields = getFields(table);
  const fieldsGql = fieldsToGql(fields || defaultFields);
  const fragmentsGql = fragmentsToGql(fragments);
  return requestUpdate(table, params, originValue, fieldsGql, fragmentsGql);
}

export async function delete{{type}}(params: Partial<API.{{type}}>) {
  return requestDelete('{{table}}', params);
}

export async function createMany{{type}}(params: any[], skipDuplicates = false) {
  return requestManyCreate('{{table}}', params, skipDuplicates);
}

export async function updateMany{{type}}(ids:number[] | string[], params: API.UpdateParams<API.{{type}}>) {
  return requestManyUpdate('{{table}}', ids, params);
}

export async function deleteMany{{type}}(ids:number[] | string[]) {
  return requestManyDelete('{{table}}', ids);
}

export async function aggregate{{type}}(
  params: API.QueryParams<API.{{type}}>,
  fields: AggregateFieldsType,
) {
  const fieldsGql = aggregateFieldsToGql(fields);
  return requestAggregate('{{table}}', fieldsGql, params);
}
{{/each}}
