function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
import { ErrorCode, getUserFriendlyMessage } from "./errors/ErrorCodes";
// 导出错误码相关类型和函数
export { ErrorCode, ErrorMessages, getErrorMessage, getUserFriendlyMessage } from "./errors/ErrorCodes";
/**
 * 错误类型枚举
 */ export var ErrorType = /*#__PURE__*/ function(ErrorType) {
    /**
   * 初始化错误
   */ ErrorType["INITIALIZATION"] = "INITIALIZATION";
    /**
   * 配置错误
   */ ErrorType["CONFIG"] = "CONFIG";
    /**
   * 存储错误
   */ ErrorType["STORAGE"] = "STORAGE";
    /**
   * 网络错误
   */ ErrorType["NETWORK"] = "NETWORK";
    /**
   * 验证错误
   */ ErrorType["VALIDATION"] = "VALIDATION";
    /**
   * 安全错误
   */ ErrorType["SECURITY"] = "SECURITY";
    /**
   * 运行时错误
   */ ErrorType["RUNTIME"] = "RUNTIME";
    /**
   * 未知错误
   */ ErrorType["UNKNOWN"] = "UNKNOWN";
    return ErrorType;
}({});
/**
 * 错误严重程度
 */ export var ErrorSeverity = /*#__PURE__*/ function(ErrorSeverity) {
    /**
   * 低 - 不影响核心功能
   */ ErrorSeverity["LOW"] = "LOW";
    /**
   * 中 - 影响部分功能
   */ ErrorSeverity["MEDIUM"] = "MEDIUM";
    /**
   * 高 - 影响核心功能
   */ ErrorSeverity["HIGH"] = "HIGH";
    /**
   * 严重 - 导致应用无法使用
   */ ErrorSeverity["CRITICAL"] = "CRITICAL";
    return ErrorSeverity;
}({});
/**
 * 框架错误基类
 */ export class FrameworkError extends Error {
    /**
   * 转换为 JSON
   */ toJSON() {
        return {
            name: this.name,
            message: this.message,
            type: this.type,
            severity: this.severity,
            code: this.code,
            recoverable: this.recoverable,
            context: this.context,
            stack: this.stack,
            originalError: this.originalError ? {
                name: this.originalError.name,
                message: this.originalError.message,
                stack: this.originalError.stack
            } : undefined
        };
    }
    constructor(message, type = "UNKNOWN", severity = "MEDIUM", options){
        super(message), /**
   * 错误类型
   */ _define_property(this, "type", void 0), /**
   * 错误严重程度
   */ _define_property(this, "severity", void 0), /**
   * 错误代码
   */ _define_property(this, "code", void 0), /**
   * 原始错误
   */ _define_property(this, "originalError", void 0), /**
   * 错误上下文
   */ _define_property(this, "context", void 0), /**
   * 是否可恢复
   */ _define_property(this, "recoverable", void 0);
        this.name = 'FrameworkError';
        this.type = type;
        this.severity = severity;
        this.code = options?.code || `${type}_ERROR`;
        this.originalError = options?.originalError;
        this.context = options?.context;
        this.recoverable = options?.recoverable ?? false;
        // 保持正确的堆栈跟踪
        if (Error.captureStackTrace) {
            Error.captureStackTrace(this, FrameworkError);
        }
    }
}
/**
 * 初始化错误
 */ export class InitializationError extends FrameworkError {
    constructor(message, originalError, context, errorCode){
        const code = errorCode || ErrorCode.INITIALIZATION_FAILED;
        super(getUserFriendlyMessage(code, message), "INITIALIZATION", "CRITICAL", {
            code: ErrorCode[code],
            originalError,
            context,
            recoverable: false
        });
        this.name = 'InitializationError';
    }
}
/**
 * 配置错误
 */ export class ConfigError extends FrameworkError {
    constructor(message, originalError, context, errorCode){
        const code = errorCode || ErrorCode.CONFIG_LOAD_FAILED;
        super(getUserFriendlyMessage(code, message), "CONFIG", "HIGH", {
            code: ErrorCode[code],
            originalError,
            context,
            recoverable: true
        });
        this.name = 'ConfigError';
    }
}
/**
 * 存储错误
 */ export class StorageError extends FrameworkError {
    constructor(message, originalError, context){
        super(message, "STORAGE", "MEDIUM", {
            code: 'STORAGE_ERROR',
            originalError,
            context,
            recoverable: true
        });
        this.name = 'StorageError';
    }
}
/**
 * 网络错误
 */ export class NetworkError extends FrameworkError {
    constructor(message, originalError, context, errorCode){
        const code = errorCode || ErrorCode.NETWORK_ERROR;
        super(getUserFriendlyMessage(code, message), "NETWORK", "MEDIUM", {
            code: ErrorCode[code],
            originalError,
            context,
            recoverable: true
        });
        this.name = 'NetworkError';
    }
}
/**
 * 验证错误
 */ export class ValidationError extends FrameworkError {
    constructor(message, context){
        super(message, "VALIDATION", "LOW", {
            code: 'VALIDATION_ERROR',
            context,
            recoverable: true
        });
        this.name = 'ValidationError';
    }
}
/**
 * 安全错误
 */ export class SecurityError extends FrameworkError {
    constructor(message, originalError, context){
        super(message, "SECURITY", "HIGH", {
            code: 'SECURITY_ERROR',
            originalError,
            context,
            recoverable: false
        });
        this.name = 'SecurityError';
    }
}
/**
 * 错误处理工具函数
 */ export const errorUtils = {
    /**
   * 判断错误是否可恢复
   */ isRecoverable (error) {
        if (error instanceof FrameworkError) {
            return error.recoverable;
        }
        return false;
    },
    /**
   * 获取错误类型
   */ getErrorType (error) {
        if (error instanceof FrameworkError) {
            return error.type;
        }
        return "UNKNOWN";
    },
    /**
   * 获取错误严重程度
   */ getErrorSeverity (error) {
        if (error instanceof FrameworkError) {
            return error.severity;
        }
        return "MEDIUM";
    },
    /**
   * 标准化错误对象
   */ normalizeError (error) {
        if (error instanceof FrameworkError) {
            return error;
        }
        if (error instanceof Error) {
            return new FrameworkError(error.message, "UNKNOWN", "MEDIUM", {
                originalError: error
            });
        }
        return new FrameworkError(String(error), "UNKNOWN", "MEDIUM");
    },
    /**
   * 提取错误信息
   */ extractErrorInfo (error) {
        const normalized = errorUtils.normalizeError(error);
        return {
            message: normalized.message,
            type: normalized.type,
            severity: normalized.severity,
            code: normalized.code,
            stack: normalized.stack
        };
    },
    /**
   * 获取错误修复建议
   * 
   * 优化：根据错误类型和代码提供修复建议
   */ getErrorSuggestions (error) {
        const normalized = errorUtils.normalizeError(error);
        const suggestions = [];
        // 根据错误代码提供建议
        switch(normalized.code){
            case 'CONTAINER_NOT_FOUND':
                suggestions.push('请确保页面中存在 id="root" 的元素，或通过 container 选项指定挂载容器');
                suggestions.push('检查 DOM 是否已加载完成，确保在 DOMContentLoaded 事件后调用 start()');
                break;
            case 'NETWORK_ERROR':
                suggestions.push('检查网络连接是否正常');
                suggestions.push('检查服务器是否正常运行');
                suggestions.push('检查 CORS 配置是否正确');
                break;
            case 'TIMEOUT_ERROR':
                suggestions.push('增加超时时间配置');
                suggestions.push('检查网络连接是否稳定');
                suggestions.push('检查服务器响应速度');
                break;
            case 'VALIDATION_ERROR':
                suggestions.push('检查配置项是否符合要求');
                suggestions.push('查看配置验证错误详情');
                suggestions.push('参考配置文档确认配置格式');
                break;
            case 'INITIALIZATION_ERROR':
                suggestions.push('检查初始化配置是否正确');
                suggestions.push('查看初始化日志了解详细错误');
                suggestions.push('尝试简化配置，逐步排查问题');
                break;
            default:
                if (normalized.type === "SECURITY") {
                    suggestions.push('检查输入内容是否包含不安全字符');
                    suggestions.push('确保配置项来源可信');
                    suggestions.push('查看安全配置文档');
                }
        }
        // 如果是可恢复的错误，提供恢复建议
        if (normalized.recoverable) {
            suggestions.push('此错误可以自动恢复，请稍后重试');
        }
        return suggestions;
    },
    /**
   * 格式化错误信息（用于开发环境）
   * 
   * 优化：提供更详细的错误信息，包括修复建议
   */ formatErrorForDev (error) {
        const normalized = errorUtils.normalizeError(error);
        const suggestions = errorUtils.getErrorSuggestions(error);
        let message = `[${normalized.code}] ${normalized.message}`;
        if (normalized.stack) {
            message += `\n\n堆栈信息:\n${normalized.stack}`;
        }
        if (suggestions.length > 0) {
            message += `\n\n修复建议:\n${suggestions.map((s, i)=>`${i + 1}. ${s}`).join('\n')}`;
        }
        if (normalized.context) {
            message += `\n\n上下文信息:\n${JSON.stringify(normalized.context, null, 2)}`;
        }
        return message;
    }
};

//# sourceMappingURL=errors.js.map