"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var redact_pii_exports = {};
__export(redact_pii_exports, {
  redactPii: () => redactPii
});
module.exports = __toCommonJS(redact_pii_exports);
const aptRegex = /(apt|bldg|dept|fl|hngr|lot|pier|rm|ste|slip|trlr|unit|#)\.? *[a-z0-9-]+\b/gi;
const poBoxRegex = /P\.? ?O\.? *Box +\d+/gi;
const roadRegex = /(street|st|road|rd|avenue|ave|drive|dr|loop|court|ct|circle|cir|lane|ln|boulevard|blvd|way)\.?\b/gi;
const piiPatterns = {
  creditCardNumber: /\d{4}[ -]?\d{4}[ -]?\d{4}[ -]?\d{4}|\d{4}[ -]?\d{6}[ -]?\d{4}\d?/g,
  streetAddress: new RegExp(
    `(\\d+\\s*(\\w+ ){1,2}${roadRegex.source}(\\s+${aptRegex.source})?)|(${poBoxRegex.source})`,
    "gi"
  ),
  zipcode: /\b\d{5}(?:[-\s]\d{4})\b/g,
  phoneNumber: /(\(?\+?[0-9]{1,2}\)?[-. ]?)?(\(?[0-9]{3}\)?|[0-9]{3})[-. ]?([0-9]{3}[-. ]?[0-9]{4}|\b[A-Z0-9]{7}\b)/g,
  ipAddress: /(\d{1,3}(\.\d{1,3}){3}|[0-9A-F]{4}(:[0-9A-F]{4}){5}(::|(:0000)+))/gi,
  usSocialSecurityNumber: /\b\d{3}[ -.]\d{2}[ -.]\d{4}\b/g,
  emailAddress: /([a-z0-9_\-.+]+)@\w+(\.\w+)*/gi,
  // username: /(user( ?name)?|login): \S+/gi,
  password: /(pass(word|phrase)?|secret): \S+/gi,
  credentials: /(login( cred(ential)?s| info(rmation)?)?|cred(ential)?s) ?:\s*\S+\s+\/?\s*\S+/gi
  // digits: /\b\d{4,}\b/g,
  // url: /([^\s:/?#]+):\/\/([^/?#\s]*)([^?#\s]*)(\?([^#\s]*))?(#([^\s]*))?/g,
};
const replacer = (key, value) => {
  if (!key || typeof value !== "string" || key === "correlationId")
    return value;
  return Object.values(piiPatterns).reduce(
    (redactedString, piiPattern) => redactedString.replace(piiPattern, "****"),
    value
  );
};
const redactPii = (data) => {
  try {
    const redactedData = JSON.stringify(data, replacer);
    return JSON.parse(redactedData);
  } catch (err) {
    return data;
  }
};
