// Lalaluxe booking — booking/ui/choices.jsx (load before app.jsx — see docs/architecture.md, "The no-build multi-file Babel contract")
// What she has picked and what she picks from, drawn on more than one screen: the running tray in the dock
// (PicksTray), the painted length picker (LengthStage) and the selectable extra row (AddonRow).

function PicksTray({ s, cfg }) {
	const items = priceItems(s);
	// A service priced by length shows its own price until a length is picked — "from", since the
	// length adds to it.
	const tiers = s.visit !== "subscription" && (s.who === "nails" || s.who === "both") && s.service && cfg.base[s.service];
	const pending = !!tiers && tiers.flat == null && !s.length;
	const labels = items.map((it) => it.label);
	const t = total(s);
	const from = pending;
	const shown = Math.round(useSpringValue(t, 260, 34));
	const minutes = totalDuration(s);
	if (labels.length === 0) return null;
	return (
		<div className="tray">
			<div className="tray-copy">
				<span className="tray-label">
					{s.visit === "subscription" ? "Your Plan" : "Your Set"}
					{minutes > 0 ? <> · about {humanDuration(minutes)}</> : " So Far"}
				</span>
				<span className="tray-items">
					{labels.length > 3 ? `${labels.slice(0, 2).join(" · ")} + ${labels.length - 2} more` : labels.join(" · ")}
				</span>
			</div>
			{/* counts in whole dollars while it moves, then settles on the total, to the cent when it has cents */}
			<span className="tray-total" aria-label={(from ? "from " : "") + money(t)}>
				{from && <small>from</small>}{money(Math.abs(shown - t) < 1 ? t : shown)}
			</span>
		</div>
	);
}

// Shared painted stage for the nail-length + subscription-plan pickers (length.jsx, plan.jsx).
// Owns the one free-edge map so the two screens can't drift. priceFor(L) returns the
// fully-formatted "$N" string; caption is the resolved label for the current value
// (captionPlaceholder shows until something is picked).
function LengthStage({ value, caption, captionPlaceholder, priceFor, onPick, labels, options }) {
	// the lengths in the studio's order (Menu → Nails → Lengths), shortest first
	const lens = options;
	const exts = lengthExts(lens);
	const extOf = (L) => exts[lens.indexOf(L)] ?? 6;
	const ext = useSpringValue(value ? extOf(value) : 6);
	return (
		<div className="length-stage">
			<div className="length-vis">
				<HandStage
					ext={ext}
					marks={lens.map((L) => ({ id: L, ext: extOf(L), label: labels[L], on: value === L }))}
				/>
			</div>

			<p className="length-cap" aria-live="polite">{value ? caption : captionPlaceholder}</p>

			<div className="length-buttons" style={{ gridTemplateColumns: `repeat(${lens.length}, minmax(0, 1fr))` }}>
				{lens.map((L) => (
					<button
						key={L}
						className={"lbtn " + (value === L ? "sel" : "")}
						aria-pressed={value === L}
						onClick={() => onPick(L)}
					>
						<span className="lname">{labels[L]}</span>
						<span className={"lprice" + (priceFor(L) === "Included" ? " free" : "")}>{priceFor(L)}</span>
					</button>
				))}
			</div>
		</div>
	);
}

// How long each length's free edge is drawn, in HandStage viewBox units, for lengths in the order
// they are shown. The five the studio started with keep the lengths they were drawn at while they
// are shown shortest first; any other set (a length the studio added, or its own order) is spread
// evenly from the shortest to the longest, so no two are drawn at one height.
const LENGTH_EXT = { short: 6, medium: 22, long: 42, xl: 62, xxl: 84 };
function lengthExts(lens) {
	const drawn = lens.map((L) => LENGTH_EXT[L]);
	if (drawn.every((v, i) => v != null && (i === 0 || v > drawn[i - 1]))) return drawn;
	return lens.map((_, i) => 6 + (78 * i) / Math.max(1, lens.length - 1));
}

// Selectable extra: arrival prep on the base screen (base.jsx) and plan extras (subaddons.jsx,
// with the locked included "✓" state). item carries the already-formatted price string;
// `included` = a non-toggleable perk. Callers compute `on`/`included` and pass an onToggle thunk.
function AddonRow({ item, on, included, onToggle }) {
	return (
		<button
			className={("addbtn addon-row " + (included ? "included" : on ? "sel" : "")).trim()}
			disabled={included || undefined}
			aria-pressed={included ? undefined : on}
			onClick={() => {
				if (!included) onToggle();
			}}
		>
			<span className="addon-copy">
				<span className="addon-name">{item.name}</span>
				<span className="addon-desc">{item.desc}</span>
			</span>
			<span className={"ap" + (item.price === "Included" ? " free" : "")}>{item.price}</span>
			<span className="plus">
				<Ico name={included || on ? "check" : "plus"} />
			</span>
		</button>
	);
}
