File size: 3,490 Bytes
691cd90
e00f16a
0b01d1f
 
9c3169c
 
691cd90
 
9c3169c
e00f16a
9c3169c
e00f16a
bbb4e4b
 
0b01d1f
 
 
bbb4e4b
 
 
0b01d1f
e00f16a
 
0b01d1f
 
 
691cd90
 
9c3169c
691cd90
bbb4e4b
 
691cd90
 
 
 
 
 
 
 
 
 
 
 
 
e00f16a
 
 
 
691cd90
0b01d1f
 
 
 
 
691cd90
e00f16a
 
bbb4e4b
e00f16a
bbb4e4b
e00f16a
 
bbb4e4b
691cd90
0b01d1f
bbb4e4b
e00f16a
bbb4e4b
 
 
691cd90
 
bbb4e4b
 
 
e00f16a
bbb4e4b
 
 
 
 
 
e00f16a
bbb4e4b
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/** engine/extractor.js - With Remove Logic */
const KEYWORD_MAP = {
    'bc': 'bc', 'ื›ืจื˜ื™ืก': 'bc', 'ื›ืจื˜ื™ืกื™ื': 'bc', 'ื‘ื™ืงื•ืจ': 'bc',
    'flyer': 'flyer', 'ืคืœื™ื™ืจ': 'flyer', 'ืคืœื™ื™ืจื™ื': 'flyer', 'ืขืœื•ืŸ': 'flyer',
    'booklet': 'booklet', 'ื—ื•ื‘ืจืช': 'booklet', 'ื—ื•ื‘ืจื•ืช': 'booklet', 'ืกืคืจ': 'booklet', 'ืกืคืจื™ื': 'booklet', 'ืงื˜ืœื•ื’': 'booklet',
    'invitation': 'invitation', 'ื”ื–ืžื ื”': 'invitation', 'ื”ื–ืžื ื•ืช': 'invitation', 'ื—ืชื•ื ื”': 'invitation',
    'rollup': 'rollup', 'ืจื•ืœืืค': 'rollup', 'ื‘ืื ืจ': 'rollup',
    'poster': 'poster', 'ืคื•ืกื˜ืจ': 'poster', 'ืงื ื‘ืก': 'poster',
    'sticker': 'sticker', 'ืžื“ื‘ืงื”': 'sticker', 'ืžื“ื‘ืงื•ืช': 'sticker',
    'envelope': 'envelope', 'ืžืขื˜ืคื”': 'envelope', 'ืžืขื˜ืคื•ืช': 'envelope',
    'folder': 'folder', 'ืคื•ืœื“ืจ': 'folder'
};

const HEBREW_NUMBERS = {
    'ืื—ื“': 1, 'ืื—ืช': 1, 'ืฉื ื™': 2, 'ืฉืชื™': 2, 'ืฉืœื•ืฉ': 3, 'ืฉืœื•ืฉื”': 3,
    'ืืจื‘ืข': 4, 'ืืจื‘ืขื”': 4, 'ื—ืžืฉ': 5, 'ื—ืžื™ืฉื”': 5, 'ืฉืฉ': 6, 'ืฉื™ืฉื”': 6,
    'ืฉื‘ืข': 7, 'ืฉืžื•ื ื”': 8, 'ืชืฉืข': 9, 'ืขืฉืจ': 10, 'ืžืื”': 100, 'ืืœืฃ': 1000
};

function extractParameters(text) {
    let cleanText = text.toLowerCase().replace(/,/g, '');
    
    const result = {
        products: [],
        qty: null,
        isReset: false,
        isRemove: false, // <--- ื—ื“ืฉ
        targetIndex: null, // <--- ื—ื“ืฉ
        isCartStatus: false,
        raw_text: text
    };

    // 1. ื–ื™ื”ื•ื™ ืžื—ื™ืงื” ืกืคืฆื™ืคื™ืช
    const removeKeywords = ['ืžื—ืง', 'ื”ืกืจ', 'ื‘ื˜ืœ', 'ืœื”ื•ืจื™ื“', 'ืชื•ืจื™ื“'];
    if (removeKeywords.some(w => cleanText.includes(w))) {
        result.isRemove = true;
        // ื ื ืกื” ืœืžืฆื•ื ืžืกืคืจ (ืœืžืฉืœ: "ืžื—ืง ืืช 1")
        const numMatch = cleanText.match(/(\d+)/);
        if (numMatch) result.targetIndex = parseInt(numMatch[0]);
        // ืื ืื™ืŸ ืžืกืคืจ, ื ื ืกื” ืœืžืฆื•ื ืฉื ืžื•ืฆืจ ืœืžื˜ื”...
    }

    // 2. ื–ื™ื”ื•ื™ ืื™ืคื•ืก ืžืœื
    const resetKeywords = ['reset', 'ื”ืชื—ืœ', 'ืชืคืจื™ื˜', 'ื ืงื” ื”ื›ืœ', 'ืื™ืคื•ืก', 'ื™ืฆื™ืื”'];
    if (resetKeywords.some(word => cleanText.includes(word)) && !result.isRemove) {
        result.isReset = true;
        return result;
    }

    // 3. ื–ื™ื”ื•ื™ ืขื’ืœื”
    if (cleanText.includes('cart') || cleanText.includes('ืขื’ืœื”') || cleanText.includes('ืกื™ื›ื•ื') || cleanText.includes('ืกื˜ื˜ื•ืก')) {
        result.isCartStatus = true;
        return result;
    }

    // 4. ื–ื™ื”ื•ื™ ืžื•ืฆืจื™ื
    const foundProducts = new Set();
    Object.keys(KEYWORD_MAP).forEach(keyword => {
        if (cleanText.includes(keyword)) {
            foundProducts.add(KEYWORD_MAP[keyword]);
        }
    });
    result.products = Array.from(foundProducts);

    // 5. ื–ื™ื”ื•ื™ ื›ืžื•ืช
    const kMatch = cleanText.match(/(\d+)k/);
    if (kMatch) {
        result.qty = parseInt(kMatch[1]) * 1000;
    } else {
        const numMatch = cleanText.match(/\d+/);
        if (numMatch) {
            // ืื ื–ื” ืœื ืžื—ื™ืงื”, ื ื™ืงื— ืืช ื”ืžืกืคืจ ื›ื›ืžื•ืช
            if (!result.isRemove) result.qty = parseInt(numMatch[0]);
        } else {
            for (const [word, val] of Object.entries(HEBREW_NUMBERS)) {
                if (cleanText.includes(word)) {
                    result.qty = val;
                    break;
                }
            }
        }
    }

    return result;
}

module.exports = { extractParameters };