'use strict';

class TimePickerService {
    static getInputLabel(language, isMinutes) {
        const languageMap = {
            en: {
                'Minutes': 'Minutes',
                'Hour': 'Hour'
            },
            fr: {
                'Minutes': 'Minutes',
                'Hour': 'Heure'
            },
            de: {
                'Minutes': 'Minuten',
                'Hour': 'Stunde'
            },
            ja: {
                'Minutes': '分',
                'Hour': '時間'
            },
            es: {
                'Minutes': 'Minutos',
                'Hour': 'Hora'
            }
        };
        const languageKey = languageMap[language] || languageMap['en'];
        return languageKey[(isMinutes) ? 'Minutes' : 'Hour'];
    }
    // Convert input string to best-guess time format
    static parseTime(timeString) {
        if (timeString === '') {
            return null;
        }
        // Find hours/minutes in string
        const time = timeString.match(/(\d+)(:(\d\d))?\s*(p?)/i);
        if (time === null) {
            return null;
        }
        const hour = time[1];
        const minutes = time[3];
        if (!hour || !minutes) {
            return null;
        }
        // Find AM/PM in string, or default based on hour value
        const controlParse = timeString.match(/am|pm/gi);
        let control = (parseInt(hour, 10) > 12) ? 'PM' : 'AM';
        if (Array.isArray(controlParse)) {
            control = controlParse[0].toUpperCase();
        }
        return {
            hour,
            minutes,
            control
        };
    }
    static parseToTimeFields(input = '') {
        const time = TimePickerService.parseTime(input);
        const timeFields = {
            meridiem: {
                hour: '',
                minutes: '',
                control: ''
            },
            military: {
                hour: '',
                minutes: '',
                control: ''
            }
        };
        if (time) {
            const hours = parseInt(time.hour, 10);
            const minutes = time.minutes;
            timeFields.meridiem.hour = (hours > 12) ? TimePickerService.padZero((hours - 12)) : TimePickerService.padZero(hours);
            timeFields.meridiem.minutes = TimePickerService.padZero(minutes);
            timeFields.meridiem.control = time.control;
            timeFields.military.hour = (time.control === 'PM' && hours <= 12 && hours > 0)
                ? timeFields.military.hour = TimePickerService.padZero(hours + 12)
                : timeFields.military.hour = TimePickerService.padZero(time.hour);
            timeFields.military.minutes = TimePickerService.padZero(minutes);
        }
        return timeFields;
    }
    static padZero(inputString) {
        let transformedString = (inputString) ? inputString.toString() : '';
        if (transformedString.length === 1) {
            transformedString = `0${transformedString}`;
        }
        return transformedString;
    }
    static stringifyParsedTime(parsedTime, timezone) {
        if (!parsedTime) {
            return '';
        }
        const valueHour = parsedTime.hour || '12';
        const valueMinutes = parsedTime.minutes || '00';
        const valueControl = parsedTime.control || '';
        const timeString = `${valueHour}:${valueMinutes} ${valueControl}`.trim();
        return (timezone) ? timeString + ` (${timezone})` : timeString;
    }
}
class TimeDisplay {
}
class MeridiemTimeDisplay {
    constructor(locale, language) {
        this.locale = locale;
        this.language = language;
        this.language = this.language ? this.language : this.locale ? this.locale.split('-')[0] : 'en';
    }
    getInputLabel(isMinutes) {
        return TimePickerService.getInputLabel(this.language, isMinutes);
    }
    genPlaceHolder(parsedTime) {
        if (!parsedTime) {
            return 'hh:mm:am/pm';
        }
        const displayHour = parsedTime.hour || 'hh';
        const displayMinutes = parsedTime.minutes || 'mm';
        const displayControl = (parsedTime.control) ? ` ${parsedTime.control}` : ':am/pm';
        return `${displayHour}:${displayMinutes}${displayControl}`;
    }
    getRegex(isMinutes) {
        return (isMinutes) ? '^[0-5]?[0-9]$' : '^(0?[1-9]|1[012])$';
    }
    getHourRange() {
        return '1-12';
    }
    autoTab(value) {
        return !isNaN(value) && [0, 1].indexOf(value) === -1;
    }
    minmaxHandler(minmax) {
        const timeSegments = TimePickerService.parseToTimeFields(minmax)['military'];
        return {
            boundary: timeSegments.hour.concat(timeSegments.minutes)
        };
    }
    parseToTimeFields(input = '') {
        const parsedTime = TimePickerService.parseToTimeFields(input);
        return parsedTime.meridiem;
    }
    useControl() {
        return true;
    }
    stringifyParsedTime(parsedTime, timezone) {
        parsedTime.control = parsedTime.control || 'AM';
        return TimePickerService.stringifyParsedTime(parsedTime, timezone);
    }
}
class MilitaryTimeDisplay {
    constructor(locale, language) {
        this.locale = locale;
        this.language = language;
        this.language = this.language ? this.language : this.locale ? this.locale.split('-')[0] : 'en';
    }
    getInputLabel(isMinutes) {
        return TimePickerService.getInputLabel(this.language, isMinutes);
    }
    genPlaceHolder(parsedTime) {
        if (!parsedTime) {
            return 'hh:mm';
        }
        const displayHour = parsedTime.hour || 'hh';
        const displayMinutes = parsedTime.minutes || 'mm';
        return `${displayHour}:${displayMinutes}`;
    }
    getRegex(isMinutes) {
        return (isMinutes) ? '^[0-5]?[0-9]$' : '^([01]?[0-9]|2[0-3])$';
    }
    getHourRange() {
        return '0-23';
    }
    autoTab(value) {
        return !isNaN(value) && [0, 1, 2].indexOf(value) === -1;
    }
    minmaxHandler(minmax) {
        const timeSegments = TimePickerService.parseToTimeFields(minmax)['military'];
        return {
            boundary: timeSegments.hour.concat(timeSegments.minutes)
        };
    }
    parseToTimeFields(input = '') {
        const parsedTime = TimePickerService.parseToTimeFields(input);
        return parsedTime.military;
    }
    useControl() {
        return false;
    }
    stringifyParsedTime(parsedTime, timezone) {
        parsedTime.hour = parsedTime.hour || '00';
        return TimePickerService.stringifyParsedTime({ hour: parsedTime.hour, minutes: parsedTime.minutes }, timezone);
    }
}

exports.MeridiemTimeDisplay = MeridiemTimeDisplay;
exports.MilitaryTimeDisplay = MilitaryTimeDisplay;
exports.TimePickerService = TimePickerService;
