// Copyright 2024 The Lynx Authors. All rights reserved.
// Licensed under the Apache License Version 2.0 that can be found in the
// LICENSE file in the root directory of this source tree.
import { options } from 'preact';
import { DIFFED, DOM } from '../../shared/render-constants.js';
const snapshotVNodeSourceMap = new Map();
let hookInstalled = false;
function formatSource(source) {
    if (!source.fileName) {
        return undefined;
    }
    if (typeof source.lineNumber === 'number' && typeof source.columnNumber === 'number') {
        return `${source.fileName}:${source.lineNumber}:${source.columnNumber}`;
    }
    if (typeof source.lineNumber === 'number') {
        return `${source.fileName}:${source.lineNumber}`;
    }
    return source.fileName;
}
function captureVNodeSource(vnode) {
    if (typeof vnode.type !== 'string') {
        return;
    }
    const source = vnode.__source;
    const id = vnode[DOM]?.__id;
    if (!source || typeof id !== 'number') {
        return;
    }
    const formattedSource = formatSource(source);
    if (formattedSource) {
        snapshotVNodeSourceMap.set(id, formattedSource);
    }
}
export function setupVNodeSourceHook() {
    if (hookInstalled) {
        return;
    }
    hookInstalled = true;
    const oldDiffed = options[DIFFED];
    options[DIFFED] = (vnode) => {
        captureVNodeSource(vnode);
        oldDiffed?.(vnode);
    };
}
export function moveSnapshotVNodeSource(oldId, newId) {
    if (oldId === newId) {
        return;
    }
    const source = snapshotVNodeSourceMap.get(oldId);
    if (source) {
        snapshotVNodeSourceMap.set(newId, source);
        snapshotVNodeSourceMap.delete(oldId);
    }
}
export function getSnapshotVNodeSource(id) {
    return snapshotVNodeSourceMap.get(id);
}
export function clearSnapshotVNodeSource() {
    snapshotVNodeSourceMap.clear();
}
//# sourceMappingURL=vnodeSource.js.map