"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PIIDetector = void 0;
const DEFAULT_CONFIG = {
    enabled: true,
    patterns: {
        email: "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b",
        phone: "\\b(?:\\+?1[-.\\s]?)?\\(?([0-9]{3})\\)?[-.\\s]?([0-9]{3})[-.\\s]?([0-9]{4})\\b",
        ssn: "\\b\\d{3}-\\d{2}-\\d{4}\\b",
        creditCard: "\\b\\d{4}[\\s-]?\\d{4}[\\s-]?\\d{4}[\\s-]?\\d{4}\\b",
        apiKey: "\\b(?:sk-|pk-|AKIA|ghp_|gho_)[A-Za-z0-9]{16,}\\b"
    },
    maskFormat: "[REDACTED:{type}]"
};
class PIIDetector {
    constructor(config) {
        this.config = { ...DEFAULT_CONFIG, ...config };
    }
    detect(text) {
        if (!this.config.enabled) {
            return null;
        }
        const textStr = this.extractText(text);
        if (!textStr) {
            return null;
        }
        const matches = [];
        const patterns = this.config.patterns;
        for (const [type, pattern] of Object.entries(patterns)) {
            const regex = new RegExp(pattern, 'gi');
            let match;
            while ((match = regex.exec(textStr)) !== null) {
                matches.push({
                    type,
                    value: match[0],
                    position: match.index
                });
            }
        }
        if (matches.length === 0) {
            return null;
        }
        return {
            detected: true,
            patterns: [...new Set(matches.map(m => m.type))],
            matches
        };
    }
    mask(text) {
        if (!this.config.enabled) {
            return typeof text === 'string' ? text : JSON.stringify(text);
        }
        const textStr = this.extractText(text);
        if (!textStr) {
            return typeof text === 'string' ? text : JSON.stringify(text);
        }
        let maskedText = textStr;
        const patterns = this.config.patterns;
        for (const [type, pattern] of Object.entries(patterns)) {
            const regex = new RegExp(pattern, 'gi');
            const maskValue = this.getMaskValue(type);
            maskedText = maskedText.replace(regex, maskValue);
        }
        return maskedText;
    }
    detectAndMask(text) {
        const detection = this.detect(text);
        if (!detection) {
            return null;
        }
        const maskedText = this.mask(text);
        return {
            ...detection,
            maskedText
        };
    }
    getMaskValue(type) {
        const format = this.config.maskFormat;
        switch (type) {
            case 'email':
                return format.replace('{type}', 'EMAIL') || '***@***';
            case 'phone':
                return format.replace('{type}', 'PHONE') || '***-***-****';
            case 'ssn':
                return format.replace('{type}', 'SSN') || '***-**-****';
            case 'creditCard':
                return format.replace('{type}', 'CREDIT_CARD') || '****-****-****-****';
            case 'apiKey':
                return format.replace('{type}', 'API_KEY') || 'sk-***';
            default:
                return format.replace('{type}', type.toUpperCase()) || '[REDACTED]';
        }
    }
    extractText(input) {
        if (typeof input === 'string') {
            return input;
        }
        if (typeof input === 'object' && input !== null) {
            if (input.text)
                return String(input.text);
            if (input.content)
                return String(input.content);
            if (input.message)
                return String(input.message);
            if (input.prompt)
                return String(input.prompt);
            if (input.input)
                return String(input.input);
            try {
                return JSON.stringify(input);
            }
            catch {
                return null;
            }
        }
        return String(input);
    }
    updateConfig(config) {
        this.config = { ...this.config, ...config };
    }
    getConfig() {
        return { ...this.config };
    }
}
exports.PIIDetector = PIIDetector;
//# sourceMappingURL=pii-detector.js.map