/**
 * SpecWeave Error Hierarchy
 *
 * Centralized error types for the SpecWeave framework.
 * All custom errors extend SpecWeaveError for consistent error handling.
 *
 * @module core/errors
 */
/**
 * Error codes for categorization
 */
export var ErrorCode;
(function (ErrorCode) {
    // Configuration errors (1xxx)
    ErrorCode[ErrorCode["CONFIG_NOT_FOUND"] = 1001] = "CONFIG_NOT_FOUND";
    ErrorCode[ErrorCode["CONFIG_INVALID"] = 1002] = "CONFIG_INVALID";
    ErrorCode[ErrorCode["CONFIG_PARSE_ERROR"] = 1003] = "CONFIG_PARSE_ERROR";
    // Sync errors (2xxx)
    ErrorCode[ErrorCode["SYNC_FAILED"] = 2001] = "SYNC_FAILED";
    ErrorCode[ErrorCode["SYNC_CONFLICT"] = 2002] = "SYNC_CONFLICT";
    ErrorCode[ErrorCode["SYNC_RATE_LIMITED"] = 2003] = "SYNC_RATE_LIMITED";
    ErrorCode[ErrorCode["SYNC_PERMISSION_DENIED"] = 2004] = "SYNC_PERMISSION_DENIED";
    ErrorCode[ErrorCode["SYNC_NETWORK_ERROR"] = 2005] = "SYNC_NETWORK_ERROR";
    // Import errors (3xxx)
    ErrorCode[ErrorCode["IMPORT_FAILED"] = 3001] = "IMPORT_FAILED";
    ErrorCode[ErrorCode["IMPORT_PARSE_ERROR"] = 3002] = "IMPORT_PARSE_ERROR";
    ErrorCode[ErrorCode["IMPORT_INVALID_FORMAT"] = 3003] = "IMPORT_INVALID_FORMAT";
    ErrorCode[ErrorCode["IMPORT_DUPLICATE"] = 3004] = "IMPORT_DUPLICATE";
    // Validation errors (4xxx)
    ErrorCode[ErrorCode["VALIDATION_FAILED"] = 4001] = "VALIDATION_FAILED";
    ErrorCode[ErrorCode["VALIDATION_SCHEMA_ERROR"] = 4002] = "VALIDATION_SCHEMA_ERROR";
    ErrorCode[ErrorCode["VALIDATION_REQUIRED_FIELD"] = 4003] = "VALIDATION_REQUIRED_FIELD";
    // Increment errors (5xxx)
    ErrorCode[ErrorCode["INCREMENT_NOT_FOUND"] = 5001] = "INCREMENT_NOT_FOUND";
    ErrorCode[ErrorCode["INCREMENT_INVALID_STATUS"] = 5002] = "INCREMENT_INVALID_STATUS";
    ErrorCode[ErrorCode["INCREMENT_LOCKED"] = 5003] = "INCREMENT_LOCKED";
    ErrorCode[ErrorCode["INCREMENT_METADATA_ERROR"] = 5004] = "INCREMENT_METADATA_ERROR";
    // Plugin errors (6xxx)
    ErrorCode[ErrorCode["PLUGIN_NOT_FOUND"] = 6001] = "PLUGIN_NOT_FOUND";
    ErrorCode[ErrorCode["PLUGIN_LOAD_ERROR"] = 6002] = "PLUGIN_LOAD_ERROR";
    ErrorCode[ErrorCode["PLUGIN_EXECUTION_ERROR"] = 6003] = "PLUGIN_EXECUTION_ERROR";
    // Integration errors (7xxx)
    ErrorCode[ErrorCode["INTEGRATION_AUTH_ERROR"] = 7001] = "INTEGRATION_AUTH_ERROR";
    ErrorCode[ErrorCode["INTEGRATION_API_ERROR"] = 7002] = "INTEGRATION_API_ERROR";
    ErrorCode[ErrorCode["INTEGRATION_TIMEOUT"] = 7003] = "INTEGRATION_TIMEOUT";
    // File system errors (8xxx)
    ErrorCode[ErrorCode["FS_READ_ERROR"] = 8001] = "FS_READ_ERROR";
    ErrorCode[ErrorCode["FS_WRITE_ERROR"] = 8002] = "FS_WRITE_ERROR";
    ErrorCode[ErrorCode["FS_NOT_FOUND"] = 8003] = "FS_NOT_FOUND";
    ErrorCode[ErrorCode["FS_PERMISSION_DENIED"] = 8004] = "FS_PERMISSION_DENIED";
    // Unknown
    ErrorCode[ErrorCode["UNKNOWN"] = 9999] = "UNKNOWN";
})(ErrorCode || (ErrorCode = {}));
/**
 * Base error for all SpecWeave errors
 *
 * @example
 * ```typescript
 * throw new SpecWeaveError('Something went wrong', {
 *   code: ErrorCode.UNKNOWN,
 *   context: { incrementId: '0001' }
 * });
 * ```
 */
export class SpecWeaveError extends Error {
    constructor(message, options = {}) {
        super(message);
        this.name = 'SpecWeaveError';
        this.code = options.code ?? ErrorCode.UNKNOWN;
        this.context = options.context;
        this.timestamp = new Date();
        this.originalCause = options.cause;
        // Maintain proper stack trace for where error was thrown
        if (Error.captureStackTrace) {
            Error.captureStackTrace(this, this.constructor);
        }
    }
    /**
     * Format error for logging
     */
    toLogString() {
        const parts = [`[${this.name}] ${this.message}`];
        parts.push(`Code: ${this.code}`);
        if (this.context) {
            parts.push(`Context: ${JSON.stringify(this.context)}`);
        }
        if (this.originalCause) {
            parts.push(`Caused by: ${this.originalCause.message}`);
        }
        return parts.join(' | ');
    }
    /**
     * Serialize error for API responses
     */
    toJSON() {
        return {
            name: this.name,
            message: this.message,
            code: this.code,
            context: this.context,
            timestamp: this.timestamp.toISOString(),
        };
    }
}
/**
 * Validation-related errors
 */
export class ValidationError extends SpecWeaveError {
    constructor(message, options = {}) {
        super(message, {
            code: options.code ?? ErrorCode.VALIDATION_FAILED,
            context: options.context,
            cause: options.cause,
        });
        this.name = 'ValidationError';
        this.field = options.field;
    }
}
/**
 * Type guard to check if an error is a SpecWeaveError
 */
export function isSpecWeaveError(error) {
    return error instanceof SpecWeaveError;
}
/**
 * Wrap an unknown error in a SpecWeaveError
 */
export function wrapError(error, message, code) {
    if (error instanceof SpecWeaveError) {
        return error;
    }
    const cause = error instanceof Error ? error : new Error(String(error));
    return new SpecWeaveError(message ?? cause.message, {
        code: code ?? ErrorCode.UNKNOWN,
        cause,
    });
}
//# sourceMappingURL=index.js.map