{{{commentText}}}
import type { Alova, AlovaMethodCreateConfig, AlovaGenerics, Method } from 'alova';
import type { $$userConfigMap, alovaInstance } from '.';
import type apiDefinitions from './apiDefinitions';

type CollapsedAlova = typeof alovaInstance;
type UserMethodConfigMap = typeof $$userConfigMap;

type Alova2MethodConfig<Responded> =
  CollapsedAlova extends Alova<
    AlovaGenerics<
      any,
      any,
      infer RequestConfig,
      infer Response,
      infer ResponseHeader,
      infer L1Cache,
      infer L2Cache,
      infer SE
    >
  >
    ? Omit<
        AlovaMethodCreateConfig<
          AlovaGenerics<Responded, any, RequestConfig, Response, ResponseHeader, L1Cache, L2Cache, SE>,
          any,
          Responded
        >,
        'params'
      >
    : never;

// Extract the return type of transform function that define in $$userConfigMap, if it not exists, use the default type.
type ExtractUserDefinedTransformed<
  DefinitionKey extends keyof typeof apiDefinitions,
  Default
> = DefinitionKey extends keyof UserMethodConfigMap
  ? UserMethodConfigMap[DefinitionKey]['transform'] extends (...args: any[]) => any
    ? Awaited<ReturnType<UserMethodConfigMap[DefinitionKey]['transform']>>
    : Default
  : Default;
type Alova2Method<
  Responded,
  DefinitionKey extends keyof typeof apiDefinitions,
  CurrentConfig extends Alova2MethodConfig<any>
> =
  CollapsedAlova extends Alova<
    AlovaGenerics<
      any,
      any,
      infer RequestConfig,
      infer Response,
      infer ResponseHeader,
      infer L1Cache,
      infer L2Cache,
      infer SE
    >
  >
    ? Method<
        AlovaGenerics<
          CurrentConfig extends undefined
            ? ExtractUserDefinedTransformed<DefinitionKey, Responded>
            : CurrentConfig['transform'] extends (...args: any[]) => any
              ? Awaited<ReturnType<CurrentConfig['transform']>>
              : ExtractUserDefinedTransformed<DefinitionKey, Responded>,
          any,
          RequestConfig,
          Response,
          ResponseHeader,
          L1Cache,
          L2Cache,
          SE
        >
      >
    : never;

{{#schemas}}
{{{.}}}
{{/schemas}}
declare global {
  interface {{{global}}} {
    {{#pathApis}}
    {{{tag}}}: {
     {{#apis}}
      /**
       * ---
       *
       * [{{{method}}}] {{{summary}}}
       *
       * **path:** {{{path}}}
       *
      {{#if pathParametersComment}}
       * ---
       *
       * **Path Parameters**
       * ```ts
       * type PathParameters = {{{pathParametersComment}}}
       * ```
       *
       {{/if}}
       {{#if queryParametersComment}}
       * ---
       *
       * **Query Parameters**
       * ```ts
       * type QueryParameters = {{{queryParametersComment}}}
       * ```
       *
       {{/if}}
       {{#if requestComment}}
       * ---
       *
       * **RequestBody**
       * ```ts
       * type RequestBody = {{{requestComment}}}
       * ```
       *
       {{/if}}
       {{#if responseComment}}
       * ---
       *
       * **Response**
       * ```ts
       * type Response = {{{responseComment}}}
       * ```
       {{/if}}
       */
      {{{name}}}<Config extends Alova2MethodConfig<{{{responseName}}}>{{#or pathParameters queryParameters requestName }} & {
          {{#if pathParameters}}
          pathParams: {{{pathParameters}}};
          {{/if}}
          {{#if queryParameters}}
          params: {{{queryParameters}}};
          {{/if}}
          {{#if requestName}}
          data: {{{requestName}}};
          {{/if}}
      }{{/or}}>(
        config{{#or pathParameters queryParameters requestName }}{{else}}?{{/or}}: Config
      ): Alova2Method<{{{responseName}}}, '{{{pathKey}}}', Config>;
     {{/apis}}
    };
    {{/pathApis}}
  }

  var {{{global}}}: {{{global}}};
}
