2026-09-04 19:06:19 +02:00
|
|
|
|
import type { Availability, DayAvailability } from '../api/types.js';
|
|
|
|
|
|
|
|
|
|
|
|
export const DAY_NAMES = ['Pon', 'Wt', 'Śr', 'Czw', 'Pt', 'Sob', 'Niedz'] as const;
|
|
|
|
|
|
|
|
|
|
|
|
/** 30-minute slots from 05:00 to 23:00. */
|
|
|
|
|
|
export const TIME_SLOTS: string[] = (() => {
|
|
|
|
|
|
const slots: string[] = [];
|
|
|
|
|
|
for (let h = 5; h <= 23; h++) {
|
|
|
|
|
|
slots.push(`${String(h).padStart(2, '0')}:00`);
|
|
|
|
|
|
if (h < 23) slots.push(`${String(h).padStart(2, '0')}:30`);
|
|
|
|
|
|
}
|
|
|
|
|
|
return slots;
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
export function emptyAvailability(): Availability {
|
|
|
|
|
|
return [null, null, null, null, null, null, null];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Coerce any stored value into a 7-element Availability (index 0=Mon ... 6=Sun). */
|
|
|
|
|
|
export function normalizeAvailability(value: unknown): Availability {
|
|
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
|
|
const arr: Availability = [];
|
|
|
|
|
|
for (let i = 0; i < 7; i++) {
|
|
|
|
|
|
const item = value[i];
|
|
|
|
|
|
arr.push(item && typeof item === 'object' ? (item as DayAvailability) : null);
|
|
|
|
|
|
}
|
|
|
|
|
|
return arr;
|
|
|
|
|
|
}
|
|
|
|
|
|
return emptyAvailability();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Human-readable summary, e.g. "Pon 18:00–20:00, Wt 18:00–20:00". Empty string if none. */
|
|
|
|
|
|
export function formatAvailability(value: unknown): string {
|
|
|
|
|
|
const arr = normalizeAvailability(value);
|
|
|
|
|
|
const parts: string[] = [];
|
|
|
|
|
|
arr.forEach((day, i) => {
|
|
|
|
|
|
if (day) parts.push(`${DAY_NAMES[i]} ${day.start}–${day.end}`);
|
|
|
|
|
|
});
|
|
|
|
|
|
return parts.join(', ');
|
|
|
|
|
|
}
|
2026-10-14 04:18:01 +02:00
|
|
|
|
|
|
|
|
|
|
/** "18:00" -> "18", "21:30" -> "21:30" — forma skrócona do siatki tygodnia. */
|
|
|
|
|
|
export function shortTime(t: string): string {
|
|
|
|
|
|
const [h, m] = t.split(':');
|
|
|
|
|
|
return m === '00' ? h : t;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Zwarty zakres dnia do siatki tygodnia, np. "18–21:30". */
|
|
|
|
|
|
export function formatDayRange(day: DayAvailability): string {
|
|
|
|
|
|
return `${shortTime(day.start)}–${shortTime(day.end)}`;
|
|
|
|
|
|
}
|