// engine/hooks/triggers.ts — Event matching logic and payload construction (SPEC-071)
// ---------------------------------------------------------------------------
// Payload constructors
// ---------------------------------------------------------------------------
/**
 * Build the payload for on_spec_created events.
 */
export function buildSpecCreatedPayload(projectId, specId, specTitle, specType) {
    return {
        trigger: 'on_spec_created',
        projectId,
        specId,
        specTitle,
        specType,
        timestamp: new Date().toISOString(),
    };
}
/**
 * Build the payload for on_spec_status_change events.
 */
export function buildStatusChangePayload(projectId, specId, specTitle, fromStatus, toStatus) {
    return {
        trigger: 'on_spec_status_change',
        projectId,
        specId,
        specTitle,
        fromStatus,
        toStatus,
        timestamp: new Date().toISOString(),
    };
}
/**
 * Build the payload for on_drift_detected events.
 */
export function buildDriftDetectedPayload(projectId, specId, driftScore, driftSummary) {
    return {
        trigger: 'on_drift_detected',
        projectId,
        specId,
        driftScore,
        driftSummary,
        timestamp: new Date().toISOString(),
    };
}
/**
 * Build the payload for on_commit events.
 */
export function buildCommitPayload(projectId, commitHash, commitMessage, branch) {
    return {
        trigger: 'on_commit',
        projectId,
        commitHash,
        commitMessage,
        branch,
        timestamp: new Date().toISOString(),
    };
}
// ---------------------------------------------------------------------------
// Filter matching
// ---------------------------------------------------------------------------
/**
 * Check whether a hook's filter conditions are satisfied by the given payload.
 * Returns true when the hook should fire (no filter = always fires).
 */
export function matchesFilter(hook, payload) {
    const filter = hook.filter;
    if (!filter) {
        return true;
    }
    if (payload.trigger === 'on_spec_status_change') {
        if (filter.fromStatus !== undefined && filter.fromStatus !== payload.fromStatus) {
            return false;
        }
        if (filter.toStatus !== undefined && filter.toStatus !== payload.toStatus) {
            return false;
        }
    }
    if (filter.specIdPattern !== undefined) {
        const specId = getSpecIdFromPayload(payload);
        if (specId !== null && !matchesGlob(specId, filter.specIdPattern)) {
            return false;
        }
    }
    return true;
}
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function getSpecIdFromPayload(payload) {
    if (payload.trigger === 'on_spec_created' ||
        payload.trigger === 'on_spec_status_change' ||
        payload.trigger === 'on_drift_detected') {
        return payload.specId;
    }
    return null;
}
/**
 * Simple glob matching: supports * wildcard (matches any character sequence).
 */
export function matchesGlob(value, pattern) {
    const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*');
    return new RegExp(`^${escaped}$`).test(value);
}
//# sourceMappingURL=triggers.js.map