"use client";

/**
 * Place-of-Delivery dropdown (issue #19) — a native <select name="placeOfDelivery">
 * sourced from the admin-managed delivery locations. Plain HTML so it works with
 * the forms' native FormData submission (no client state needed).
 *
 * `options` are the formatted "Company — address" strings (also the stored value).
 * `current` is the PO's existing place-of-delivery; if it isn't one of the active
 * options (legacy / imported / a since-removed location) it is preserved as a
 * leading "(current)" option so an edit never silently drops it.
 */
export function DeliveryLocationField({
  options,
  current,
  className,
}: {
  options: string[];
  current?: string | null;
  className?: string;
}) {
  const cur = (current ?? "").trim();
  const currentMissing = cur.length > 0 && !options.includes(cur);

  return (
    <select name="placeOfDelivery" defaultValue={cur} className={className}>
      <option value="">— Select a delivery location —</option>
      {currentMissing && <option value={cur}>{cur} (current)</option>}
      {options.map((o) => (
        <option key={o} value={o}>
          {o}
        </option>
      ))}
    </select>
  );
}
