function compareDocumentOrder(a, b) {
    if (a === b)
        return 0;
    const position = a.compareDocumentPosition(b);
    if (position & Node.DOCUMENT_POSITION_PRECEDING)
        return 1;
    if (position & Node.DOCUMENT_POSITION_FOLLOWING)
        return -1;
    return 0;
}
export class RuntimeRegistry {
    shellsByElement = new Map();
    register(detail) {
        const existing = this.shellsByElement.get(detail.element);
        const nextRecord = {
            ...detail,
            sortKey: Date.now(),
        };
        // Idempotent no-op for identical registrations.
        if (existing &&
            existing.kind === nextRecord.kind &&
            existing.itemId === nextRecord.itemId &&
            existing.canonicalItemId === nextRecord.canonicalItemId) {
            return false;
        }
        this.shellsByElement.set(detail.element, nextRecord);
        return true;
    }
    unregister(element) {
        return this.shellsByElement.delete(element);
    }
    getOrderedShells(kind) {
        const records = Array.from(this.shellsByElement.values()).filter((entry) => !kind || entry.kind === kind);
        records.sort((a, b) => compareDocumentOrder(a.element, b.element));
        return records;
    }
    getCanonicalIdMap() {
        const map = {};
        for (const entry of this.getOrderedShells("item")) {
            if (!entry.itemId)
                continue;
            map[entry.itemId] = entry.canonicalItemId || entry.itemId;
        }
        return map;
    }
    clear() {
        this.shellsByElement.clear();
    }
}
//# sourceMappingURL=RuntimeRegistry.js.map