"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DateTimeUtils = void 0;
const luxon_1 = require("@golemio/core/dist/shared/luxon");
const moment_timezone_1 = __importDefault(require("@golemio/core/dist/shared/moment-timezone"));
class DateTimeUtils {
    /**
     * @example 2022-10-26 08:55:10+01 -> 2022-10-26T09:55:10+02:00
     */
    static formatSQLTimestamp(sqlTimestamp) {
        return luxon_1.DateTime.fromSQL(sqlTimestamp).setZone(this.TIMEZONE).toFormat(this.FORMAT_DATE_TIME);
    }
    /**
     * @example 2022-10-26T08:55:10+01:00 -> 09:55:10
     */
    static parseUTCTimeFromISO(isoTimestamp = new Date().toISOString()) {
        return luxon_1.DateTime.fromISO(isoTimestamp).setZone(this.TIMEZONE).toFormat(this.FORMAT_TIME);
    }
    static getStopDateTimeForTripOrigin(stopTime, tripOriginTimestamp) {
        const { hours: stopTimeHours, minutes: stopTimeMinutes, seconds: stopTimeSeconds } = this.getTimeSegments(stopTime);
        const tripOriginMoment = moment_timezone_1.default.utc(tripOriginTimestamp).tz(this.TIMEZONE);
        const isOvernight = stopTimeHours >= 24;
        const isOriginAfterMidnight = tripOriginMoment.hours() < 12;
        const shouldSubtractADayFromTripOrigin = isOvernight && isOriginAfterMidnight;
        tripOriginMoment.subtract(shouldSubtractADayFromTripOrigin ? 1 : 0, "days");
        tripOriginMoment.hours(12).minutes(0).seconds(0).milliseconds(0);
        tripOriginMoment.subtract(12, "hours");
        tripOriginMoment.add(stopTimeHours, "hours").add(stopTimeMinutes, "minutes").add(stopTimeSeconds, "seconds");
        return tripOriginMoment.toDate();
    }
    /**
     * Get a `Date` object corresponding to a given GTFS stop time and a given start of the trip service day
     *
     * @param stopTimeInSeconds GTFS stop time in seconds. The time is measured from "noon minus 12h" of the service day
     * (effectively midnight except for days on which daylight savings time changes occur). For times occurring after midnight on
     * the service day, enter the time as a value greater than 24:00:00 in HH:MM:SS. See also
     * <https://gtfs.org/schedule/reference/#:~:text=A%20phone%20number.-,Time,-%2D%20Time%20in%20the>.
     * @param startDayTimestamp Unix timestamp of the local time start of the service day (that is, the midnight before the trip
     * starts) **in milliseconds**. If not provided, the start of the current day is used.
     */
    static getStopDateTimeForDayStart(stopTimeInSeconds, startDayTimestamp) {
        let stopTimeMoment;
        if (startDayTimestamp) {
            stopTimeMoment = (0, moment_timezone_1.default)(startDayTimestamp).tz(this.TIMEZONE);
        }
        else {
            stopTimeMoment = (0, moment_timezone_1.default)().tz(this.TIMEZONE).startOf("day");
        }
        stopTimeMoment.hours(12).minutes(0).seconds(0).milliseconds(0);
        stopTimeMoment.subtract(12, "hours");
        stopTimeMoment.add(stopTimeInSeconds, "seconds");
        return stopTimeMoment.toDate();
    }
    /**
     * Get a localized, time-zone aware stop time formatted to `HH:mm:ss`
     *
     * @param stopTime The stop time to format
     */
    static formatStopTime(stopTime) {
        return this.stopTimeFormatter.format(stopTime);
    }
    /**
     * Get hours, minutes, and seconds from a given time
     *
     * @param time The time to get the segments from. Either a number in seconds, or a string in the HH:MM:SS format
     * (H:MM:SS is also accepted)
     */
    static getTimeSegments(time) {
        if (typeof time === "string") {
            const [hours, minutes, seconds] = time.split(":").map((e) => parseInt(e, 10));
            return { hours, minutes, seconds };
        }
        const hours = Math.floor(time / 60 ** 2);
        time %= 60 ** 2;
        const minutes = Math.floor(time / 60);
        const seconds = time % 60;
        return { hours, minutes, seconds };
    }
}
exports.DateTimeUtils = DateTimeUtils;
_a = DateTimeUtils;
DateTimeUtils.LOCALE = "cs-CZ";
DateTimeUtils.TIMEZONE = "Europe/Prague";
// ISO 8601, no fractional seconds (same as moment.defaultFormat)
DateTimeUtils.FORMAT_DATE_TIME = "yyyy-LL-dd'T'HH:mm:ssZZ";
DateTimeUtils.FORMAT_TIME = "HH:mm:ss";
DateTimeUtils.stopTimeFormatter = new Intl.DateTimeFormat(_a.LOCALE, {
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit",
    timeZone: _a.TIMEZONE,
});
//# sourceMappingURL=DateTimeUtils.js.map