"use strict";
// @ts-check
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = decrypt;
const crypto_1 = require("crypto");
const buffer_1 = require("buffer");
const grab_keys_1 = __importDefault(require("../../utils/grab-keys"));
/**
 * # Decrypt Function
 */
function decrypt({ encryptedString, encryptionKey, encryptionSalt, }) {
    if (!(encryptedString === null || encryptedString === void 0 ? void 0 : encryptedString.match(/./))) {
        console.log("Encrypted string is invalid");
        return encryptedString;
    }
    const { key: encrptKey, salt, keyLen, algorithm, bufferAllocSize, } = (0, grab_keys_1.default)({ encryptionKey });
    if (!(encrptKey === null || encrptKey === void 0 ? void 0 : encrptKey.match(/.{8,}/))) {
        console.log("Decrption key is invalid");
        return encryptedString;
    }
    if (!(salt === null || salt === void 0 ? void 0 : salt.match(/.{8,}/))) {
        console.log("Decrption salt is invalid");
        return encryptedString;
    }
    let key = (0, crypto_1.scryptSync)(encrptKey, salt, keyLen);
    let iv = buffer_1.Buffer.alloc(bufferAllocSize, 0);
    const decipher = (0, crypto_1.createDecipheriv)(algorithm, key, iv);
    try {
        let decrypted = decipher.update(encryptedString, "hex", "utf8");
        decrypted += decipher.final("utf8");
        return decrypted;
    }
    catch (error) {
        console.log("Error in decrypting =>", error.message);
        return encryptedString;
    }
}