import axios from 'axios';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import * as urlJoin from 'url-join';
{{#if routeInterfaceRelativePath }}import {
    {{#if hasBody}} {{{ bodyInterface }}}, {{/if}}
    {{#if hasQuery}} {{{ queryInterface }}}, {{/if}}
    {{#if hasPathParams}} {{{ pathParamsInterface }}}, {{/if}}
    {{{ routeSuccessInterface }}}
} from '{{{ routeInterfaceRelativePath }}}';{{/if}}
import { BaseParams } from '{{{ clientTypeRelativePath }}}';
import { RequestError, RequestSuccess } from '{{{ requestTypeRelativePath }}}';
import { {{{ functionInterfaceName }}} } from './{{{ functionInterfaceRelativePath }}}';

export const {{{ functionName }}} = ({ baseUrl, headers }: BaseParams): {{{ functionInterfaceName }}} => async ({{#ifOr hasBody hasQuery hasPathParams}}params: {
    {{#if hasBody}} body: {{{ bodyInterface }}}; {{/if}}
    {{#if hasQuery}} query: {{{ queryInterface }}}; {{/if}}
    {{#if hasPathParams}} pathParams: {{{ pathParamsInterface }}}; {{/if}}
}{{/ifOr}}): Promise<
    RequestSuccess<{{#if routeSuccessInterface}}{{{ routeSuccessInterface }}}{{/if}}{{#ifNot routeSuccessInterface}}void{{/ifNot}}> | RequestError
> => {
    try {
        {{#ifOr hasBody hasQuery hasPathParams}} const { {{#if hasQuery}} query, {{/if}} {{#if hasBody}} body, {{/if}} {{#if hasPathParams}} pathParams {{/if}} } = params {{/ifOr}}
        const url = urlJoin(baseUrl, `{{{ routePath }}}`);
const result = await axios.{{{ method }}}<{{#if routeSuccessInterface}}{{{ routeSuccessInterface }}}{{/if}}{{#ifNot routeSuccessInterface}}void{{/ifNot}}>(url,{{#if hasBody}} body,{{/if}}{{#if shouldHaveDefaultBody}}{},{{/if}} { headers{{#if hasQuery}}, params: query{{/if}} });
        return { hasFailed: false, status: result.status, data: result.data };
    } catch (e: any) {
        if (axios.isAxiosError(e)) {
            if (e.response !== undefined) {
                return {
                    hasFailed: true,
                    status: e.response.status,
                    error: e.response.data,
                };
            }
            if (e.request !== undefined) {
                return {
                    hasFailed: true,
                    status: 503,
                    error: e.request,
                };
            }
        }
        return {
            hasFailed: true,
            status: 500,
            error: e.toString(),
        };
    }
};
