import { computed } from 'vue';
import { usePage } from '@inertiajs/vue3';
import { formatInTimeZone, zonedTimeToUtcIso, nowInTimeZone } from '@/utils/timezone';

// One shared source for every page that displays or picks a date/time, so
// they all read the tenant's configured timezone (Settings > General
// Settings) the same way instead of each re-implementing it. Backs onto
// resources/js/utils/timezone.js for the actual conversion/formatting math.
export function useTenantTimezone() {
    const timezone = computed(() => usePage().props.tenantTimezone || 'UTC');

    function formatDate(iso) {
        return formatInTimeZone(iso, timezone.value, { hour: undefined, minute: undefined, hour12: undefined });
    }

    function formatDateTime(iso) {
        return formatInTimeZone(iso, timezone.value);
    }

    function formatTime(iso) {
        return formatInTimeZone(iso, timezone.value, { month: undefined, day: undefined, year: undefined });
    }

    function toUtcIso(localValue) {
        return zonedTimeToUtcIso(localValue, timezone.value);
    }

    function now() {
        return nowInTimeZone(timezone.value);
    }

    return { timezone, formatDate, formatDateTime, formatTime, toUtcIso, now };
}
