"use strict";
/**
 * Core type definitions for DataPilot
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataPilotError = exports.ErrorCategory = exports.ErrorSeverity = exports.DataType = void 0;
// Data Types
var DataType;
(function (DataType) {
    DataType["STRING"] = "string";
    DataType["NUMBER"] = "number";
    DataType["INTEGER"] = "integer";
    DataType["FLOAT"] = "float";
    DataType["DATE"] = "date";
    DataType["DATETIME"] = "datetime";
    DataType["BOOLEAN"] = "boolean";
    DataType["UNKNOWN"] = "unknown";
})(DataType || (exports.DataType = DataType = {}));
// Error Types
var ErrorSeverity;
(function (ErrorSeverity) {
    ErrorSeverity["LOW"] = "low";
    ErrorSeverity["MEDIUM"] = "medium";
    ErrorSeverity["HIGH"] = "high";
    ErrorSeverity["CRITICAL"] = "critical";
})(ErrorSeverity || (exports.ErrorSeverity = ErrorSeverity = {}));
var ErrorCategory;
(function (ErrorCategory) {
    ErrorCategory["PARSING"] = "parsing";
    ErrorCategory["VALIDATION"] = "validation";
    ErrorCategory["ANALYSIS"] = "analysis";
    ErrorCategory["MEMORY"] = "memory";
    ErrorCategory["IO"] = "io";
    ErrorCategory["CONFIGURATION"] = "configuration";
    ErrorCategory["NETWORK"] = "network";
    ErrorCategory["PERMISSION"] = "permission";
    ErrorCategory["SECURITY"] = "security";
    ErrorCategory["PERFORMANCE"] = "performance";
})(ErrorCategory || (exports.ErrorCategory = ErrorCategory = {}));
class DataPilotError extends Error {
    code;
    severity;
    category;
    context;
    suggestions;
    recoverable;
    details;
    enhancedStack;
    preservedStack;
    timestamp;
    verboseInfo;
    constructor(message, code, severity = ErrorSeverity.MEDIUM, category = ErrorCategory.ANALYSIS, context, suggestions, recoverable = true, details) {
        super(message);
        this.code = code;
        this.severity = severity;
        this.category = category;
        this.context = context;
        this.suggestions = suggestions;
        this.recoverable = recoverable;
        this.details = details;
        this.name = 'DataPilotError';
        this.timestamp = Date.now();
        // Preserve original stack trace
        this.preservedStack = this.stack;
        // Capture enhanced stack trace with context
        this.captureEnhancedStack();
        // Add context stack if available
        if (this.context?.originalStack) {
            this.enhancedStack = this.context.originalStack;
        }
    }
    /**
     * Create a parsing error with appropriate context
     */
    static parsing(message, code, context, suggestions) {
        return new DataPilotError(message, code, ErrorSeverity.HIGH, ErrorCategory.PARSING, context, suggestions, true);
    }
    /**
     * Create a memory error with appropriate context
     */
    static memory(message, code, context, suggestions) {
        return new DataPilotError(message, code, ErrorSeverity.CRITICAL, ErrorCategory.MEMORY, context, suggestions, true);
    }
    /**
     * Create a validation error with appropriate context
     */
    static validation(message, code, context, suggestions) {
        return new DataPilotError(message, code, ErrorSeverity.HIGH, ErrorCategory.VALIDATION, context, suggestions, false);
    }
    /**
     * Create an analysis error with graceful degradation
     */
    static analysis(message, code, context, suggestions) {
        return new DataPilotError(message, code, ErrorSeverity.MEDIUM, ErrorCategory.ANALYSIS, context, suggestions, true);
    }
    /**
     * Create a security error with appropriate context
     */
    static security(message, code, context, suggestions) {
        return new DataPilotError(message, code, ErrorSeverity.HIGH, ErrorCategory.SECURITY, context, suggestions, false);
    }
    /**
     * Capture enhanced stack trace with context preservation
     */
    captureEnhancedStack() {
        const stackLines = (this.stack || '').split('\n');
        const enhancedLines = stackLines.map((line, index) => {
            if (index === 0)
                return line; // Error message line
            // Add context information to stack frames
            if (this.context?.callStack && this.context.callStack[index - 1]) {
                return `${line} [Context: ${this.context.callStack[index - 1]}]`;
            }
            return line;
        });
        this.enhancedStack = enhancedLines.join('\n');
    }
    /**
     * Set verbose information for debugging
     */
    setVerboseInfo(verboseMode = false) {
        if (!verboseMode)
            return;
        this.verboseInfo = {
            fullContext: {
                ...this.context,
                errorCode: this.code,
                errorCategory: this.category,
                errorSeverity: this.severity,
                timestamp: this.timestamp,
                recoverable: this.recoverable,
            },
            stackTrace: (this.enhancedStack || this.stack || '').split('\n'),
            performanceMetrics: {
                memoryUsage: this.context?.memoryUsage,
                timeElapsed: this.context?.timeElapsed,
                performanceContext: this.context?.performanceContext,
            },
            memorySnapshot: process.memoryUsage(),
        };
    }
    /**
     * Get formatted error message with enhanced context
     */
    getFormattedMessage(verboseMode = false) {
        let message = `❌ ERROR: ${this.message}`;
        if (this.context) {
            const contextParts = [];
            if (this.context.filePath)
                contextParts.push(`File: ${this.context.filePath}`);
            if (this.context.section)
                contextParts.push(`Section: ${this.context.section}`);
            if (this.context.analyzer)
                contextParts.push(`Analyzer: ${this.context.analyzer}`);
            if (this.context.operationName)
                contextParts.push(`Operation: ${this.context.operationName}`);
            if (this.context.rowIndex !== undefined)
                contextParts.push(`Row: ${this.context.rowIndex}`);
            if (this.context.columnName)
                contextParts.push(`Column: ${this.context.columnName}`);
            if (contextParts.length > 0) {
                message += `\n   ${contextParts.join('\n   ')}`;
            }
            // Add verbose context in verbose mode
            if (verboseMode) {
                message += this.getVerboseContext();
            }
        }
        return message;
    }
    /**
     * Get verbose context information for debugging
     */
    getVerboseContext() {
        if (!this.context)
            return '';
        let verbose = '';
        // Stack trace information
        if (this.enhancedStack) {
            verbose += `\n   Stack: ${this.enhancedStack.split('\n').slice(1, 4).join('\n          ')}`;
        }
        // Silent failure detection
        if (this.context.silentFailure?.detected) {
            verbose += `\n   Silent Failure: ${this.context.silentFailure.failureType}`;
            verbose += `\n   Expected: ${this.context.silentFailure.expectedResult}`;
            verbose += `\n   Actual: ${JSON.stringify(this.context.silentFailure.actualResult)}`;
        }
        // Performance context
        if (this.context.performanceContext) {
            const perf = this.context.performanceContext;
            const duration = perf.endTime ? perf.endTime - perf.startTime : 'ongoing';
            verbose += `\n   Performance: ${duration}ms`;
            if (perf.memoryBefore && perf.memoryAfter) {
                const memDiff = perf.memoryAfter.heapUsed - perf.memoryBefore.heapUsed;
                verbose += `, Memory Δ: ${(memDiff / 1024 / 1024).toFixed(2)}MB`;
            }
        }
        // Dependency context
        if (this.context.dependencyContext && Object.keys(this.context.dependencyContext).length > 0) {
            verbose += `\n   Dependencies: ${Object.keys(this.context.dependencyContext).join(', ')}`;
        }
        // Additional context
        if (this.context.additionalContext && Object.keys(this.context.additionalContext).length > 0) {
            verbose += `\n   Context: ${JSON.stringify(this.context.additionalContext, null, 2).replace(/\n/g, '\n           ')}`;
        }
        return verbose;
    }
    /**
     * Get enhanced suggestions with debugging hints
     */
    getEnhancedSuggestions(verboseMode = false) {
        const suggestions = this.getSuggestions();
        if (!verboseMode || !this.context) {
            return suggestions;
        }
        // Add debugging-specific suggestions
        const debugSuggestions = [];
        if (this.context.silentFailure?.detected) {
            debugSuggestions.push(`• Debug Silent Failure: Check for ${this.context.silentFailure.failureType} in ${this.context.analyzer || 'analyzer'}`);
        }
        if (this.context.performanceContext?.memoryAfter?.heapUsed > 500 * 1024 * 1024) {
            debugSuggestions.push('• Memory Issue: Consider processing data in smaller chunks (--maxRows 5000)');
        }
        if (this.context.dependencyContext) {
            const missingDeps = Object.entries(this.context.dependencyContext)
                .filter(([_, value]) => !value)
                .map(([key]) => key);
            if (missingDeps.length > 0) {
                debugSuggestions.push(`• Missing Dependencies: Ensure ${missingDeps.join(', ')} completed successfully`);
            }
        }
        return [...suggestions, ...debugSuggestions];
    }
    /**
     * Get actionable suggestions as formatted text
     */
    getSuggestions() {
        if (!this.suggestions || this.suggestions.length === 0) {
            return [];
        }
        return this.suggestions.map((suggestion) => {
            let text = `• ${suggestion.action}: ${suggestion.description}`;
            if (suggestion.command) {
                text += ` (Run: ${suggestion.command})`;
            }
            return text;
        });
    }
}
exports.DataPilotError = DataPilotError;
//# sourceMappingURL=types.js.map