"use client" import type React from "react" import { useState, useRef } from "react" import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { CheckCircle2, XCircle, GripHorizontal } from "lucide-react" import { TerminalPanel } from "./terminal-panel" import { useIsMobile } from "@/hooks/use-mobile" interface WebInteraction { type: "yesno" | "menu" | "msgbox" | "input" | "inputbox" id: string title: string message: string options?: Array<{ label: string; value: string }> default?: string } interface ScriptTerminalModalProps { open: boolean onClose: () => void scriptPath: string scriptName: string params?: Record title: string description: string } export function ScriptTerminalModal({ open, onClose, scriptPath, scriptName, params = {}, title, description, }: ScriptTerminalModalProps) { const [isComplete, setIsComplete] = useState(false) const [exitCode, setExitCode] = useState(null) const [currentInteraction, setCurrentInteraction] = useState(null) const [interactionInput, setInteractionInput] = useState("") const isMobile = useIsMobile() const [modalHeight, setModalHeight] = useState(80) const [isResizing, setIsResizing] = useState(false) const startYRef = useRef(0) const startHeightRef = useRef(80) const initMessage = { script_path: scriptPath, params: { EXECUTION_MODE: "web", ...params, }, } const handleInteractionResponse = (value: string) => { if (value === "cancel" || value === "") { setCurrentInteraction(null) setInteractionInput("") onClose() return } // TerminalPanel manejará el envío de la respuesta setCurrentInteraction(null) setInteractionInput("") } const handleResizeStart = (e: React.MouseEvent | React.TouchEvent) => { setIsResizing(true) startYRef.current = "clientY" in e ? e.clientY : e.touches[0].clientY startHeightRef.current = modalHeight document.addEventListener("mousemove", handleResize as any) document.addEventListener("touchmove", handleResize as any) document.addEventListener("mouseup", handleResizeEnd) document.addEventListener("touchend", handleResizeEnd) } const handleResize = (e: MouseEvent | TouchEvent) => { if (!isResizing) return const currentY = e instanceof MouseEvent ? e.clientY : e.touches[0].clientY const deltaY = currentY - startYRef.current const newHeight = startHeightRef.current + (deltaY / window.innerHeight) * 100 setModalHeight(Math.max(50, Math.min(95, newHeight))) } const handleResizeEnd = () => { setIsResizing(false) document.removeEventListener("mousemove", handleResize as any) document.removeEventListener("touchmove", handleResize as any) document.removeEventListener("mouseup", handleResizeEnd) document.removeEventListener("touchend", handleResizeEnd) } return ( <> e.preventDefault()} onEscapeKeyDown={(e) => e.preventDefault()} > {title}
{isComplete && (exitCode === 0 ? ( ) : ( ))}

{title}

{description &&

{description}

}
{ setCurrentInteraction({ type: interaction.type as any, id: interaction.id, title: interaction.title || "", message: interaction.message || "", options: interaction.options, default: interaction.default, }) }} onClose={onClose} />
{!isMobile && (
)}
{currentInteraction && ( e.preventDefault()} onEscapeKeyDown={(e) => e.preventDefault()} hideClose > {currentInteraction.title}

{currentInteraction.message}

{currentInteraction.type === "yesno" && (
)} {currentInteraction.type === "menu" && currentInteraction.options && (
{currentInteraction.options.map((option) => ( ))}
)} {(currentInteraction.type === "input" || currentInteraction.type === "inputbox") && (
setInteractionInput(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { handleInteractionResponse(interactionInput) } }} placeholder={currentInteraction.default || ""} />
)} {currentInteraction.type === "msgbox" && (
)}
)} ) }