import type { ParentSchedule, ParentTaskParams } from '@/api/parent/types'
|
|
const WEEKDAY_KEYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] as const
|
|
export function pad2(value: number): string {
|
return String(value).padStart(2, '0')
|
}
|
|
export function normalizeTime(raw?: string): string {
|
const text = String(raw || '').trim()
|
const match = text.match(/^(\d{1,2}):(\d{1,2})/)
|
if (!match)
|
return '07:30'
|
const hour = Math.min(23, Math.max(0, Number(match[1])))
|
const minute = Math.min(59, Math.max(0, Number(match[2])))
|
return `${pad2(hour)}:${pad2(minute)}`
|
}
|
|
export function weekdayLabel(days: number[] | undefined, t: (key: string) => string): string {
|
const unique = [...new Set((days || []).filter(day => day >= 1 && day <= 7))].sort()
|
if (unique.length === 0 || unique.length === 7)
|
return t('parentTask.everyDay')
|
if (unique.join(',') === '1,2,3,4,5')
|
return t('parentTask.weekdays')
|
return unique.map(day => t(`parentTask.weekday.${WEEKDAY_KEYS[day - 1]}`)).join('、')
|
}
|
|
export function formatSchedule(schedule: ParentSchedule | undefined, t: (key: string) => string): string {
|
if (!schedule?.type)
|
return ''
|
if (schedule.type === 'hourly') {
|
const minute = Number(schedule.minute || 0)
|
return minute === 0
|
? t('parentTask.hourlyOnTheHour')
|
: t('parentTask.hourlyAt', { minute: pad2(minute) })
|
}
|
if (schedule.type === 'interval') {
|
return t('parentTask.everyMinutes', { minutes: schedule.every_minutes || 60 })
|
}
|
return `${weekdayLabel(schedule.weekdays, t)} ${normalizeTime(schedule.time)}`
|
}
|
|
export function formatOnComplete(params: ParentTaskParams | undefined, t: (key: string) => string): string {
|
if (params?.on_complete === 'power_off')
|
return t('parentTask.completePowerOff')
|
return t('parentTask.completeChat')
|
}
|
|
export function formatLesson(params: ParentTaskParams | undefined): string {
|
if (params?.lesson_name && params?.lesson_name_en)
|
return `${params.lesson_name} / ${params.lesson_name_en}`
|
if (params?.lesson_name)
|
return params.lesson_name
|
return params?.lesson_id || ''
|
}
|