"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QuantumSwarmDemo = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
/**
 * QuantumSwarmDemo.tsx
 *
 * A demo component showcasing the Quantum Swarm Orchestrator client integration
 * with React components. This demonstrates agent registration, workflow creation,
 * and insight contribution.
 */
const react_1 = require("react");
const useQuantumSwarm_js_1 = require("../../services/quantum-swarm/useQuantumSwarm.js");
const react_2 = require("@chakra-ui/react");
/**
 * Quantum Swarm Demo Component
 * Demonstrates the core features of the Quantum Swarm Orchestrator
 */
const QuantumSwarmDemo = () => {
    const toast = (0, react_2.useToast)();
    // Initialize the quantum swarm hook
    const { agents, workflows, insights, loading, error, createAgent, createWorkflow, contributeInsight, fetchWorkflowInsights } = (0, useQuantumSwarm_js_1.useQuantumSwarm)({
        autoInitialize: true
    });
    // Local state for form inputs
    const [agentName, setAgentName] = (0, react_1.useState)('');
    const [agentCapabilities, setAgentCapabilities] = (0, react_1.useState)('');
    const [quantumAffinity, setQuantumAffinity] = (0, react_1.useState)(0.7);
    const [workflowName, setWorkflowName] = (0, react_1.useState)('');
    const [workflowDescription, setWorkflowDescription] = (0, react_1.useState)('');
    const [selectedAgents, setSelectedAgents] = (0, react_1.useState)([]);
    const [selectedWorkflow, setSelectedWorkflow] = (0, react_1.useState)('');
    const [selectedAgent, setSelectedAgent] = (0, react_1.useState)('');
    const [insightType, setInsightType] = (0, react_1.useState)('emergence');
    const [insightContent, setInsightContent] = (0, react_1.useState)('');
    const [insightConfidence, setInsightConfidence] = (0, react_1.useState)(0.8);
    // Handle agent creation
    const handleCreateAgent = async () => {
        try {
            if (!agentName || !agentCapabilities) {
                toast({
                    title: 'Missing information',
                    description: 'Please provide a name and capabilities for the agent.',
                    status: 'warning',
                    duration: 3000,
                    isClosable: true
                });
                return;
            }
            // Parse capabilities from comma-separated string
            const capabilities = agentCapabilities
                .split(',')
                .map(cap => cap.trim())
                .filter(Boolean);
            // Create specialization scores based on capabilities
            const specializationScores = {};
            capabilities.forEach(cap => {
                specializationScores[cap] = 0.7 + (Math.random() * 0.3);
            });
            // Register the agent
            const agent = await createAgent({
                name: agentName,
                capabilities,
                quantumAffinity,
                entanglementCapacity: Math.floor(capabilities.length * 1.5),
                specializationScores
            });
            toast({
                title: 'Agent created',
                description: `Successfully created agent: ${agent.name}`,
                status: 'success',
                duration: 3000,
                isClosable: true
            });
            // Reset form
            setAgentName('');
            setAgentCapabilities('');
            setQuantumAffinity(0.7);
        }
        catch (err) {
            toast({
                title: 'Failed to create agent',
                description: err instanceof Error ? err.message : String(err),
                status: 'error',
                duration: 5000,
                isClosable: true
            });
        }
    };
    // Handle workflow creation
    const handleCreateWorkflow = async () => {
        try {
            if (!workflowName || !workflowDescription) {
                toast({
                    title: 'Missing information',
                    description: 'Please provide a name and description for the workflow.',
                    status: 'warning',
                    duration: 3000,
                    isClosable: true
                });
                return;
            }
            // Create the workflow
            const workflow = await createWorkflow(workflowName, workflowDescription, selectedAgents.length > 0 ? selectedAgents : undefined);
            toast({
                title: 'Workflow created',
                description: `Successfully created workflow: ${workflow.name}`,
                status: 'success',
                duration: 3000,
                isClosable: true
            });
            // Reset form
            setWorkflowName('');
            setWorkflowDescription('');
            setSelectedAgents([]);
        }
        catch (err) {
            toast({
                title: 'Failed to create workflow',
                description: err instanceof Error ? err.message : String(err),
                status: 'error',
                duration: 5000,
                isClosable: true
            });
        }
    };
    // Handle insight contribution
    const handleContributeInsight = async () => {
        try {
            if (!selectedWorkflow || !selectedAgent || !insightContent) {
                toast({
                    title: 'Missing information',
                    description: 'Please select a workflow, an agent, and provide insight content.',
                    status: 'warning',
                    duration: 3000,
                    isClosable: true
                });
                return;
            }
            // Contribute the insight
            const insight = await contributeInsight(selectedWorkflow, selectedAgent, insightType, insightContent, insightConfidence);
            toast({
                title: 'Insight contributed',
                description: `Successfully contributed ${insightType} insight to workflow`,
                status: 'success',
                duration: 3000,
                isClosable: true
            });
            // Fetch updated insights
            await fetchWorkflowInsights(selectedWorkflow);
            // Reset form
            setInsightContent('');
        }
        catch (err) {
            toast({
                title: 'Failed to contribute insight',
                description: err instanceof Error ? err.message : String(err),
                status: 'error',
                duration: 5000,
                isClosable: true
            });
        }
    };
    // Toggle agent selection
    const toggleAgentSelection = (agentId) => {
        if (selectedAgents.includes(agentId)) {
            setSelectedAgents(selectedAgents.filter(id => id !== agentId));
        }
        else {
            setSelectedAgents([...selectedAgents, agentId]);
        }
    };
    // Handle workflow selection
    const handleWorkflowSelect = async (workflowId) => {
        setSelectedWorkflow(workflowId);
        if (workflowId) {
            await fetchWorkflowInsights(workflowId);
        }
    };
    // Get status badge color based on status
    const getStatusColor = (status) => {
        switch (status) {
            case 'initializing': return 'blue';
            case 'in_progress': return 'green';
            case 'completed': return 'teal';
            case 'failed': return 'red';
            default: return 'gray';
        }
    };
    // Get insight type badge color
    const getInsightColor = (type) => {
        switch (type) {
            case 'convergence': return 'green';
            case 'divergence': return 'purple';
            case 'emergence': return 'blue';
            case 'quantum_leap': return 'pink';
            case 'consensus': return 'teal';
            default: return 'gray';
        }
    };
    return ((0, jsx_runtime_1.jsxs)(react_2.Container, { maxW: "container.xl", py: 8, children: [(0, jsx_runtime_1.jsx)(react_2.Heading, { as: "h1", mb: 6, children: "Quantum Swarm Orchestrator Demo" }), (error.agents || error.workflows || error.insights) && ((0, jsx_runtime_1.jsxs)(react_2.Alert, { status: "error", mb: 6, children: [(0, jsx_runtime_1.jsx)(react_2.AlertIcon, {}), (0, jsx_runtime_1.jsx)(react_2.AlertTitle, { children: "Error:" }), (0, jsx_runtime_1.jsx)(react_2.AlertDescription, { children: error.agents?.message || error.workflows?.message || error.insights?.message })] })), (0, jsx_runtime_1.jsxs)(react_2.Flex, { direction: { base: 'column', lg: 'row' }, gap: 8, children: [(0, jsx_runtime_1.jsxs)(react_2.Box, { flex: "1", children: [(0, jsx_runtime_1.jsxs)(react_2.Card, { mb: 6, children: [(0, jsx_runtime_1.jsx)(react_2.CardHeader, { children: (0, jsx_runtime_1.jsx)(react_2.Heading, { size: "md", children: "Create Quantum Agent" }) }), (0, jsx_runtime_1.jsx)(react_2.CardBody, { children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 4, align: "stretch", children: [(0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Agent Name" }), (0, jsx_runtime_1.jsx)(react_2.Input, { value: agentName, onChange: (e) => setAgentName(e.target.value), placeholder: "Enter agent name" })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Capabilities (comma-separated)" }), (0, jsx_runtime_1.jsx)(react_2.Input, { value: agentCapabilities, onChange: (e) => setAgentCapabilities(e.target.value), placeholder: "pattern_recognition, data_analysis, etc." })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsxs)(react_2.FormLabel, { children: ["Quantum Affinity: ", quantumAffinity.toFixed(2)] }), (0, jsx_runtime_1.jsx)(react_2.Input, { type: "range", min: 0, max: 1, step: 0.1, value: quantumAffinity, onChange: (e) => setQuantumAffinity(parseFloat(e.target.value)) })] })] }) }), (0, jsx_runtime_1.jsx)(react_2.CardFooter, { children: (0, jsx_runtime_1.jsx)(react_2.Button, { colorScheme: "blue", onClick: handleCreateAgent, isLoading: loading.agents, children: "Create Agent" }) })] }), (0, jsx_runtime_1.jsxs)(react_2.Card, { children: [(0, jsx_runtime_1.jsx)(react_2.CardHeader, { children: (0, jsx_runtime_1.jsx)(react_2.Heading, { size: "md", children: "Registered Agents" }) }), (0, jsx_runtime_1.jsx)(react_2.CardBody, { children: loading.agents ? ((0, jsx_runtime_1.jsx)(react_2.Flex, { justify: "center", align: "center", minH: "200px", children: (0, jsx_runtime_1.jsx)(react_2.Spinner, { size: "xl" }) })) : agents.length === 0 ? ((0, jsx_runtime_1.jsx)(react_2.Text, { color: "gray.500", children: "No agents registered yet" })) : ((0, jsx_runtime_1.jsx)(react_2.VStack, { spacing: 3, align: "stretch", children: agents.map((agent) => ((0, jsx_runtime_1.jsx)(react_2.Card, { variant: "outline", borderColor: selectedAgents.includes(agent.id) ? 'blue.500' : 'gray.200', onClick: () => toggleAgentSelection(agent.id), cursor: "pointer", _hover: { shadow: 'md' }, children: (0, jsx_runtime_1.jsx)(react_2.CardBody, { children: (0, jsx_runtime_1.jsxs)(react_2.Flex, { justify: "space-between", align: "center", children: [(0, jsx_runtime_1.jsxs)(react_2.VStack, { align: "start", spacing: 1, children: [(0, jsx_runtime_1.jsx)(react_2.Heading, { size: "sm", children: agent.name }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontSize: "sm", color: "gray.600", children: ["ID: ", agent.id.substring(0, 8), "..."] }), (0, jsx_runtime_1.jsx)(react_2.HStack, { mt: 2, children: agent.capabilities.map((cap) => ((0, jsx_runtime_1.jsx)(react_2.Badge, { colorScheme: "purple", children: cap }, cap))) })] }), (0, jsx_runtime_1.jsxs)(react_2.VStack, { align: "end", children: [(0, jsx_runtime_1.jsxs)(react_2.Badge, { colorScheme: "blue", children: ["Quantum: ", agent.quantumAffinity.toFixed(2)] }), (0, jsx_runtime_1.jsxs)(react_2.Badge, { colorScheme: "green", children: ["Entanglement: ", agent.entanglementCapacity] })] })] }) }) }, agent.id))) })) })] })] }), (0, jsx_runtime_1.jsxs)(react_2.Box, { flex: "1", children: [(0, jsx_runtime_1.jsxs)(react_2.Card, { mb: 6, children: [(0, jsx_runtime_1.jsx)(react_2.CardHeader, { children: (0, jsx_runtime_1.jsx)(react_2.Heading, { size: "md", children: "Create Cognitive Workflow" }) }), (0, jsx_runtime_1.jsx)(react_2.CardBody, { children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 4, align: "stretch", children: [(0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Workflow Name" }), (0, jsx_runtime_1.jsx)(react_2.Input, { value: workflowName, onChange: (e) => setWorkflowName(e.target.value), placeholder: "Enter workflow name" })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Description" }), (0, jsx_runtime_1.jsx)(react_2.Textarea, { value: workflowDescription, onChange: (e) => setWorkflowDescription(e.target.value), placeholder: "Describe the purpose of this cognitive workflow", rows: 3 })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsxs)(react_2.FormLabel, { children: ["Selected Agents: ", selectedAgents.length] }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "sm", color: "gray.600", mb: 2, children: "Click on agents in the left panel to select them for this workflow, or leave empty for automatic agent selection." })] })] }) }), (0, jsx_runtime_1.jsx)(react_2.CardFooter, { children: (0, jsx_runtime_1.jsx)(react_2.Button, { colorScheme: "green", onClick: handleCreateWorkflow, isLoading: loading.workflows, children: "Create Workflow" }) })] }), (0, jsx_runtime_1.jsxs)(react_2.Card, { children: [(0, jsx_runtime_1.jsx)(react_2.CardHeader, { children: (0, jsx_runtime_1.jsx)(react_2.Heading, { size: "md", children: "Cognitive Workflows" }) }), (0, jsx_runtime_1.jsx)(react_2.CardBody, { children: loading.workflows ? ((0, jsx_runtime_1.jsx)(react_2.Flex, { justify: "center", align: "center", minH: "200px", children: (0, jsx_runtime_1.jsx)(react_2.Spinner, { size: "xl" }) })) : workflows.length === 0 ? ((0, jsx_runtime_1.jsx)(react_2.Text, { color: "gray.500", children: "No workflows created yet" })) : ((0, jsx_runtime_1.jsx)(react_2.VStack, { spacing: 3, align: "stretch", children: workflows.map((workflow) => ((0, jsx_runtime_1.jsx)(react_2.Card, { variant: "outline", borderColor: selectedWorkflow === workflow.id ? 'green.500' : 'gray.200', onClick: () => handleWorkflowSelect(workflow.id), cursor: "pointer", _hover: { shadow: 'md' }, children: (0, jsx_runtime_1.jsx)(react_2.CardBody, { children: (0, jsx_runtime_1.jsxs)(react_2.Flex, { justify: "space-between", align: "center", children: [(0, jsx_runtime_1.jsxs)(react_2.VStack, { align: "start", spacing: 1, children: [(0, jsx_runtime_1.jsx)(react_2.Heading, { size: "sm", children: workflow.name }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "sm", color: "gray.600", noOfLines: 2, children: workflow.description }), (0, jsx_runtime_1.jsxs)(react_2.Text, { fontSize: "xs", color: "gray.500", children: ["Created: ", workflow.createdAt.toLocaleString()] })] }), (0, jsx_runtime_1.jsxs)(react_2.VStack, { align: "end", children: [(0, jsx_runtime_1.jsx)(react_2.Badge, { colorScheme: getStatusColor(workflow.status), children: workflow.status }), (0, jsx_runtime_1.jsxs)(react_2.Badge, { colorScheme: "blue", children: ["Agents: ", workflow.agentIds.length] }), (0, jsx_runtime_1.jsxs)(react_2.Badge, { colorScheme: "purple", children: ["Insights: ", workflow.insights.length] })] })] }) }) }, workflow.id))) })) })] })] }), (0, jsx_runtime_1.jsxs)(react_2.Box, { flex: "1", children: [(0, jsx_runtime_1.jsxs)(react_2.Card, { mb: 6, children: [(0, jsx_runtime_1.jsx)(react_2.CardHeader, { children: (0, jsx_runtime_1.jsx)(react_2.Heading, { size: "md", children: "Contribute Insight" }) }), (0, jsx_runtime_1.jsx)(react_2.CardBody, { children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { spacing: 4, align: "stretch", children: [(0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Select Workflow" }), (0, jsx_runtime_1.jsx)(react_2.Select, { value: selectedWorkflow, onChange: (e) => handleWorkflowSelect(e.target.value), placeholder: "Select workflow", children: workflows.map((workflow) => ((0, jsx_runtime_1.jsx)("option", { value: workflow.id, children: workflow.name }, workflow.id))) })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Select Agent" }), (0, jsx_runtime_1.jsx)(react_2.Select, { value: selectedAgent, onChange: (e) => setSelectedAgent(e.target.value), placeholder: "Select agent", children: agents.map((agent) => ((0, jsx_runtime_1.jsx)("option", { value: agent.id, children: agent.name }, agent.id))) })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Insight Type" }), (0, jsx_runtime_1.jsxs)(react_2.Select, { value: insightType, onChange: (e) => setInsightType(e.target.value), children: [(0, jsx_runtime_1.jsx)("option", { value: "convergence", children: "Convergence" }), (0, jsx_runtime_1.jsx)("option", { value: "divergence", children: "Divergence" }), (0, jsx_runtime_1.jsx)("option", { value: "emergence", children: "Emergence" }), (0, jsx_runtime_1.jsx)("option", { value: "quantum_leap", children: "Quantum Leap" }), (0, jsx_runtime_1.jsx)("option", { value: "consensus", children: "Consensus" })] })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsx)(react_2.FormLabel, { children: "Insight Content" }), (0, jsx_runtime_1.jsx)(react_2.Textarea, { value: insightContent, onChange: (e) => setInsightContent(e.target.value), placeholder: "Describe the insight", rows: 4 })] }), (0, jsx_runtime_1.jsxs)(react_2.FormControl, { children: [(0, jsx_runtime_1.jsxs)(react_2.FormLabel, { children: ["Confidence: ", insightConfidence.toFixed(2)] }), (0, jsx_runtime_1.jsx)(react_2.Input, { type: "range", min: 0, max: 1, step: 0.05, value: insightConfidence, onChange: (e) => setInsightConfidence(parseFloat(e.target.value)) })] })] }) }), (0, jsx_runtime_1.jsx)(react_2.CardFooter, { children: (0, jsx_runtime_1.jsx)(react_2.Button, { colorScheme: "purple", onClick: handleContributeInsight, isLoading: loading.insights, isDisabled: !selectedWorkflow || !selectedAgent, children: "Contribute Insight" }) })] }), (0, jsx_runtime_1.jsxs)(react_2.Card, { children: [(0, jsx_runtime_1.jsx)(react_2.CardHeader, { children: (0, jsx_runtime_1.jsxs)(react_2.Heading, { size: "md", children: ["Workflow Insights", selectedWorkflow && ((0, jsx_runtime_1.jsx)(react_2.Badge, { ml: 2, colorScheme: "green", children: workflows.find(w => w.id === selectedWorkflow)?.name }))] }) }), (0, jsx_runtime_1.jsx)(react_2.CardBody, { children: loading.insights ? ((0, jsx_runtime_1.jsx)(react_2.Flex, { justify: "center", align: "center", minH: "200px", children: (0, jsx_runtime_1.jsx)(react_2.Spinner, { size: "xl" }) })) : !selectedWorkflow ? ((0, jsx_runtime_1.jsx)(react_2.Text, { color: "gray.500", children: "Select a workflow to view insights" })) : insights.length === 0 ? ((0, jsx_runtime_1.jsx)(react_2.Text, { color: "gray.500", children: "No insights for this workflow yet" })) : ((0, jsx_runtime_1.jsx)(react_2.VStack, { spacing: 3, align: "stretch", children: insights.map((insight) => ((0, jsx_runtime_1.jsx)(react_2.Card, { variant: "outline", children: (0, jsx_runtime_1.jsx)(react_2.CardBody, { children: (0, jsx_runtime_1.jsxs)(react_2.VStack, { align: "start", spacing: 2, children: [(0, jsx_runtime_1.jsxs)(react_2.Flex, { w: "100%", justify: "space-between", align: "center", children: [(0, jsx_runtime_1.jsx)(react_2.Badge, { colorScheme: getInsightColor(insight.type), children: insight.type }), (0, jsx_runtime_1.jsxs)(react_2.Badge, { colorScheme: "yellow", children: ["Confidence: ", insight.confidenceScore.toFixed(2)] })] }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontWeight: "bold", children: insight.description }), (0, jsx_runtime_1.jsx)(react_2.Text, { fontSize: "sm", color: "gray.600", children: typeof insight.content === 'string'
                                                                    ? insight.content
                                                                    : JSON.stringify(insight.content, null, 2) }), (0, jsx_runtime_1.jsx)(react_2.Divider, { my: 1 }), (0, jsx_runtime_1.jsxs)(react_2.HStack, { fontSize: "xs", color: "gray.500", children: [(0, jsx_runtime_1.jsxs)(react_2.Text, { children: ["Contributors: ", insight.contributingAgents.length] }), (0, jsx_runtime_1.jsxs)(react_2.Text, { children: ["Created: ", insight.timestamp.toLocaleString()] })] })] }) }) }, insight.id))) })) })] })] })] })] }));
};
exports.QuantumSwarmDemo = QuantumSwarmDemo;
exports.default = exports.QuantumSwarmDemo;
//# sourceMappingURL=QuantumSwarmDemo.js.map