import dayjs from 'dayjs'
import {
    DatePicker,
    RangePicker,
} from '..'

export function getValueExtractor(comp: DatePicker | RangePicker) {
    if (comp.props.mode === 'quarter') {
        return (text: string) => {
            const parts = text.split('-Q').map(i => ~~i)
            if (parts.length === 2) {
                const month = (parts[1] - 1) * 3
                if (month < 0 || month > 11) {
                    return dayjs('')
                }
                return dayjs(new Date(parts[0], month))
            }

            // 返回 invalid 日期 (如果不合法)
            return dayjs('')
        }
    }
    return (text: string) => dayjs(text, comp.computedItemFormat)
}
