import { Types } from '@pawel-up/data-mock/Types.js';
import { Time } from '@pawel-up/data-mock/Time.js';
import { Lorem } from '@pawel-up/data-mock/Lorem.js';
import { Internet } from '@pawel-up/data-mock/Internet.js';
import { Har } from '@pawel-up/data-mock/Har.js';
import Http from '@pawel-up/data-mock/Http.js';
import { HttpResponse } from '../../models/HttpResponse.js';
import { Kind as ResponseRedirectKind } from '../../models/ResponseRedirect.js';
import { HttpResponseKind, ResponseKind } from '../../models/kinds.js';
export class Response {
    types;
    lorem;
    time;
    http;
    har;
    internet;
    constructor(init = {}) {
        this.types = new Types(init.seed);
        this.lorem = new Lorem(init);
        this.time = new Time(init);
        this.http = new Http(init);
        this.har = new Har(init);
        this.internet = new Internet(init);
    }
    _getPayload(mime) {
        if (!mime) {
            return undefined;
        }
        switch (mime) {
            case 'multipart/form-data':
                return this.http.formData.form();
            default:
                return this.http.payload.payload(mime);
        }
    }
    async httpResponse(init = {}) {
        const mime = this.http.headers.contentType(init.payload);
        const body = this._getPayload(mime);
        const headers = this.http.headers.headers('response', { mime: mime });
        const statusGroup = init.statusGroup ? init.statusGroup : this.types.number({ min: 2, max: 5 });
        const sCode = this.types.number({ min: 0, max: 99 }).toString();
        const code = Number(`${statusGroup}${sCode.padStart(2, '0')}`);
        const status = this.lorem.word();
        const result = {
            kind: HttpResponseKind,
            status: code,
            statusText: status,
            headers,
        };
        const response = new HttpResponse(result);
        if (body) {
            await response.writePayload(body);
        }
        return response.toJSON();
    }
    async response(init = {}) {
        const base = await this.httpResponse(init);
        const length = this.types.number({ min: 10, max: 4000 });
        const result = {
            ...base,
            kind: ResponseKind,
            loadingTime: length,
        };
        if (init.timings) {
            result.timings = this.har.timing(init);
        }
        return result;
    }
    size() {
        const result = {
            request: this.types.number({ min: 10 }),
            response: this.types.number({ min: 10 }),
        };
        return result;
    }
    async redirect(init = {}) {
        const start = this.time.timestamp();
        const end = this.time.timestamp({ min: start + 1 });
        const info = {
            kind: ResponseRedirectKind,
            startTime: start,
            endTime: end,
            url: this.internet.uri(),
            response: await this.httpResponse({ ...init, statusGroup: 3 }),
        };
        if (init.timings) {
            info.timings = this.har.timing(init);
        }
        return info;
    }
    async redirects(size = 1, init) {
        const result = [];
        for (let i = 0; i < size; i++) {
            result.push(this.redirect(init));
        }
        return Promise.all(result);
    }
}
//# sourceMappingURL=Response.js.map