File size: 1,708 Bytes
2c28af1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export const WAIT_UNITS = [
    { value: 'days', label: 'Days', singular: 'day', min: 1, max: 365 },
    { value: 'hours', label: 'Hours', singular: 'hour', min: 1, max: 720 },
    { value: 'minutes', label: 'Minutes', singular: 'minute', min: 1, max: 10080 },
];

export function waitStepAmount(step) {
    if (!step) return 1;
    if (step.amount != null) return Number(step.amount) || 1;
    if (step.days != null) return Number(step.days) || 1;
    return 1;
}

export function waitStepUnit(step) {
    const u = (step?.unit || 'days').toLowerCase();
    if (u === 'hours' || u === 'hour') return 'hours';
    if (u === 'minutes' || u === 'minute') return 'minutes';
    return 'days';
}

export function waitUnitMeta(unit) {
    return WAIT_UNITS.find((u) => u.value === unit) || WAIT_UNITS[0];
}

export function clampWaitAmount(amount, unit) {
    const meta = waitUnitMeta(unit);
    const n = Number(amount);
    if (!Number.isFinite(n)) return meta.min;
    return Math.max(meta.min, Math.min(meta.max, Math.round(n)));
}

export function formatWaitStep(step, { short = false } = {}) {
    const amount = waitStepAmount(step);
    const unit = waitStepUnit(step);
    const meta = waitUnitMeta(unit);
    if (short) {
        if (unit === 'days') return `${amount}d`;
        if (unit === 'hours') return `${amount}h`;
        return `${amount}m`;
    }
    const word = amount === 1 ? meta.singular : meta.label.toLowerCase();
    return `${amount} ${word}`;
}

export function normalizeWaitStep(step) {
    if (!step || step.type !== 'wait') return step;
    const unit = waitStepUnit(step);
    const amount = clampWaitAmount(waitStepAmount(step), unit);
    return { ...step, amount, unit };
}