import { useEffect, useRef, useState } from 'react' import type { RefObject } from 'react' import type { GroupListItem } from '@gruperly/shared' import type { LucideIcon } from 'lucide-react' import { CalendarCheck, CalendarX, History, Loader2, MoveRight, UserPlus, Wallet } from 'lucide-react' import { Button, Modal } from '../../components/ui' import { useAttendeeDetail } from './AttendeeDetailProvider' import { useTranslation } from 'react-i18next' type Consequence = { key: string; icon: LucideIcon; text: string } function isFull(group: GroupListItem): boolean { return group.capacity != null && group.attendeesCount >= group.capacity } /* "Cambiar de grupo" (sale de este y entra al otro) y "Agregar a otro grupo" (queda en los dos) comparten el selector de destino; cambian el texto, las consecuencias y la acción. */ export function ChangeGroupDialog({ returnFocusRef, }: { /** The element focus returns to when it closes (the "⋯" button). */ returnFocusRef: RefObject }) { const { groupId, attendee, groupName, groups, groupsPending, groupDialogMode, closeGroupDialog, moveToGroup, isMovingAttendee, addToGroup, isAddingToGroup, } = useAttendeeDetail() const isOpen = groupDialogMode !== null const isAdd = groupDialogMode === 'add' const [targetGroupId, setTargetGroupId] = useState('') const wasOpen = useRef(false) const { t } = useTranslation(['attendeeDetail', 'common']) // Cada apertura arranca sin destino elegido; al cerrar, el foco vuelve al "⋯". useEffect(() => { if (isOpen) { wasOpen.current = true setTargetGroupId('') return } if (!wasOpen.current) return wasOpen.current = false returnFocusRef.current?.focus() }, [isOpen, returnFocusRef]) if (!attendee) return null // Fuera el grupo actual y los grupos donde ya está. Al agregar, una baja // anterior (DROPPED) se puede reactivar; al mover, no (sería otra membresía). const blocked = new Set( attendee.otherGroups .filter((membership) => !isAdd || membership.status !== 'DROPPED') .map((membership) => membership.groupId), ) const otherGroups = groups.filter((group) => group.id !== groupId && !blocked.has(group.id)) const currentName = groupName ?? t('changeGroup.currentGroup') const isSubmitting = isAdd ? isAddingToGroup : isMovingAttendee const consequences: Consequence[] = isAdd ? [ { key: 'sigue', icon: CalendarCheck, text: t('changeGroup.addStays', { group: currentName }), }, { key: 'datos', icon: History, text: t('changeGroup.addShares'), }, { key: 'cuotas', icon: Wallet, text: t('changeGroup.addSeparate'), }, ] : [ { key: 'historial', icon: History, text: t('changeGroup.moveKeeps'), }, { key: 'cuotas', icon: Wallet, text: t('changeGroup.moveFees', { group: currentName }), }, { key: 'faltas', icon: CalendarX, text: t('changeGroup.moveNotices', { group: currentName }), }, ] return (
{groupsPending ? (
) : otherGroups.length === 0 ? (

{groups.length > 1 ? t('changeGroup.allGroups') : isAdd ? t('changeGroup.noGroupsAdd') : t('changeGroup.noGroupsMove')}

) : ( )}
    {consequences.map(({ icon: Icon, key, text }) => (
  • ))}
) }