"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Checker = void 0;
const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
const worker_threads_1 = require("worker_threads");
const worker_1 = require("./worker");
if (!worker_threads_1.isMainThread) {
    process.stdout.isTTY = true;
}
class Checker {
    constructor({ name, absFilePath, createDiagnostic, build }) {
        this.name = name;
        this.absFilePath = absFilePath;
        this.build = build;
        this.createDiagnostic = createDiagnostic;
        this.build = build;
    }
    static log(...args) {
        this.logger.forEach((fn) => fn(...args));
    }
    prepare() {
        const script = (0, worker_1.createScript)({
            absFilename: this.absFilePath,
            buildBin: this.build.buildBin,
            serverChecker: { createDiagnostic: this.createDiagnostic },
        });
        this.script = script;
        return script;
    }
    initMainThread() {
        (0, tiny_invariant_1.default)(this.script, `script should be created in 'prepare', but got ${this.script}`);
        if (worker_threads_1.isMainThread) {
            const createServeAndBuild = this.script.mainScript();
            return createServeAndBuild;
        }
    }
    initWorkerThread() {
        (0, tiny_invariant_1.default)(this.script, `script should be created in 'prepare', but got ${this.script}`);
        if (!worker_threads_1.isMainThread) {
            this.script.workerScript();
        }
    }
}
exports.Checker = Checker;
Checker.logger = [
    (...args) => {
        console.log(args[0].payload);
    },
];
