"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.createHookContext = createHookContext;
exports.deriveHookContext = deriveHookContext;
exports.mergeContextMetadata = mergeContextMetadata;
// =============================================================================
// Hook Context Factory
// =============================================================================
/**
 * 创建钩子执行上下文
 */
function createHookContext(sessionId, traceId, phase) {
    const metadata = new Map();
    let aborted = false;
    let abortReason;
    const context = {
        sessionId,
        traceId,
        phase,
        get aborted() {
            return aborted;
        },
        get abortReason() {
            return abortReason;
        },
        metadata,
        abort(reason) {
            aborted = true;
            abortReason = reason;
        },
        set(key, value) {
            metadata.set(key, value);
        },
        get(key) {
            return metadata.get(key);
        },
    };
    return context;
}
// =============================================================================
// Hook Context Utilities
// =============================================================================
/**
 * 从现有上下文创建子上下文 (继承元数据)
 */
function deriveHookContext(parent, overrides) {
    const child = createHookContext(overrides?.sessionId ?? parent.sessionId, overrides?.traceId ?? parent.traceId, overrides?.phase ?? parent.phase);
    // 复制父上下文的元数据
    for (const [key, value] of parent.metadata) {
        child.metadata.set(key, value);
    }
    return child;
}
/**
 * 合并多个上下文的元数据
 */
function mergeContextMetadata(target, ...sources) {
    for (const source of sources) {
        for (const [key, value] of source.metadata) {
            target.metadata.set(key, value);
        }
    }
}
//# sourceMappingURL=hookContext.js.map