export class DatetimePickerService {
    static addTime(date, time) {
        if (!date) {
            return date;
        }
        const mins = (time.minutes) ? Number(time.minutes) : 0;
        let hrs = 0;
        if (time.hour && Number(time.hour) === 12) {
            hrs = (time.control && time.control === 'AM') ? 0 : Number(12);
        }
        else if (time.hour) {
            const controlOffset = (time.control && time.control === 'PM') ? 12 : 0;
            hrs = Number(time.hour) + controlOffset;
        }
        const adjustedDateTime = date.getTime() + (mins * 60000) + (hrs * 3600000);
        return new Date(adjustedDateTime);
    }
    static getNoTimeDate(date) {
        const d = new Date(date);
        d.setHours(0, 0, 0, 0);
        return d;
    }
    static localizeForOuterValue(date, locale, meridiem) {
        if (!date) {
            return undefined;
        }
        const options = { hour12: !!meridiem, month: '2-digit', day: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit' };
        return new Intl.DateTimeFormat(locale, options).format(date);
    }
    // Brittle, but only effective way to uniformly derive time from an international string, because js
    static replacer(inputString) {
        /* tslint:disable:no-irregular-whitespace */
        // Identifies if am/pm in another language exists
        const amRegex = new RegExp('a..m.|午前');
        const pmRegex = new RegExp('p..m.|午後');
        const am = amRegex.test(inputString);
        const pm = pmRegex.test(inputString);
        const cleaned = inputString
            // remove anything that will make Date evaluation fail
            .replace(',', '')
            .replace('à', '')
            // remove language-specific am/pm notation
            .replace(amRegex, '')
            .replace(pmRegex, '');
        // Re-identify AM/PM appropriately
        if (am) {
            return cleaned.concat(' AM');
        }
        else if (pm) {
            return cleaned.concat(' PM');
        }
        else {
            return cleaned;
        }
    }
}
