"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hashConfigs = exports.EmptyValueError = exports.DigestTypes = exports.EncryptionMethods = void 0;
exports.processHashing = processHashing;
const crypto = __importStar(require("crypto"));
exports.EncryptionMethods = ['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'];
exports.DigestTypes = ['hex', 'base64'];
class EmptyValueError extends Error {
    constructor(message) {
        super(message);
        this.name = 'EmptyValueError';
    }
}
exports.EmptyValueError = EmptyValueError;
exports.hashConfigs = {
    md5: { lengthHex: 32, lengthBase64: 24 },
    sha1: { lengthHex: 40, lengthBase64: 28 },
    sha224: { lengthHex: 56, lengthBase64: 40 },
    sha256: { lengthHex: 64, lengthBase64: 44 },
    sha384: { lengthHex: 96, lengthBase64: 64 },
    sha512: { lengthHex: 128, lengthBase64: 88 }
};
class SmartHashing {
    constructor(encryptionMethod = 'sha256', digest = 'hex') {
        this.encryptionMethod = encryptionMethod;
        this.digest = digest;
        this.preHashed = false;
    }
    isAlreadyHashed(value) {
        const config = exports.hashConfigs[this.encryptionMethod];
        if (!config)
            throw new Error(`Unsupported encryption method: ${this.encryptionMethod}`);
        let regex;
        switch (this.digest) {
            case 'hex':
                regex = new RegExp(`^[a-f0-9]{${config.lengthHex}}$`, 'i');
                this.preHashed = value.length === config.lengthHex && regex.test(value);
                break;
            case 'base64':
                regex = new RegExp(`^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$`);
                this.preHashed = value.length === config.lengthBase64 && regex.test(value);
                break;
            default:
                throw new Error(`Unsupported digest type: ${this.digest}`);
        }
        return this.preHashed;
    }
    hash(value) {
        if (value.trim() === '') {
            throw new EmptyValueError('Cannot hash an empty string');
        }
        if (this.preHashed) {
            return value;
        }
        return crypto.createHash(this.encryptionMethod).update(value).digest(this.digest);
    }
}
function processHashing(value, encryptionMethod, digest, cleaningFunction) {
    if (value.trim() === '') {
        return '';
    }
    const smartHashing = new SmartHashing(encryptionMethod, digest);
    if (smartHashing.isAlreadyHashed(value)) {
        return value;
    }
    if (cleaningFunction) {
        value = cleaningFunction(value);
    }
    return smartHashing.hash(value);
}
//# sourceMappingURL=hashing-utils.js.map