"use client";

import { useTranslation } from "react-i18next";

import AccountSwitcher from "./AccountSwitcher";
import { useAuthStore } from "@/store/authStore";

/**
 * BE-129 — the account block at the foot of the phone menu: who is signed in
 * here, how to switch, and the two ways out.
 *
 * Its own component because the phone menu is a flat list of links and this is
 * the one part of it with structure — and because `HeaderMobileMenu` is at the
 * 200-line limit, which this would have pushed it past.
 *
 * «تسجيل الخروج» ends the account being used and continues as the next stored
 * one; «تسجيل الخروج من كل الحسابات» appears only when that distinction is
 * real, and clears the device.
 */
export default function MobileAccountActions({
  close,
  handleLogout,
  handleLogoutAll,
}: {
  close: () => void;
  handleLogout: () => void;
  handleLogoutAll: () => void;
}) {
  const { t } = useTranslation();
  /* Read here rather than threaded down: it is one derived boolean, and
     `AccountSwitcher` beside it already reads the same store. */
  const hasOtherAccounts = useAuthStore((state) => state.accounts.length > 0);

  return (
    <>
      <AccountSwitcher close={close} />
      <li className="border-b border-b-primary-5/40 text-primary-5">
        <button
          className="w-full block text-sm ps-5 py-2 pe-6 text-start"
          onClick={handleLogout}
        >
          {t("COMMON.LOGOUT")}
        </button>
      </li>
      {hasOtherAccounts && (
        <li className="border-b border-b-primary-5/40 text-secondary-102">
          <button
            className="w-full block text-sm ps-5 py-2 pe-6 text-start"
            onClick={handleLogoutAll}
          >
            {t("COMMON.LOGOUT_ALL")}
          </button>
        </li>
      )}
    </>
  );
}
