import { usePage } from '@inertiajs/react';
import {
    DEFAULT_BOOKING_TIMEZONE,
    bookingDateKeyAfterDays,
    bookingDateKeyFromDate,
    bookingLocalDateFromKey,
    bookingTodayKey,
    bookingTimezoneLabel,
    formatBookingDate,
    formatBookingDateKey,
    formatBookingDateTime,
    formatBookingTime,
    formatBookingTimezone,
    toBookingScheduledAt,
} from '@/lib/booking-timezones';

type SharedBookingTimezoneProps = {
    bookingTimezone?: string;
    bookingTimezoneLabel?: string;
};

export function useBookingTimezone(): string {
    const { bookingTimezone } = usePage<SharedBookingTimezoneProps>().props;

    return bookingTimezone ?? DEFAULT_BOOKING_TIMEZONE;
}

export function useBookingTimezoneLabel(): string {
    const timezone = useBookingTimezone();
    const { bookingTimezoneLabel: sharedLabel } =
        usePage<SharedBookingTimezoneProps>().props;

    return sharedLabel ?? bookingTimezoneLabel(timezone);
}

export function useBookingDateTimeFormatters() {
    const timezone = useBookingTimezone();

    return {
        timezone,
        timezoneLabel: bookingTimezoneLabel(timezone),
        formatTimezone: formatBookingTimezone,
        formatDate: (value: string) => formatBookingDate(value, timezone),
        formatTime: (value: string) => formatBookingTime(value, timezone),
        formatDateTime: (value: string) =>
            formatBookingDateTime(value, timezone),
        todayKey: () => bookingTodayKey(timezone),
        dateKeyAfterDays: (days: number) =>
            bookingDateKeyAfterDays(days, timezone),
        toScheduledAt: (dateKey: string, time: string) =>
            toBookingScheduledAt(dateKey, time),
        formatDateKey: (dateKey: string) =>
            formatBookingDateKey(dateKey, timezone),
        localDateFromKey: (dateKey: string) =>
            bookingLocalDateFromKey(dateKey),
        dateKeyFromDate: (date: Date) => bookingDateKeyFromDate(date),
    };
}
