"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VlsChecker = exports.createDiagnostic = void 0;
const os_1 = __importDefault(require("os"));
const worker_threads_1 = require("worker_threads");
const Checker_1 = require("../../Checker");
const logger_1 = require("../../logger");
const types_1 = require("../../types");
const diagnostics_1 = require("./diagnostics");
const createDiagnostic = (pluginConfig) => {
    let overlay = true;
    let terminal = true;
    let command;
    return {
        config: ({ enableOverlay, enableTerminal, env }) => {
            overlay = enableOverlay;
            terminal = enableTerminal;
            command = env.command;
        },
        configureServer({ root }) {
            return __awaiter(this, void 0, void 0, function* () {
                const workDir = root;
                const onDispatchDiagnosticsSummary = (errorCount, warningCount) => {
                    if (!terminal)
                        return;
                    (0, logger_1.consoleLog)((0, logger_1.composeCheckerSummary)('VLS', errorCount, warningCount));
                };
                const onDispatchDiagnostics = (normalized) => {
                    if (overlay && command === 'serve') {
                        worker_threads_1.parentPort === null || worker_threads_1.parentPort === void 0 ? void 0 : worker_threads_1.parentPort.postMessage({
                            type: types_1.ACTION_TYPES.overlayError,
                            payload: (0, logger_1.toViteCustomPayload)('vls', (0, logger_1.diagnosticToRuntimeError)(normalized)),
                        });
                    }
                    if (terminal) {
                        (0, logger_1.consoleLog)(normalized.map((d) => (0, logger_1.diagnosticToTerminalLog)(d, 'VLS')).join(os_1.default.EOL));
                    }
                };
                const vlsConfig = pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.vls;
                yield (0, diagnostics_1.diagnostics)(workDir, 'WARN', {
                    onDispatchDiagnostics,
                    onDispatchDiagnosticsSummary,
                    watch: true,
                    verbose: false,
                    config: typeof vlsConfig === 'object' ? vlsConfig : null,
                });
            });
        },
    };
};
exports.createDiagnostic = createDiagnostic;
class VlsChecker extends Checker_1.Checker {
    constructor() {
        super({
            name: 'vls',
            absFilePath: __filename,
            build: {
                buildBin: (config) => {
                    if (typeof config.vls === 'object') {
                        return [
                            'vite-plugin-checker-vls',
                            [
                                'diagnostics',
                                '--checker-config',
                                // Escape quotes so that the system shell doesn't strip them out:
                                '"' + JSON.stringify(config.vls).replace(/[\\"]/g, '\\$&') + '"',
                            ],
                        ];
                    }
                    return ['vite-plugin-checker-vls', ['diagnostics']];
                },
            },
            createDiagnostic: exports.createDiagnostic,
        });
    }
    init() {
        const createServeAndBuild = super.initMainThread();
        module.exports.createServeAndBuild = createServeAndBuild;
        super.initWorkerThread();
    }
}
exports.VlsChecker = VlsChecker;
const vlsChecker = new VlsChecker();
vlsChecker.prepare();
vlsChecker.init();
