import { buildWaferMap } from '../renderer/buildWaferMap.js';
import { analyzeWaferMap, analyzeWaferLot } from '../stats/index.js';
self.onmessage = (ev) => {
    const msg = ev.data;
    if (msg.type === 'ping') {
        self.postMessage({ type: 'pong' });
        return;
    }
    if (msg.type === 'run') {
        try {
            const result = buildWaferMap(msg.input);
            self.postMessage({ type: 'result', id: msg.id, result });
        }
        catch (err) {
            const message = err instanceof Error ? err.message : String(err);
            self.postMessage({ type: 'error', id: msg.id, message });
        }
    }
    if (msg.type === 'analyze') {
        try {
            const waferSummaries = msg.results.map(r => analyzeWaferMap(r, msg.options));
            const lotSummary = msg.hasMultiWafer
                ? analyzeWaferLot(msg.results, { ...msg.options, perWaferSummaries: waferSummaries })
                : null;
            self.postMessage({ type: 'analyzed', id: msg.id, waferSummaries, lotSummary });
        }
        catch (err) {
            const message = err instanceof Error ? err.message : String(err);
            self.postMessage({ type: 'error', id: msg.id, message });
        }
    }
    if (msg.type === 'runWithAnalysis') {
        try {
            const results = msg.inputs.map(input => buildWaferMap(input));
            const waferSummaries = results.map(r => analyzeWaferMap(r, msg.options));
            const lotSummary = msg.hasMultiWafer
                ? analyzeWaferLot(results, { ...msg.options, perWaferSummaries: waferSummaries })
                : null;
            self.postMessage({ type: 'resultWithAnalysis', id: msg.id, results, waferSummaries, lotSummary });
        }
        catch (err) {
            const message = err instanceof Error ? err.message : String(err);
            self.postMessage({ type: 'error', id: msg.id, message });
        }
    }
};
//# sourceMappingURL=wafermap.worker.js.map