"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const worker_threads_1 = require("worker_threads");
const taskdoc_search_1 = require("./taskdoc-search");
function isRecord(value) {
    return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function parseWorkerPayload(value) {
    if (!isRecord(value)) {
        throw new Error('Taskdoc suggestion worker payload must be an object');
    }
    const query = value['query'];
    if (typeof query !== 'string') {
        throw new Error('Taskdoc suggestion worker payload.query must be a string');
    }
    const rootDirRaw = value['rootDir'];
    const limitRaw = value['limit'];
    return {
        query,
        ...(typeof rootDirRaw === 'string' ? { rootDir: rootDirRaw } : {}),
        ...(typeof limitRaw === 'number' ? { limit: limitRaw } : {}),
    };
}
async function main() {
    if (worker_threads_1.parentPort === null) {
        throw new Error('Taskdoc suggestion worker requires a parentPort');
    }
    try {
        const payload = parseWorkerPayload(worker_threads_1.workerData);
        const suggestions = await (0, taskdoc_search_1.searchTaskDocumentSuggestionsInRtws)(payload);
        const message = { kind: 'ok', suggestions };
        worker_threads_1.parentPort.postMessage(message);
    }
    catch (error) {
        const message = {
            kind: 'error',
            errorText: error instanceof Error ? error.message : 'Taskdoc suggestion worker failed',
        };
        worker_threads_1.parentPort.postMessage(message);
    }
}
void main();
