import { projectWorkbookSemanticSnapshot } from './workbook-semantic-projection.js';
export function stableStringifyWorkbookSemanticSnapshot(snapshot) {
    return JSON.stringify(snapshot);
}
export function workbookSemanticSnapshotsEqual(left, right) {
    return (stableStringifyWorkbookSemanticSnapshot(projectWorkbookSemanticSnapshot(left)) ===
        stableStringifyWorkbookSemanticSnapshot(projectWorkbookSemanticSnapshot(right)));
}
export function diffWorkbookSemanticSnapshots(left, right) {
    return diffSemanticValues(projectWorkbookSemanticSnapshot(left), projectWorkbookSemanticSnapshot(right));
}
function diffSemanticValues(left, right, path = []) {
    if (Object.is(left, right)) {
        return [];
    }
    if (Array.isArray(left) || Array.isArray(right)) {
        if (!Array.isArray(left) || !Array.isArray(right)) {
            return [semanticDiff(path, left, right)];
        }
        const length = Math.max(left.length, right.length);
        const diffs = [];
        for (let index = 0; index < length; index += 1) {
            diffs.push(...diffSemanticValues(left[index], right[index], [...path, String(index)]));
        }
        return diffs;
    }
    if (isRecord(left) || isRecord(right)) {
        if (!isRecord(left) || !isRecord(right)) {
            return [semanticDiff(path, left, right)];
        }
        const keys = [...new Set([...Object.keys(left), ...Object.keys(right)])].toSorted();
        return keys.flatMap((key) => diffSemanticValues(left[key], right[key], [...path, key]));
    }
    return [semanticDiff(path, left, right)];
}
function semanticDiff(path, left, right) {
    return {
        path: path.length === 0 ? '$' : `$.${path.join('.')}`,
        left,
        right,
    };
}
function isRecord(value) {
    return typeof value === 'object' && value !== null && !Array.isArray(value);
}
//# sourceMappingURL=workbook-semantic-comparison.js.map