"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleUnaccent = exports.getResponsiveClass = exports.decryptCookie = exports.encryptDataSync = void 0;
exports.unaccentValue = unaccentValue;
const tslib_1 = require("tslib");
const crypto_js_1 = tslib_1.__importDefault(require("crypto-js"));
const constants_1 = require("../utils/constants");
const encryptDataSync = (data) => {
    const iv = crypto_js_1.default.lib.WordArray.random(constants_1.IV_LENGTH);
    const encrypted = crypto_js_1.default.AES.encrypt(JSON.stringify(data), crypto_js_1.default.enc.Hex.parse(constants_1.SECRET_KEY), {
        iv,
        mode: crypto_js_1.default.mode.CBC,
        padding: crypto_js_1.default.pad.Pkcs7,
    });
    return JSON.stringify({
        iv: crypto_js_1.default.enc.Base64.stringify(iv),
        encryptedData: encrypted.toString(),
    });
};
exports.encryptDataSync = encryptDataSync;
const decryptCookie = (encryptedJson) => {
    try {
        const { encryptedData, iv } = JSON.parse(encryptedJson);
        const ivBytes = crypto_js_1.default.enc.Base64.parse(iv);
        const secretKeyBytes = crypto_js_1.default.enc.Hex.parse(constants_1.SECRET_KEY);
        const decrypted = crypto_js_1.default.AES.decrypt(encryptedData, secretKeyBytes, {
            iv: ivBytes,
            mode: crypto_js_1.default.mode.CBC,
            padding: crypto_js_1.default.pad.Pkcs7,
        });
        const decryptedText = decrypted.toString(crypto_js_1.default.enc.Utf8);
        return decryptedText ? JSON.parse(decryptedText) : null;
    }
    catch (error) {
        console.error('[decryptCookie] Error:', error);
        return null;
    }
};
exports.decryptCookie = decryptCookie;
// chuyển chữ có dấu thành không dấu
function unaccentValue(rawValue) {
    if (!rawValue)
        return '';
    return rawValue
        .normalize('NFD') // Tách các ký tự có dấu thành ký tự cơ bản và dấu
        .replace(/[\u0300-\u036f]/g, '') // Loại bỏ các dấu
        .replace(/Đ/g, 'D') // Thay Đ thành D
        .replace(/đ/g, 'd'); // Thay đ thành d
}
const getResponsiveClass = (prefix, value, map) => {
    if (!value)
        return '';
    if (typeof value !== 'object') {
        const finalVal = map ? map[value] : value;
        return finalVal ? `${prefix}-${String(finalVal)}` : prefix;
    }
    return Object.entries(value)
        .map(([breakpoint, val]) => {
        if (val === undefined)
            return '';
        const finalVal = map ? map[val] : val;
        const classBody = finalVal ? `${prefix}-${String(finalVal)}` : prefix;
        if (breakpoint === 'initial') {
            return classBody;
        }
        return `${breakpoint}:${classBody}`;
    })
        .filter(Boolean)
        .join(' ');
};
exports.getResponsiveClass = getResponsiveClass;
/**
 *
 *
 * @param {string} str
 * @return {*}
 * chuyển về chữ không dấu
 */
const handleUnaccent = (str) => {
    if (!str || str === '')
        return '';
    const unaccentedStr = str
        .normalize('NFD')
        .replace(/[\u0300-\u036f]/g, '')
        .replace(/đ/g, 'd')
        .replace(/Đ/g, 'D')
        .replace(/_/g, '')
        .replace(/[^\w.\s]/g, '');
    return unaccentedStr;
};
exports.handleUnaccent = handleUnaccent;
//# sourceMappingURL=helpers.js.map