import { AxiosResponse } from 'axios';
import { AbpResponse } from '../../models/http/abpResponse';
import connectService from '../../services/connectService';
import { IHttpCommand } from './httpCommand';
import { HttpMethod } from './httpMethod';

export abstract class HttpCommandBase<TRequest, TResponse, TError> implements IHttpCommand<TRequest, TResponse, TError> {

    public abstract endpoint: string;

    public abstract method: HttpMethod;

    public buildEndpoint(): string {
        if (this.endpoint.indexOf('{externalId}') !== -1 && connectService.currentUser) {
            return this.endpoint.replace('{externalId}', connectService.currentUser.externalId);
        } else {
            return this.endpoint;
        }
    }

    public abstract buildRequestContent(): TRequest;

    public transformResponse(rawResponse: AbpResponse<TResponse> | null): TResponse | null {
        if (rawResponse) {
            return rawResponse.result;
        } else {
            return null;
        }
    }

    public handleError(_rawResponse: AxiosResponse<AbpResponse<TError>> | null): void {
        //
    }
}