import { parsePlaceHours } from '@trek/shared'; import type { Accommodation, Assignment, Category, Day, DayNote, Place, Reservation } from '../../types'; import { getDisplayTimeForDay, getSpanPhase } from '../../utils/dayMerge'; import { getFlightLegs, getTrainLegs, usesStationRoute } from '../../utils/flightLegs'; import { formatMoney, splitReservationDateTime } from '../../utils/formatters'; import type { TransportLegDetail } from '../Planner/transportDetailModel'; import { escHtml, hexColour, safeImg, shortDate, websiteHost } from './tripPdfHtml'; import { bookingIcon, bookingTint, categoryIcon, glyph, noteIcon } from './tripPdfIcons'; /* * One day of the trip PDF: its header, where the night is spent, and the day's plan * as a timeline, times on the left, a rail in the day's colour, and a card for every * stop, booking and note. */ type Translate = (key: string, params?: Record) => string; /** A booking as the day merge hands it over, a leg of a multi-leg booking included. */ export type PdfBooking = Reservation & { __leg?: TransportLegDetail & { total?: number } }; /** What the day's markup needs from the export around it. */ export interface PdfDayContext { tr: Translate; fmtTime: (value?: string | null) => string; locale: string | undefined; tripCurrency: string; categories: readonly Category[]; /** The trip's full places by id: an assignment carries only a projection of its place. */ placesById: ReadonlyMap; /** The picture to print for a place, already shrunk where it could be; null for none. */ photoOf: (place: Assignment['place']) => string | null; /** A place's description or notes as sanitized markdown, laid out as an info row. */ markdown: (text: string) => string; /** A note's markdown as sanitized HTML, for a card that brings its own box. */ markdownHtml: (text: string) => string; } const NEUTRAL = '#6b6b75'; function metadataOf(r: PdfBooking): Record { if (!r.metadata) return {}; try { const parsed: unknown = typeof r.metadata === 'string' ? JSON.parse(r.metadata) : r.metadata; return parsed && typeof parsed === 'object' ? (parsed as Record) : {}; } catch { return {}; } } const text = (value: unknown): string => (typeof value === 'string' || typeof value === 'number' ? String(value) : ''); /** "1 h 30 min", or null for no duration at all. */ function durationLabel(minutes: number | null | undefined): string | null { if (typeof minutes !== 'number' || !Number.isFinite(minutes) || minutes <= 0) return null; const whole = Math.round(minutes); const h = Math.floor(whole / 60); const m = whole % 60; if (h === 0) return `${m} min`; return m === 0 ? `${h} h` : `${h} h ${m} min`; } function timeColumn(start: string, end: string): string { return `
${start ? `${escHtml(start)}` : ''}${ end ? `${escHtml(end)}` : '' }
`; } function item(kind: string, time: string, marker: string, body: string, style = ''): string { return `
  • ${time}
    ${marker}
    ${body}
  • `; } // ── Bookings ────────────────────────────────────────────────────────────── /** "Departure", "Pick-up" and the like, for a booking that spans days; null on its single day. */ function spanLabel(r: PdfBooking, phase: string, tr: Translate): string | null { if (phase === 'single') return null; // Each key spelled out, so the key check can follow every branch to its string. const start = phase === 'start'; const end = phase === 'end'; if (r.type === 'flight') return tr(`reservations.span.${start ? 'departure' : end ? 'arrival' : 'inTransit'}`); if (r.type === 'car') return tr(`reservations.span.${start ? 'pickup' : end ? 'return' : 'active'}`); // Parking reuses span.pickup for its end day: you drop the car off and collect it // again later, so the rental car's wording carries over without a second key. if (r.type === 'parking') return tr(`reservations.span.${start ? 'dropOff' : end ? 'pickup' : 'ongoing'}`); return tr(`reservations.span.${start ? 'start' : end ? 'end' : 'ongoing'}`); } function stopsOf(r: PdfBooking, label: 'code' | 'name'): string[] { return (r.endpoints || []) .slice() .sort((a, b) => (a.sequence ?? 0) - (b.sequence ?? 0)) .map((e) => (label === 'code' ? e.code || e.name : e.name) || '') .filter(Boolean); } /** One line under a booking's title: the way it goes, its facts as badges and its own booking code. */ interface BookingLine { route: string; facts: string[]; code: string; /** A table's party size, printed with the people icon so it needs no word. */ people?: string; } /** * The lines under a booking's title. A multi-leg flight or train gets one per leg, each * with its own number and booking code, since that is the code the gate asks for * (#1943); a leg row prints its own leg; everything else gets one line or none. */ function bookingLines(r: PdfBooking, tr: Translate): BookingLine[] { const meta = metadataOf(r); const route = (from?: string | null, to?: string | null) => from || to ? [from, to].filter(Boolean).join(' → ') : ''; const seat = (value: unknown) => (text(value) ? `${tr('reservations.meta.seat')} ${text(value)}` : ''); const platform = (value: unknown) => (text(value) ? `${tr('reservations.meta.platform')} ${text(value)}` : ''); const line = (way: string, facts: unknown[], code: unknown = ''): BookingLine => ({ route: way, facts: facts.map(text).filter(Boolean), code: text(code), }); const filled = (lines: BookingLine[]) => lines.filter((l) => l.route || l.facts.length || l.code); if (r.__leg) { const l = r.__leg; const source = (usesStationRoute(r.type) ? getTrainLegs(r) : getFlightLegs(r))[l.index]; return filled([ line( route(l.from, l.to), [l.airline, l.flight_number, l.train_number, platform(l.platform), seat(l.seat)], source?.confirmation_number ), ]); } if (r.type === 'flight') { const legs = getFlightLegs(r); if (legs.length > 1) { return filled( legs.map((l) => line(route(l.from, l.to), [l.airline, l.flight_number, seat(l.seat)], l.confirmation_number)) ); } const stops = stopsOf(r, 'code'); const way = stops.length >= 2 ? stops.join(' → ') : route(text(meta.departure_airport), text(meta.arrival_airport)); return filled([line(way, [meta.airline, meta.flight_number, seat(meta.seat)])]); } if (r.type === 'train') { const legs = getTrainLegs(r); if (legs.length > 1) { return filled( legs.map((l) => line(route(l.from, l.to), [l.train_number, platform(l.platform), seat(l.seat)], l.confirmation_number) ) ); } const stops = stopsOf(r, 'code'); return filled([ line(stops.length >= 2 ? stops.join(' → ') : '', [meta.train_number, platform(meta.platform), seat(meta.seat)]), ]); } if (r.type === 'car') { // A rental with stops is a drive, and the printout is what people take into the car // (#1797). Consecutive repeats collapse, and a chain of one place is no route: the // ordinary rental is picked up and returned at the same desk. const stops = stopsOf(r, 'name').filter((name, i, all) => name !== all[i - 1]); return new Set(stops).size >= 2 ? [line(stops.join(' → '), [])] : []; } if (r.type === 'restaurant') return text(meta.party_size) ? [{ ...line('', []), people: text(meta.party_size) }] : []; if (r.type === 'event') return filled([line('', [meta.venue])]); if (r.type === 'tour') return filled([line('', [meta.operator])]); return []; } function bookingLineHtml(l: BookingLine): string { return `
    ${l.route ? `${escHtml(l.route)}` : ''}${ l.people ? `${glyph('people', 9)}${escHtml(l.people)}` : '' }${l.facts .map((fact) => `${escHtml(fact)}`) .join('')}${l.code ? `${glyph('key', 9)}${escHtml(l.code)}` : ''}
    `; } /** A booking's lines on their own, for the booking list of the overview. */ export function bookingLinesHtml(r: PdfBooking, tr: Translate): string { return bookingLines(r, tr).map(bookingLineHtml).join(''); } /** A booking on the day's timeline. Sets `flags.transportNote` when it prints a note. */ export function bookingItem(r: PdfBooking, day: Day, ctx: PdfDayContext, flags: { transportNote: boolean }): string { const { tr, fmtTime } = ctx; const phase = getSpanPhase(r, day.id); const span = spanLabel(r, phase, tr); // Start and end the way the day plan draws them (#1310). The landing time only on a // booking that begins and ends this day: across a span the arrival belongs to the // arrival day, which shows it as its own time. const start = fmtTime(splitReservationDateTime(getDisplayTimeForDay(r, day.id)).time); const end = phase === 'single' ? fmtTime(splitReservationDateTime(r.reservation_end_time).time) : ''; const note = typeof r.notes === 'string' ? r.notes.trim() : ''; if (note) flags.transportNote = true; const meta = metadataOf(r); const location = r.location || text(meta.location); const status = r.status === 'confirmed' ? tr('reservations.confirmed') : r.status === 'pending' ? tr('reservations.pending') : ''; const tint = hexColour(bookingTint(r.type), NEUTRAL); const extras = [ r.confirmation_number ? `${glyph('key', 9)}${escHtml(r.confirmation_number)}` : '', status ? `${escHtml(status)}` : '', ].filter(Boolean); const card = `
    ${bookingIcon(r.type)}
    ${span ? `
    ${escHtml(span)}
    ` : ''}
    ${escHtml(r.title)}
    ${bookingLines(r, tr).map(bookingLineHtml).join('')} ${location ? `
    ${escHtml(location)}
    ` : ''} ${extras.length ? `
    ${extras.join('')}
    ` : ''} ${note ? `
    ${ctx.markdownHtml(note)}
    ` : ''}
    `; return item('booking', timeColumn(start, end), bookingIcon(r.type, 11), card, `--kind:${tint}`); } // ── Notes ───────────────────────────────────────────────────────────────── /** * A day note. A coloured note carries its colour as the rule along its edge and its * icon (#1629), not as a fill: a tint costs ink and reads as grey on a mono printer. */ export function noteItem(note: DayNote, ctx: PdfDayContext): string { const colour = note.color ? hexColour(note.color, '') : ''; // A note's `time` is the free line the day plan prints under its text, not a clock, // so it stays in the card and the time column stays empty. The planner renders it as // markdown, so the card does too (#3236). const subtitle = typeof note.time === 'string' ? note.time.trim() : ''; const card = `
    ${noteIcon(note.icon)}
    ${escHtml(note.text)}
    ${ subtitle ? `
    ${ctx.markdownHtml(subtitle)}
    ` : '' }
    `; return item('note', timeColumn('', ''), '', card); } // ── Places ──────────────────────────────────────────────────────────────── /** The day's opening hours of a place, Monday first like the stored week; null when unknown. */ function hoursOnDay(place: Partial, day: Day, ctx: PdfDayContext): string | null { const week = parsePlaceHours(place.opening_hours); if (!week || !day.date) return null; const weekday = (new Date(day.date + 'T00:00:00Z').getUTCDay() + 6) % 7; const hours = week[weekday]; if (!hours) return null; if (hours.closed) return ctx.tr('inspector.closed'); if (!hours.open && !hours.close) return null; return [ctx.fmtTime(hours.open), ctx.fmtTime(hours.close)].filter(Boolean).join(' – '); } function placeFacts(place: Assignment['place'] & Partial, ctx: PdfDayContext): string[] { const facts: string[] = []; const duration = durationLabel(place.duration_minutes); if (duration) facts.push(`${glyph('duration', 9)}${escHtml(duration)}`); const price = Number(place.price); if (Number.isFinite(price) && price > 0) { facts.push( `${glyph('price', 9)}${escHtml(formatMoney(price, place.currency || ctx.tripCurrency, ctx.locale || 'en'))}` ); } if (typeof place.rating_avg === 'number' && place.rating_avg > 0) { const count = place.rating_count ? ` (${place.rating_count})` : ''; facts.push(`${glyph('star', 9)}${place.rating_avg.toFixed(1)}${count}`); } for (const tag of place.tags || []) { if (!tag.name) continue; facts.push( `${escHtml(tag.name)}` ); } return facts; } function placeDetails( place: Assignment['place'] & Partial, assignment: Assignment, day: Day, ctx: PdfDayContext ): string[] { const details: string[] = []; const hours = hoursOnDay(place, day, ctx); if (hours) details.push( `${glyph('hours', 9)}${escHtml(ctx.tr('inspector.openingHours'))} ${escHtml(hours)}` ); if (place.phone) details.push(`${glyph('phone', 9)}${escHtml(place.phone)}`); if (place.website) details.push(`${glyph('web', 9)}${escHtml(websiteHost(place.website))}`); if (place.email) details.push(`${glyph('mail', 9)}${escHtml(place.email)}`); const people = (assignment.participants || []).map((p) => p.username).filter(Boolean); if (people.length) { details.push( `${glyph('people', 9)}${escHtml(ctx.tr('inspector.participants'))}: ${escHtml(people.join(', '))}` ); } return details; } /** A planned place on the day's timeline, numbered like the stops on the map. */ export function placeItem(assignment: Assignment, number: number, day: Day, ctx: PdfDayContext): string { // The assignment carries a projection of its place; the rating, the e-mail, the // hours and the reservation note are only on the trip's own place. const place: Assignment['place'] & Partial = { ...ctx.placesById.get(assignment.place.id), ...assignment.place, }; const category = ctx.categories.find((c) => c.id === place.category_id); const catColour = hexColour(category?.color, NEUTRAL); const photo = ctx.photoOf(assignment.place); const media = photo ? `` : `
    ${categoryIcon(category?.icon)}
    `; const facts = placeFacts(place, ctx); const details = placeDetails(place, assignment, day, ctx); const hasPosition = place.lat != null && place.lng != null; const notes = [place.notes, place.reservation_notes, assignment.notes].filter( (value): value is string => typeof value === 'string' && value.trim() !== '' ); const card = `
    ${media}
    ${escHtml(place.name)} ${category ? `${escHtml(category.name)}` : ''}
    ${facts.length ? `
    ${facts.join('')}
    ` : ''} ${place.address ? `
    ${glyph('pin')}${escHtml(place.address)}
    ` : ''} ${ hasPosition ? `
    ${glyph('coords')}${Number(place.lat).toFixed(5)}, ${Number(place.lng).toFixed(5)}
    ` : '' } ${place.description ? `
    ${ctx.markdown(place.description)}
    ` : ''} ${notes.map((note) => `
    ${ctx.markdown(note)}
    `).join('')} ${details.length ? `
    ${details.join('')}
    ` : ''}
    `; const start = ctx.fmtTime(place.place_time); const end = ctx.fmtTime(place.end_time); return item('place', timeColumn(start, end), String(number), card); } // ── Stays ───────────────────────────────────────────────────────────────── /** * Where the day's night is spent. The day of arrival and the day of leaving get the * full card with the time, the address and the booking code; the nights in between a * quiet line, since the same hotel on every page is the plan's least news. */ export function staysBlock(stays: readonly Accommodation[], day: Day, ctx: PdfDayContext): string { if (!stays.length) return ''; const cards = stays.map((stay) => { const isCheckIn = day.id === stay.start_day_id; const isCheckOut = day.id === stay.end_day_id; const name = stay.place_name || stay.reservation_title || ''; const photo = safeImg(stay.place_image); const media = `${photo ? `` : glyph(isCheckOut ? 'checkOut' : isCheckIn ? 'checkIn' : 'bed', 16)}`; if (!isCheckIn && !isCheckOut) { return `
    ${media}
    ${escHtml(ctx.tr('reservations.meta.linkAccommodation'))}
    ${escHtml(name)}
    `; } const label = ctx.tr(isCheckIn ? 'reservations.meta.checkIn' : 'reservations.meta.checkOut'); const time = ctx.fmtTime(isCheckIn ? stay.check_in : stay.check_out); const extras = [ isCheckIn && stay.confirmation ? `${glyph('key', 9)}${escHtml(stay.confirmation)}` : '', ].filter(Boolean); return `
    ${media}
    ${escHtml(label)}${time ? ` ${escHtml(time)}` : ''}
    ${escHtml(name)}
    ${stay.place_address ? `
    ${escHtml(stay.place_address)}
    ` : ''} ${stay.notes ? `
    ${escHtml(stay.notes)}
    ` : ''} ${extras.length ? `
    ${extras.join('')}
    ` : ''}
    `; }); return `
    ${cards.join('')}
    `; } // ── Header ──────────────────────────────────────────────────────────────── interface DayHeaderFacts { stops: number; distance: string | null; cost: string | null; } /** The day's header: its number in the day's colour, the date, the title and its figures. */ export function dayHeader(day: Day, facts: DayHeaderFacts, ctx: PdfDayContext): string { const n = day.day_number ?? 0; const pills = [ facts.stops > 0 ? `${glyph('pin', 9)}${facts.stops}` : '', facts.distance ? `${glyph('route', 9)}${escHtml(facts.distance)}` : '', facts.cost ? `${glyph('price', 9)}${facts.cost}` : '', ].filter(Boolean); return `
    ${escHtml(ctx.tr('pdf.day'))}${n}
    ${day.date ? `
    ${escHtml(shortDate(day.date, ctx.locale))}
    ` : ''}
    ${escHtml(day.title || ctx.tr('dayplan.dayN', { n }))}
    ${pills.length ? `
    ${pills.join('')}
    ` : ''}
    `; } // ── Photo strip ─────────────────────────────────────────────────────────── interface StripPhoto { url: string; caption: string; } /** * The strip that opens a day: up to three of its photographed stops side by side, the * first one widest, each named on a frosted label. Nothing for a day without photos. */ export function photoStrip(photos: readonly StripPhoto[]): string { if (!photos.length) return ''; return `
    ${photos .map( (p) => `
    ${escHtml(p.caption)}
    ` ) .join('')}
    `; }