/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
import { i as initWasm, l as linesDiffToWire, D as DefaultLinesDiffComputer, g as getWasm, R as RangeMapping, a as Range, b as DetailedLineRangeMapping, L as LineRange, c as LinesDiff } from './wire.js';

/// <reference lib="WebWorker" />
/**
 * Diff worker entry. Built as a separate rollup output (`dist/worker.js`).
 *
 * The worker is intentionally narrow: it consumes a `ComputeRequest`,
 * produces a `LinesDiff`, converts it to the wire shape, and posts it back.
 * All `LinesDiff -> DiffResult` adapter work happens on the main thread,
 * which already has the input strings.
 */
const tsImpl = new DefaultLinesDiffComputer();
self.addEventListener('message', (ev) => {
    const msg = ev.data;
    if (msg.kind === 'init') {
        void handleInit(msg);
    }
    else if (msg.kind === 'computeDiff') {
        void handleCompute(msg);
    }
});
async function handleInit(req) {
    try {
        if (req.backend === 'wasm') {
            await initWasm();
        }
        const resp = { kind: 'init', id: req.id, ok: true };
        self.postMessage(resp);
    }
    catch (err) {
        const resp = {
            kind: 'computeDiff',
            id: req.id,
            ok: false,
            error: String(err instanceof Error ? err.message : err),
        };
        self.postMessage(resp);
    }
}
async function handleCompute(req) {
    try {
        let linesDiff;
        if (req.backend === 'ts') {
            linesDiff = computeTs(req.originalLines, req.modifiedLines, req.options);
        }
        else {
            await initWasm();
            linesDiff = computeWasm(req.originalLines, req.modifiedLines, req.options);
        }
        const result = linesDiffToWire(linesDiff);
        const resp = { kind: 'computeDiff', id: req.id, ok: true, result };
        self.postMessage(resp);
    }
    catch (err) {
        const resp = {
            kind: 'computeDiff',
            id: req.id,
            ok: false,
            error: String(err instanceof Error ? err.message : err),
        };
        self.postMessage(resp);
    }
}
function computeTs(original, modified, options) {
    const internal = toInternalOptions(options);
    return tsImpl.computeDiff(original, modified, internal);
}
function computeWasm(original, modified, options) {
    const wasm = getWasm();
    const wasmOptions = toWasmOptions(options);
    const wasmResult = wasm.computeDiffLines(original, modified, wasmOptions);
    return wasmLinesDiffToLinesDiff(wasmResult);
}
function wasmLinesDiffToLinesDiff(wasmResult) {
    const changes = wasmResult.changes.map((c) => {
        const inner = c.innerChanges?.map((ic) => new RangeMapping(new Range(ic.originalStartLine, ic.originalStartColumn, ic.originalEndLine, ic.originalEndColumn), new Range(ic.modifiedStartLine, ic.modifiedStartColumn, ic.modifiedEndLine, ic.modifiedEndColumn)));
        return new DetailedLineRangeMapping(new LineRange(c.original.startLineNumber, c.original.endLineNumberExclusive), new LineRange(c.modified.startLineNumber, c.modified.endLineNumberExclusive), inner);
    });
    return new LinesDiff(changes, [], wasmResult.hitTimeout);
}
function toInternalOptions(options) {
    return {
        maxComputationTimeMs: options?.maxComputationTimeMs ?? 0,
        ignoreTrimWhitespace: options?.ignoreTrimWhitespace ?? false,
        computeMoves: options?.computeMoves ?? false,
        extendToSubwords: options?.extendToSubwords ?? false,
    };
}
function toWasmOptions(options) {
    const out = {};
    if (options?.maxComputationTimeMs !== undefined)
        out.maxComputationTimeMs = options.maxComputationTimeMs;
    if (options?.ignoreTrimWhitespace !== undefined)
        out.ignoreTrimWhitespace = options.ignoreTrimWhitespace;
    if (options?.computeMoves !== undefined)
        out.computeMoves = options.computeMoves;
    if (options?.extendToSubwords !== undefined)
        out.extendToSubwords = options.extendToSubwords;
    return out;
}//# sourceMappingURL=https://github.com/VSCodium/sourcemaps/releases/download/stable-4c0b0c6cc561d2d3636d1ec250935431876ce4dc/node_modules-@vscode-diff-dist-worker.js.map
