"use client";

import type { Dispatch, MouseEvent, SetStateAction } from "react";
import { useTranslation } from "react-i18next";
import { BsThreeDots } from "react-icons/bs";
import { MdDelete, MdEdit } from "react-icons/md";

/**
 * The three-dots edit/delete menu shown on a reply the signed-in user wrote.
 *
 * Both the reply being viewed and each reply underneath it use this, so the
 * dropdown key is the caller's `${"reply-"}${id}` string rather than something
 * derived here — the page and its children share one `showDropdown` slot and
 * only one menu may be open at a time.
 *
 * The caller owns the positioned wrapper and the `dropdownRefs` entry (the
 * click-outside handler measures that element). `showToggle` carries the
 * `is_creator && !is_deleted` gate: the viewed reply mounts its wrapper (and so
 * its ref) for every visitor and hides only the button, whereas a reply
 * underneath it mounts nothing at all for a visitor who cannot edit it.
 */
export default function ReplyActionsMenu({
  dropdownKey,
  selectedLanguage,
  showDropdown,
  setShowDropdown,
  onEdit,
  onDelete,
  iconClassName,
  showToggle = true,
  stopPropagationOnToggle = false,
}: {
  dropdownKey: string;
  selectedLanguage: string;
  showDropdown: string | null;
  setShowDropdown: Dispatch<SetStateAction<string | null>>;
  onEdit: () => void;
  onDelete: () => void;
  /* The viewed reply uses a larger glyph than the replies under it. */
  iconClassName: string;
  /* false hides the button but keeps the open panel mounted. */
  showToggle?: boolean;
  /* Set where the menu sits inside a row that is itself clickable. */
  stopPropagationOnToggle?: boolean;
}) {
  const { t } = useTranslation();
  const isOpen = showDropdown === dropdownKey;

  return (
    <>
      {showToggle && (
        <button
          onClick={(e: MouseEvent<HTMLButtonElement>) => {
            if (stopPropagationOnToggle) {
              e.preventDefault();
              e.stopPropagation();
            }
            setShowDropdown(isOpen ? null : dropdownKey);
          }}
          className="p-2 hover:bg-gray-100 rounded-full transition-colors duration-200"
        >
          <BsThreeDots className={iconClassName} />
        </button>
      )}

      {isOpen && (
        <div
          className={`absolute top-full mt-2 w-32 bg-white border border-gray-200 rounded-lg shadow-lg z-20 ${
            selectedLanguage === "ar" ? "left-0" : "right-0"
          }`}
        >
          <button
            onClick={onEdit}
            className="flex items-center gap-2 w-full px-4 py-2 text-left text-gray-700 hover:bg-gray-50 rounded-t-lg transition-colors duration-200"
          >
            <MdEdit className="text-primary-5" />
            <span className="text-sm">{t("COMMON.EDIT_TEXT")}</span>
          </button>
          <button
            onClick={onDelete}
            className="flex items-center gap-2 w-full px-4 py-2 text-left text-red-600 hover:bg-gray-50 rounded-b-lg transition-colors duration-200"
          >
            <MdDelete className="text-red-500" />
            <span className="text-sm">{t("COMMON.DELETE")}</span>
          </button>
        </div>
      )}
    </>
  );
}
