﻿
// ReSharper disable InconsistentNaming

export class UrlEncodedRequestConsumingClient {
    private http: { fetch(url: RequestInfo, init?: RequestInit): Promise<Response> };
    private baseUrl: string;
    protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;

    constructor(baseUrl?: string, http?: { fetch(url: RequestInfo, init?: RequestInit): Promise<Response> }) {
        this.http = http ? http : window as any;
        this.baseUrl = baseUrl ?? "";
    }

    addMessage(message: Foo | null | undefined, messageId: string | null | undefined, date: Date | undefined, list: string[] | null | undefined, signal?: AbortSignal): Promise<void> {
        let url_ = this.baseUrl + "/api/UrlEncodedRequestConsuming";
        url_ = url_.replace(/[?&]$/, "");

        let content_ = "";
        if (message !== undefined)
            content_ += encodeURIComponent("message") + "=" + encodeURIComponent("" + message) + "&";
        if (messageId !== undefined)
            content_ += encodeURIComponent("messageId") + "=" + encodeURIComponent("" + messageId) + "&";
        if (date === null)
            throw new globalThis.Error("The parameter 'date' cannot be null.");
        else if (date !== undefined)
            content_ += encodeURIComponent("date") + "=" + encodeURIComponent(date ? "" + date.toJSON() : "") + "&";
        if (list !== undefined)
            list && list.forEach(item => { content_ += encodeURIComponent("list") + "=" + encodeURIComponent("" + item) + "&"; });
        content_ = content_.replace(/&$/, "");

        let options_: RequestInit = {
            body: content_,
            method: "POST",
            signal,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
            }
        };

        return this.http.fetch(url_, options_).then((_response: Response) => {
            return this.processAddMessage(_response);
        });
    }

    protected processAddMessage(response: Response): Promise<void> {
        const status = response.status;
        let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
        if (status === 204) {
            return response.text().then((_responseText) => {
            return;
            });
        } else if (status !== 200 && status !== 204) {
            return response.text().then((_responseText) => {
            return throwException("An unexpected server error occurred.", status, _responseText, _headers);
            });
        }
        return Promise.resolve<void>(null as any);
    }

    getMessage(messageId: string | null, signal?: AbortSignal): Promise<Foo | null> {
        let url_ = this.baseUrl + "/api/UrlEncodedRequestConsuming?";
        if (messageId === undefined)
            throw new globalThis.Error("The parameter 'messageId' must be defined.");
        else if(messageId !== null)
            url_ += "messageId=" + encodeURIComponent("" + messageId) + "&";
        url_ = url_.replace(/[?&]$/, "");

        let options_: RequestInit = {
            method: "GET",
            signal,
            headers: {
                "Accept": "application/json"
            }
        };

        return this.http.fetch(url_, options_).then((_response: Response) => {
            return this.processGetMessage(_response);
        });
    }

    protected processGetMessage(response: Response): Promise<Foo | null> {
        const status = response.status;
        let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
        if (status === 200) {
            return response.text().then((_responseText) => {
            let result200: any = null;
            let resultData200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
            result200 = resultData200 ? Foo.fromJS(resultData200) : null as any;
            return result200;
            });
        } else if (status !== 200 && status !== 204) {
            return response.text().then((_responseText) => {
            return throwException("An unexpected server error occurred.", status, _responseText, _headers);
            });
        }
        return Promise.resolve<Foo | null>(null as any);
    }
}

export class Foo implements IFoo {
    bar?: string | undefined;

    constructor(data?: IFoo) {
        if (data) {
            for (var property in data) {
                if (data.hasOwnProperty(property))
                    (this as any)[property] = (data as any)[property];
            }
        }
    }

    init(_data?: any) {
        if (_data) {
            this.bar = _data["Bar"];
        }
    }

    static fromJS(data: any): Foo {
        data = typeof data === 'object' ? data : {};
        let result = new Foo();
        result.init(data);
        return result;
    }

    toJSON(data?: any) {
        data = typeof data === 'object' ? data : {};
        data["Bar"] = this.bar;
        return data;
    }
}

export interface IFoo {
    bar?: string | undefined;
}

export class ApiException extends Error {
    override message: string;
    status: number;
    response: string;
    headers: { [key: string]: any; };
    result: any;

    constructor(message: string, status: number, response: string, headers: { [key: string]: any; }, result: any) {
        super();

        this.message = message;
        this.status = status;
        this.response = response;
        this.headers = headers;
        this.result = result;
    }

    protected isApiException = true;

    static isApiException(obj: any): obj is ApiException {
        return obj.isApiException === true;
    }
}

function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): any {
    if (result !== null && result !== undefined)
        throw result;
    else
        throw new ApiException(message, status, response, headers, null);
}