/**
 * 配置验证模块
 * 使用Zod进行配置验证
 */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get ConfigValidationError () {
        return ConfigValidationError;
    },
    get ConfigValidator () {
        return ConfigValidator;
    }
});
const _zod = require("zod");
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;
}
let ConfigValidationError = class ConfigValidationError extends Error {
    constructor(message, errors){
        super(message), _define_property(this, "errors", void 0), this.errors = errors;
        this.name = 'ConfigValidationError';
    }
};
let ConfigValidator = class ConfigValidator {
    /**
   * 验证配置
   * 
   * @param config - 要验证的配置
   * @param schema - Zod模式
   * @returns 验证后的配置
   * @throws ConfigValidationError 如果配置验证失败
   */ static validate(config, schema) {
        const result = schema.safeParse(config);
        if (!result.success) {
            throw new ConfigValidationError('配置验证失败', result.error);
        }
        return result.data;
    }
    /**
   * 安全验证配置（不抛出异常）
   * 
   * @param config - 要验证的配置
   * @param schema - Zod模式
   * @returns 验证结果
   */ static safeValidate(config, schema) {
        const result = schema.safeParse(config);
        if (result.success) {
            return {
                success: true,
                data: result.data
            };
        }
        return {
            success: false,
            error: result.error
        };
    }
    /**
   * 创建StartOptions的部分验证模式
   * 注意：这是一个基础模式，可以根据需要扩展
   */ static createStartOptionsSchema() {
        return _zod.z.object({
            loggerLevel: _zod.z.enum([
                'DEBUG',
                'INFO',
                'WARN',
                'ERROR'
            ]).optional(),
            showSplashScreen: _zod.z.enum([
                'auto',
                'always',
                'never',
                'first-time-only'
            ]).optional(),
            minSplashScreenTime: _zod.z.number().positive().optional(),
            security: _zod.z.object({
                enableXSSProtection: _zod.z.boolean().optional(),
                validateConfig: _zod.z.boolean().optional(),
                sanitizeUserInput: _zod.z.boolean().optional(),
                csp: _zod.z.object({
                    defaultSrc: _zod.z.array(_zod.z.string()).optional(),
                    scriptSrc: _zod.z.array(_zod.z.string()).optional(),
                    styleSrc: _zod.z.array(_zod.z.string()).optional(),
                    imgSrc: _zod.z.array(_zod.z.string()).optional(),
                    connectSrc: _zod.z.array(_zod.z.string()).optional(),
                    fontSrc: _zod.z.array(_zod.z.string()).optional(),
                    frameSrc: _zod.z.array(_zod.z.string()).optional(),
                    allowUnsafeInline: _zod.z.boolean().optional(),
                    allowUnsafeEval: _zod.z.boolean().optional()
                }).optional()
            }).optional()
        }).passthrough(); // passthrough允许其他字段通过
    }
    /**
   * 验证配置并提供友好的错误信息
   * 
   * @param config - 要验证的配置
   * @param schema - Zod模式
   * @returns 验证后的配置
   * @throws ConfigValidationError 如果配置验证失败，包含详细的错误信息
   */ static validateWithFriendlyErrors(config, schema) {
        const result = schema.safeParse(config);
        if (!result.success) {
            // 构建友好的错误信息
            // 注意：ZodError 使用 issues 属性，不是 errors
            const errorMessages = result.error.issues.map((issue)=>{
                const path = issue.path.length > 0 ? issue.path.join('.') : 'root';
                return `配置项 "${path}": ${issue.message}`;
            });
            const friendlyMessage = `配置验证失败:\n${errorMessages.join('\n')}`;
            throw new ConfigValidationError(friendlyMessage, result.error);
        }
        return result.data;
    }
};

//# sourceMappingURL=ConfigValidator.js.map