/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
export const DEFAULT_RULES = [
    {
        name: 'email',
        pattern: /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g,
        replacement: '[REDACTED-EMAIL]',
    },
    {
        name: 'file-path-user',
        pattern: /\/(?:Users|home)\/[^/\s]+/g,
        replacement: '/[REDACTED-PATH]',
    },
    {
        name: 'ipv4',
        pattern: /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/g,
        replacement: '[REDACTED-IP]',
    },
];
export function redactString(input, rules = DEFAULT_RULES) {
    let output = input;
    let redactionCount = 0;
    for (const rule of rules) {
        const regex = new RegExp(rule.pattern.source, rule.pattern.flags);
        const matches = output.match(regex);
        if (matches) {
            redactionCount += matches.length;
            output = output.replace(regex, rule.replacement);
        }
    }
    return { output, redactionCount };
}
export function redactRecord(record, rules = DEFAULT_RULES) {
    const output = {};
    let totalRedactions = 0;
    for (const [key, value] of Object.entries(record)) {
        if (typeof value === 'string') {
            const { output: redacted, redactionCount } = redactString(value, rules);
            output[key] = redacted;
            totalRedactions += redactionCount;
        }
        else {
            output[key] = value;
        }
    }
    return { output, redactionCount: totalRedactions };
}
//# sourceMappingURL=pii-redactor.js.map