import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import { ExecutionValidator } from '@principal-ai/principal-view-core';
/**
 * Load a workflow template from a file
 */
export async function loadWorkflow(filePath) {
    try {
        const content = await readFile(filePath, 'utf-8');
        return JSON.parse(content);
    }
    catch (error) {
        if (error.code === 'ENOENT') {
            throw new Error(`Workflow file not found: ${filePath}`);
        }
        throw new Error(`Failed to parse workflow file: ${error.message}`);
    }
}
/**
 * Load execution data from a .otel.json file
 *
 * Automatically handles OTLP standard format conversion via ExecutionValidator.
 */
export async function loadExecution(filePath) {
    try {
        const content = await readFile(filePath, 'utf-8');
        const parsed = JSON.parse(content);
        // Use ExecutionValidator to handle OTLP format conversion
        const validator = new ExecutionValidator();
        return validator.validateOrThrow(parsed, filePath);
    }
    catch (error) {
        if (error.code === 'ENOENT') {
            throw new Error(`Execution file not found: ${filePath}`);
        }
        throw new Error(`Failed to parse execution file: ${error.message}`);
    }
}
/**
 * Convert execution data to OtelEvent array format expected by workflow APIs
 */
export function executionToEvents(execution) {
    const events = [];
    for (const span of execution.spans) {
        for (const event of span.events) {
            events.push({
                name: event.name,
                timestamp: event.time,
                type: 'log',
                attributes: {
                    ...event.attributes,
                    'span.id': span.id,
                    'span.name': span.name,
                },
            });
        }
    }
    return events;
}
/**
 * Resolve a file path relative to a base directory
 */
export function resolvePath(filePath, baseDir) {
    if (baseDir) {
        return resolve(baseDir, filePath);
    }
    return resolve(filePath);
}
/**
 * Format a timestamp as human-readable date/time
 */
export function formatTimestamp(timestamp) {
    const date = new Date(timestamp);
    return date.toLocaleString('en-US', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false,
    });
}
/**
 * Format duration in milliseconds
 */
export function formatDuration(ms) {
    if (ms < 1000) {
        return `${ms}ms`;
    }
    const seconds = (ms / 1000).toFixed(1);
    return `${seconds}s`;
}
/**
 * Group attributes by prefix (e.g., 'auth.method' -> 'auth')
 */
export function groupAttributesByPrefix(attributes) {
    const grouped = {};
    for (const [key, value] of Object.entries(attributes)) {
        const parts = key.split('.');
        if (parts.length > 1) {
            const prefix = parts[0];
            if (!grouped[prefix]) {
                grouped[prefix] = {};
            }
            grouped[prefix][key] = value;
        }
        else {
            if (!grouped['Other']) {
                grouped['Other'] = {};
            }
            grouped['Other'][key] = value;
        }
    }
    return grouped;
}
/**
 * Filter attributes by pattern (supports glob-like patterns)
 */
export function filterAttributes(attributes, pattern) {
    const regex = new RegExp('^' + pattern.replace(/\./g, '\\.').replace(/\*/g, '.*') + '$');
    const filtered = {};
    for (const [key, value] of Object.entries(attributes)) {
        if (regex.test(key)) {
            filtered[key] = value;
        }
    }
    return filtered;
}
/**
 * Count events by type/name
 */
export function countEventsByType(events) {
    const counts = new Map();
    for (const event of events) {
        const count = counts.get(event.name) || 0;
        counts.set(event.name, count + 1);
    }
    return counts;
}
/**
 * Format attribute value for display
 */
export function formatValue(value) {
    if (typeof value === 'string') {
        return `"${value}"`;
    }
    if (typeof value === 'number' || typeof value === 'boolean') {
        return String(value);
    }
    if (value === null || value === undefined) {
        return 'null';
    }
    if (typeof value === 'object') {
        return JSON.stringify(value);
    }
    return String(value);
}
/**
 * Capitalize first letter of a string
 */
export function capitalize(str) {
    return str.charAt(0).toUpperCase() + str.slice(1);
}
