"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBoldIconColor = exports.truncateByChars = exports.htmlToText = exports.normalizeCode = exports.checkChildBefore = exports.delay = void 0;
exports.normalizeString = normalizeString;
exports.flattenMenuTree = flattenMenuTree;
exports.enablePer = enablePer;
const constant_1 = require("./constant");
function normalizeString(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
        .replace(/y/g, 'i') // Thay y thành i
        .trim()
        .toLowerCase();
}
function flattenMenuTree(tree) {
    const result = [];
    function insertIfNotExist(newData, currResult) {
        const isExist = currResult.some((item) => item.name === newData.name);
        if (!isExist)
            result.push(newData);
    }
    tree.forEach((root) => {
        var _a;
        const rootCode = root.code;
        if (rootCode === constant_1.MENU_CODE.DASHBOARD)
            return;
        const rootPath = root.path;
        insertIfNotExist({
            id: root.code,
            name: root.name,
            code: rootCode,
            path: root.path,
            pathCode: rootCode,
        }, result);
        if (((_a = root === null || root === void 0 ? void 0 : root.child) === null || _a === void 0 ? void 0 : _a.length) > 0 && rootCode !== constant_1.MENU_CODE.DASHBOARD) {
            root.child.forEach((child1) => {
                var _a;
                let finalPath1 = '';
                if (rootPath.includes(child1.path)) {
                    finalPath1 = rootPath;
                }
                else {
                    finalPath1 = `/${rootPath.split('/')[1]}${child1.path}`;
                }
                insertIfNotExist({
                    id: `${root.code}_${child1.code}_${child1.name}`,
                    name: `${root.name} / ${child1.name}`,
                    code: rootCode,
                    path: finalPath1,
                    pathCode: child1.code,
                }, result);
                if (((_a = child1 === null || child1 === void 0 ? void 0 : child1.child) === null || _a === void 0 ? void 0 : _a.length) > 0) {
                    child1.child.forEach((child2) => {
                        let finalPath2 = '';
                        if (rootPath.includes(child2.path)) {
                            finalPath2 = finalPath1;
                        }
                        else {
                            finalPath2 = `/${finalPath1.split('/')[1]}${child2.path}`;
                        }
                        insertIfNotExist({
                            id: `${root.code}_${child1.code}_${child2.code}_${child2.name}`,
                            name: `${root.name} / ${child2.name}`,
                            code: rootCode,
                            path: finalPath2,
                            pathCode: child2.code,
                        }, result);
                    });
                }
            });
        }
    });
    return result;
}
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
exports.delay = delay;
function enablePer(code, arr) {
    if (!code)
        return true;
    return arr === null || arr === void 0 ? void 0 : arr.some((item) => item === code);
}
const checkChildBefore = (item, currentItem) => {
    const currentIndexActive = item.find((s) => s.current);
    return currentIndexActive ? currentIndexActive.id > currentItem.id : false;
};
exports.checkChildBefore = checkChildBefore;
const normalizeCode = (raw) => {
    if (!raw)
        return '';
    return raw.replace(/-/g, '_');
};
exports.normalizeCode = normalizeCode;
const htmlToText = (html) => {
    if (!html)
        return '';
    return html
        .replace(/<style[\s\S]*?>[\s\S]*?<\/style>/gi, ' ')
        .replace(/<script[\s\S]*?>[\s\S]*?<\/script>/gi, ' ')
        .replace(/<br\s*\/?>/gi, ' ')
        .replace(/<\/p>/gi, ' ')
        .replace(/<[^>]+>/g, ' ')
        .replace(/&nbsp;/g, ' ')
        .replace(/\s+/g, ' ')
        .trim();
};
exports.htmlToText = htmlToText;
const truncateByChars = (text, limit) => {
    const t = (text !== null && text !== void 0 ? text : '').trim();
    if (!t)
        return { short: '', isLong: false };
    // nếu <= limit => không cắt
    if (t.length <= limit)
        return { short: t, isLong: false };
    // cắt và trim tránh dính khoảng trắng
    const short = t.slice(0, limit).trimEnd();
    return { short, isLong: true };
};
exports.truncateByChars = truncateByChars;
const getBoldIconColor = (isActive) => (isActive ? '#111827' : '#4B5563');
exports.getBoldIconColor = getBoldIconColor;
//# sourceMappingURL=helper.js.map