/**
 * Analytics event writer core module.
 *
 * Extracted from hooks/handlers/post-tool-use-analytics.ts.
 * Appends analytics events to events.jsonl.
 *
 * Never throws.
 *
 * @module core/analytics/event-writer
 */
import * as fs from 'fs';
import * as path from 'path';
import { maskCredentialsInData } from '../../utils/credential-masker.js';
/**
 * Extract plugin name from a skill name.
 * "sw:pm" → "specweave", "frontend-design:component" → "frontend-design", "my-skill" → "specweave"
 */
export function extractPlugin(skillName) {
    if (!skillName.includes(':'))
        return 'specweave';
    const prefix = skillName.split(':')[0];
    return prefix === 'sw' ? 'specweave' : prefix;
}
/**
 * Append an analytics event to events.jsonl.
 * Creates the analytics directory if it does not exist.
 */
export function appendAnalyticsEvent(analyticsDir, event) {
    try {
        const safeEvent = maskCredentialsInData(event);
        fs.mkdirSync(analyticsDir, { recursive: true });
        fs.appendFileSync(path.join(analyticsDir, 'events.jsonl'), JSON.stringify(safeEvent) + '\n');
        return { written: 1 };
    }
    catch {
        return { written: 0 };
    }
}
/**
 * Build and append a skill analytics event.
 */
export function writeSkillEvent(analyticsDir, name, plugin, timestamp) {
    const event = {
        timestamp: timestamp ?? new Date().toISOString(),
        type: 'skill',
        name,
        success: true,
        plugin: plugin ?? extractPlugin(name),
    };
    return appendAnalyticsEvent(analyticsDir, event);
}
/**
 * Build and append an agent analytics event.
 */
export function writeAgentEvent(analyticsDir, name, timestamp) {
    const event = {
        timestamp: timestamp ?? new Date().toISOString(),
        type: 'agent',
        name,
        success: true,
        plugin: 'specweave',
    };
    return appendAnalyticsEvent(analyticsDir, event);
}
//# sourceMappingURL=event-writer.js.map