File size: 2,887 Bytes
760daa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const helpers = {
    removeAccents: (str) => {
        if (!str) return "";
        return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/đ/g, 'd').replace(/Đ/g, 'D');
    },

    mapWeightToSize: (weightStr) => {
        const w = parseInt(weightStr);
        if (isNaN(w) || w < 5) return null;
        if (w >= 5 && w <= 10) return "1";
        if (w > 10 && w <= 12) return "2";
        if (w > 12 && w <= 14) return "3";
        if (w > 14 && w <= 16) return "4";
        if (w > 16 && w <= 18) return "5";
        if (w > 18 && w <= 20) return "6";
        if (w > 20 && w <= 22) return "7";
        if (w > 22 && w <= 25) return "8";
        if (w > 25 && w <= 30) return "10";
        if (w > 30 && w <= 35) return "12";
        if (w > 35 && w <= 40) return "14";
        if (w > 40 && w <= 48) return "S";
        if (w > 48 && w <= 55) return "M";
        if (w > 55 && w <= 62) return "L";
        if (w > 62 && w <= 70) return "XL";
        return "2XL";
    },

    formatReply: (reply, session, globalInventory = null) => {
        if (!reply) return "";
        let finalReply = reply;
        
        const entities = session.entities || {};
        const { productCode, weight, phone, address } = entities;
        const inventory = (globalInventory && globalInventory.inventory) ? globalInventory.inventory : (globalInventory || {});

        // A. Xử lý Địa chỉ & SĐT
        finalReply = finalReply.replace(/{{dia_chi}}|{address}/g, address || "địa chỉ cũ của Mẹ");
        finalReply = finalReply.replace(/{{sdt}}|{phone}/g, phone || "số điện thoại nhen");

        // B. Xử lý Mã hàng
        const code = productCode ? String(productCode).toUpperCase() : null;
        finalReply = finalReply.replace(/{productCode}|{{ma_hang}}/g, code || "mẫu này");

        // C. Xử lý Size
        const size = weight ? helpers.mapWeightToSize(weight) : null;
        finalReply = finalReply.replace(/{size}|{{size}}/g, size ? `Size ${size}` : "size chuẩn");

        // D. Xử lý Ảnh
        if (finalReply.includes("{image_link}")) {
            const img = (code && inventory[code]) ? inventory[code].image : null;
            finalReply = finalReply.replace(/{image_link}/g, img ? `📸 Ảnh: ${img}` : "tại kho nhen!");
        }

        // --- E. FIX NÚT BẤM DÀI & LẶP ICON ---
        // 1. Dọn dẹp icon ✨ bị nhân đôi (lỗi trong ảnh Đạt gửi)
        finalReply = finalReply.replace(/✨\s*✨/g, '✨');

        // 2. Ép cấu trúc [Label|Command] - Xóa khoảng trắng để nút co lại tối đa
        finalReply = finalReply.replace(/\[\s*([^\]|]+)\s*\|\s*([^\]]+)\s*\]/g, (match, label, command) => {
            return `[${label.trim()}|${command.trim()}]`;
        });

        return finalReply.replace(/\s\s+/g, ' ').trim();
    }
};

module.exports = helpers;