"use client";

import Image from "next/image";
import i18n from "@/lib/i18n/config";
import { cn } from "@/lib/helpers";

const asset = (path: string) => `/assets/${path}`;

/**
 * The two controls that sit inside the date input's right edge: an optional
 * clear button and the calendar icon.
 *
 * Both are absolutely positioned against the input, and both flip sides under
 * RTL — the clear button sits inboard of the icon, which keeps the edge.
 */
export default function DateFieldAdornments({
  clearable,
  displayValue,
  disabled,
  showDueDate,
  isRtl,
  onClear,
  onIconClick,
}: {
  clearable?: boolean;
  displayValue: string;
  disabled?: boolean;
  showDueDate?: boolean;
  isRtl: boolean;
  onClear: () => void;
  onIconClick: () => void;
}) {
  return (
    <>
        {clearable && displayValue && !disabled && (
          <button
            type="button"
            onClick={onClear}
            aria-label={i18n.t("COMMON.CLEAR_DATE")}
            className="absolute text-primary-5/60 hover:text-primary-5"
            // Sits inboard of the calendar icon, which keeps its own edge.
            style={{
              left: isRtl ? "38px" : "auto",
              right: isRtl ? "auto" : "38px",
              top: "50%",
              transform: "translateY(-50%)",
            }}
          >
            <span aria-hidden="true" className="text-lg leading-none">
              ×
            </span>
          </button>
        )}

        {/* Custom Calendar Icon */}
        <button
          type="button"
          aria-label={i18n.t("COMMON.OPEN_CALENDAR")}
          disabled={disabled}
          onClick={disabled ? undefined : onIconClick}
          className={cn(
            "absolute text-primary-5",
            disabled && "cursor-not-allowed opacity-50"
          )}
          style={{
            left: isRtl ? "10px" : "auto",
            right: isRtl ? "auto" : "10px",
            top: "50%",
            transform: "translateY(-50%)",
          }}
        >
          {showDueDate ? (
            <>
              <Image
                src={asset("homepage/dateicon.svg")}
                alt=""
                width={20}
                height={20}
                className={disabled ? "opacity-50" : ""}
              />
              {disabled && (
                <div className="absolute inset-0 flex items-center justify-center">
                  <div className="h-0.5 w-full bg-gray-400 rotate-45 rounded-full" />
                </div>
              )}
            </>
          ) : (
            <Image
              src={asset("homepage/dateicon.svg")}
              alt=""
              width={20}
              height={20}
            />
          )}
        </button>
    </>
  );
}
