// Pure helpers for the Encaisser dialog's reservation picker.
//
// The picker (SearchableSelect) filters client-side over `name +
// internal_name + searchValue`. A reservation's `name` is only
// "#contract — client", so without a populated `searchValue` the user
// cannot find a reservation by its PROPERTY ("Dar Sophia"), place, or
// dates — typing them returns "Aucun résultat" even though the row is
// loaded. `buildReservationSearchValue` folds that text into searchValue.

/** French-style date formatter: 2026-05-20 -> 20/05/2026. */
export function frDate(iso: string | null | undefined): string {
    if (!iso) return '';
    const [y, m, d] = iso.slice(0, 10).split('-');
    if (!y || !m || !d) return iso;
    return `${d}/${m}/${y}`;
}

/** Minimal shape needed to build the searchable text for a reservation. */
export interface ReservationSearchInput {
    name: string;
    property_name?: string | null;
    date_from?: string | null;
    date_to?: string | null;
}

/**
 * Build the extra text folded into SearchableSelect's filter haystack so a
 * reservation can be matched by property name and dates (both ISO and the
 * French d/m/Y the user actually reads), not just the "#contract — client"
 * label.
 */
export function buildReservationSearchValue(item: ReservationSearchInput): string {
    return [
        item.name,
        item.property_name,
        item.date_from,
        item.date_to,
        frDate(item.date_from),
        frDate(item.date_to),
    ]
        .filter(Boolean)
        .join(' ');
}
