"use client"
import { useRef, useCallback, useMemo } from "react"
import { useIsXs } from "@/hooks/use-xs"
import { Sheet, SheetContent, SheetTitle } from "@/components/ui/sheet"
import { useComposeWindows } from "@/lib/compose-context"
import { cn } from "@/lib/utils"
import { ComposeWindow } from "@/components/gmail/compose/compose-window"
export function ComposeModalManager() {
const { composeWindows } = useComposeWindows()
const isXs = useIsXs()
const nonMaximized = composeWindows.filter(
(w) => !w.maximized && w.placement !== "inline"
)
const maximized = composeWindows.filter((w) => w.maximized && w.placement !== "inline")
const xsSheetCloseRef = useRef<(() => void) | null>(null)
const bindXsSheetClose = useCallback((fn: (() => void) | null) => {
xsSheetCloseRef.current = fn
}, [])
/** Une seule fenĂȘtre dock visible en xs : la plus rĂ©cente (comportement type pile). */
const xsActiveDock =
isXs && nonMaximized.length > 0 ? nonMaximized[nonMaximized.length - 1] : null
const handleXsSheetOpenChange = useCallback((open: boolean) => {
if (!open) {
xsSheetCloseRef.current?.()
}
}, [])
const MODAL_WIDTH = 500
const MINIMIZED_WIDTH = 280
const GAP = 12
const RIGHT_OFFSET = 80
const positions = useMemo(() => {
const reversed = [...nonMaximized].reverse()
const result: { id: string; right: number; hidden: boolean }[] = []
let cursor = RIGHT_OFFSET
for (let i = 0; i < reversed.length; i++) {
const w = reversed[i]
const width = w.minimized ? MINIMIZED_WIDTH : MODAL_WIDTH
result.push({
id: w.id,
right: cursor,
hidden: i >= 2 && !w.minimized,
})
cursor += width + GAP
}
return result
}, [nonMaximized])
if (isXs) {
return (
<>