"use client";

import type { ReactNode } from "react";
import { useTranslation } from "@enode/core/translations/hooks";
import { Button } from "./button";

type FloatingConfirmProps = {
  onClick: () => void;
  /**
   * The button text. Defaults to "Confirm", or "Confirm ({count})" when
   * `count` is above 0. Pass an already translated label for other verbs
   * ("Select workout", "Save a copy").
   */
  label?: ReactNode;
  /** Number of picked items, shown in the default label. Ignored with `label`. */
  count?: number;
  disabled?: boolean;
  /** An in-flight confirm: blocks taps and sets `aria-busy` (see `Button`). */
  busy?: boolean;
  /**
   * Lifts the pill by 4.5rem, e.g. to clear a bottom bar or tray of the
   * picker that would otherwise sit underneath it.
   */
  raised?: boolean;
};

/**
 * The floating primary pill at the bottom-right of a picker drawer view —
 * the confirm of a selection list where a full-width footer bar would cover
 * rows.
 *
 * @remarks
 * Positioned `absolute` at the bottom of the nearest positioned ancestor
 * (a `DrawerView` root), above the content, with the bottom safe-area inset
 * added. The full-width wrapper is `pointer-events-none` so the list behind
 * it stays scrollable and tappable beside the pill; only the button takes
 * pointer events. For a form's Save / Cancel row use `DrawerFooter` instead.
 */
export function FloatingConfirm({
  onClick,
  label,
  count = 0,
  disabled,
  busy,
  raised = false,
}: FloatingConfirmProps) {
  const { t } = useTranslation();
  const text =
    label ?? (count > 0 ? t("Confirm ({count})", { count }) : t("Confirm"));
  return (
    <div
      className={`pointer-events-none absolute inset-x-0 bottom-0 z-40 flex justify-end px-6 ${
        raised
          ? "pb-[calc(6rem+env(safe-area-inset-bottom))]"
          : "pb-[calc(1.5rem+env(safe-area-inset-bottom))]"
      }`}
    >
      <Button
        variant="primary"
        size="rounded"
        onClick={onClick}
        disabled={disabled}
        busy={busy}
        className="pointer-events-auto shadow-floating"
      >
        {text}
      </Button>
    </div>
  );
}
