/* eslint-disable @typescript-eslint/no-empty-function */
import process from 'process';
import cluster from 'cluster';
import { HttpProject, Kind as HttpProjectKind } from '../../models/HttpProject.js';
import { sleep } from '../../lib/timers/Timers.js';
import { ProjectRunner } from './ProjectRunner.js';
import { State } from './enums.js';
import { InMemoryCookieJar } from '../../cookies/InMemoryCookieJar.js';
class ProjectExeWorker extends ProjectRunner {
    initialize() {
        if (cluster.isPrimary) {
            throw new Error(`This file should not be called directly.`);
        }
        if (process.send) {
            process.send({ cmd: 'online' });
        }
        process.on('message', this.messageHandler.bind(this));
    }
    messageHandler(message) {
        switch (message.cmd) {
            case 'run':
                this.run(message.data);
                break;
        }
    }
    async run(options) {
        options.cookies = new InMemoryCookieJar();
        try {
            const schema = options.project;
            let project;
            if (schema.kind === HttpProjectKind) {
                project = new HttpProject(schema);
            }
            else {
                throw new Error(`Unknown project kind: ${schema.kind}`);
            }
            await this.configure(project, options);
            await this.execute();
        }
        catch (e) {
            const cause = e;
            if (process.send) {
                process.send({ cmd: 'error', data: cause.message });
            }
        }
    }
    async execute() {
        const { root } = this;
        if (!root) {
            throw new Error(`The project runner is not configured.`);
        }
        function unhandledRejection() { }
        process.on('unhandledRejection', unhandledRejection);
        this._state = State.Running;
        this.startTime = Date.now();
        while (this.remaining > 0) {
            this.remaining--;
            await this.executeIteration();
            this.index++;
            if (this.remaining && this.options?.iterationDelay) {
                await sleep(this.options.iterationDelay);
            }
        }
        process.off('unhandledRejection', unhandledRejection);
        this.endTime = Date.now();
        const log = await this.createReport();
        if (process.send) {
            process.send({ cmd: 'result', data: log.iterations });
        }
        this._state = State.Idle;
        return log;
    }
}
const instance = new ProjectExeWorker();
instance.noEmit = true;
instance.initialize();
//# sourceMappingURL=ProjectRunnerWorker.js.map