import type { ReactNode } from 'react'
import { Plus, Pencil, Trash2, Link2Off } from 'lucide-react'
import { useTripStore } from '../../store/tripStore'
import { useSettingsStore } from '../../store/settingsStore'
import { useTranslation } from '../../i18n'
import CustomSelect from '../shared/CustomSelect'
import { Tooltip } from '../shared/Tooltip'
import { fs } from '../shared/DialogShell'
import { formatMoney } from '../../utils/formatters'
import { catMeta } from '../Budget/costsCategories'
import { useExpenseLinks } from './useRecordLinks'
import type { BudgetItem } from '../../types'
/**
* The Costs block inside a booking modal — and, since #1298, inside the place
* form, which links its expenses the same way. Replaces the old inline price +
* budget category fields.
*
* A booking or place can carry several expenses (the flight, then the upgrade
* bought for it, #2084). Each is listed with edit, unlink (the expense stays in
* Costs) and delete. Under them, "create expense" makes a new one (the modal
* saves its own record first, then opens the full Costs editor), and once the
* record exists an expense already in Costs can be linked to it from a list.
*
* Exactly one of reservationId / placeId is set — they are the two sides of the
* same link, and the block behaves identically on both.
*/
export function BookingCostsSection({ reservationId, placeId = null, hintKey = 'reservations.createExpenseHint', pendingExpense, onCreate, onEdit, onRemove, labelClassName, customTooltips = false, whiteButtons = false }: {
reservationId: number | null
/** Set instead of reservationId when the block sits in the place form (#1298). */
placeId?: number | null
/** The create button's hint: what gets saved before the editor opens, "the booking" or "the place". */
hintKey?: string
/** A cost parsed from an import that will be linked on save — previewed before the booking exists. */
pendingExpense?: { total_price: number; currency?: string | null; category: string } | null
onCreate: () => void
onEdit: (item: BudgetItem) => void
onRemove: (item: BudgetItem) => void
/** Replaces the block's own label style, so a host form can match its other fields. */
labelClassName?: string
/** Shows the row buttons' hints in the shared Tooltip instead of the native title. */
customTooltips?: boolean
/**
* Draws the create button and the expense rows white on a hairline, the look
* of the dialog frame's secondary buttons, instead of the grey fill.
*/
whiteButtons?: boolean
}) {
const { t, locale } = useTranslation()
const tripCurrency = useTripStore(s => s.trip?.currency)
const displayCurrency = useSettingsStore(s => s.settings.default_currency)
const base = (displayCurrency || tripCurrency || 'EUR').toUpperCase()
// An amount is printed in its own currency, unconverted. One saved without a
// currency is in the trip's own (#2525), which is how Costs reads it; labelling
// it with the display currency turned a 120 EUR deposit into $120.00.
const ownCurrency = (currency: string | null | undefined) => currency || tripCurrency || base
const { targetId, linked, unlinked, link, unlink } = useExpenseLinks(reservationId, placeId)
const labelCls = labelClassName ?? 'block text-[11px] font-semibold uppercase tracking-[0.08em] text-content-faint mb-[6px]'
const rowCls = whiteButtons ? 'bg-surface-card border border-edge-faint' : 'bg-surface-secondary border border-edge'
const rowStyle = { display: 'flex', alignItems: 'center', gap: 10, padding: '10px 12px', borderRadius: 10 } as const
// White, the row's buttons trade the border for the hairline ring and shadow of
// the create button under them; the border stays, transparent, to keep their size.
const iconButton = whiteButtons
? 'border border-transparent bg-surface-card text-content-muted shadow-sm ring-1 ring-edge-faint transition-colors hover:bg-surface-hover hover:text-content'
: 'text-content-muted border border-edge bg-surface-card transition-colors hover:bg-surface-tertiary hover:text-content'
const deleteButton = whiteButtons
? 'border border-transparent bg-surface-card text-content-muted shadow-sm ring-1 ring-edge-faint transition-colors hover:bg-danger-soft hover:text-danger'
: 'text-content-muted border border-edge bg-surface-card transition-colors hover:bg-danger-soft hover:text-danger'
const iconButtonStyle = { display: 'inline-flex', padding: 7, borderRadius: 8, cursor: 'pointer' } as const
const rowButton = (label: string, className: string, onClick: () => void, icon: ReactNode) => {
const button = (
)
return customTooltips ?