"use client";

import { locationHref } from "@/lib/helpers";

/**
 * An address that opens a map.
 *
 * Three detail screens rendered this as a `<p>` or a `<button>` with a
 * `window.open` handler. A `<p onClick>` is not reachable by keyboard at all;
 * the `<button>` was, but it is still announced as a button when the thing it
 * does is navigate, and it cannot be middle-clicked or opened in a new tab.
 * Both are now one `<a href target="_blank">`, which is what the interaction
 * actually is.
 *
 * When the record carries neither a link nor coordinates there is nothing to
 * open, so the address renders as plain text — previously it still looked
 * clickable and the handler returned without doing anything.
 *
 * `rel="noopener noreferrer"`: `noopener` matches what the imperative version
 * passed to `window.open`, and `noreferrer` is added because the destination is
 * a third-party map and the referrer would leak the Fursa page the visitor was
 * reading.
 */
export default function LocationLink({
  address,
  locationUrl,
  latitude,
  longitude,
  className,
}: {
  /** Already localized by the caller — only it knows which field to pick. */
  address: string;
  locationUrl?: string | null;
  latitude?: number | string | null;
  longitude?: number | string | null;
  className?: string;
}) {
  const href = locationHref(locationUrl, latitude, longitude);

  if (!href) {
    return (
      <span className={className} title={address}>
        {address}
      </span>
    );
  }

  return (
    <a
      href={href}
      target="_blank"
      rel="noopener noreferrer"
      className={className}
      title={address}
    >
      {address}
    </a>
  );
}
