"use client";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useCallback, useMemo, useState } from "react";
import { cn } from "@stigmer/theme";
import { SessionComposer } from "../composer";
import { ResizableSplit } from "../internal/ResizableSplit";
import { SessionInspector } from "./inspector/SessionInspector";
import { useNewSessionFlow } from "./useNewSessionFlow";
/**
 * Full-featured new-session launcher organism — the pre-session analog
 * of `SessionViewer`.
 *
 * Owns `useNewSessionFlow` internally and composes:
 * - **Centered composer** (primary pane): `SessionComposer` with all
 *   context pickers (agent, workspace, MCP servers, skills)
 * - **SessionInspector** (secondary pane): progressively revealed when
 *   context is attached (workspace/agent/MCP/skills/vars non-empty),
 *   showing only the Setup tab with interactive workspace actions.
 *
 * Framework-agnostic — no Next.js, no Tauri, no routing deps. Host
 * apps inject platform-specific values via props (DD-004/DD-016).
 *
 * @example
 * ```tsx
 * // Desktop
 * <NewSessionViewer
 *   org={org}
 *   onSessionCreated={(id) => navigate(`/sessions/${id}`)}
 *   onError={(msg) => toast.error(msg)}
 *   enableLocal
 *   onBrowseLocalFolder={browseLocalFolder}
 * />
 *
 * // Web
 * <NewSessionViewer
 *   org={org}
 *   onSessionCreated={navigateToSession}
 *   onError={(msg) => toast.error(msg)}
 *   enableGitHub
 *   gitHubConnection={gitHubConnection}
 * />
 * ```
 */
export function NewSessionViewer({ org, onSessionCreated, onError, gitHubConnection, enableGitHub = true, enableLocal = false, onBrowseLocalFolder, workspaceFileLister, initialAgentRef, initialInstanceId, initialAttachments, heading = "What would you like to work on?", placeholder = "Describe what you need help with\u2026", initialRows = 3, autoFocus = true, footerContent, className, }) {
    const flow = useNewSessionFlow({ org, onSessionCreated, onError });
    const [interactionMode, setInteractionMode] = useState("agent");
    const hasContext = flow.workspace.hasEntries ||
        flow.agentRef !== null ||
        flow.mcpServerUsages.length > 0 ||
        flow.skillRefs.length > 0 ||
        (flow.sessionVariables != null && !flow.sessionVariables.isEmpty);
    const handleRemoveAgent = useCallback(() => {
        flow.setAgentRef(null);
        flow.setResolution(null);
    }, [flow.setAgentRef, flow.setResolution]);
    const handleRemoveMcp = useCallback((ref) => {
        flow.setMcpServerUsages(flow.mcpServerUsages.filter((u) => !(u.mcpServerRef.org === ref.org && u.mcpServerRef.slug === ref.slug)));
    }, [flow.mcpServerUsages, flow.setMcpServerUsages]);
    const handleRemoveSkill = useCallback((ref) => {
        flow.setSkillRefs(flow.skillRefs.filter((r) => !(r.org === ref.org && r.slug === ref.slug)));
    }, [flow.skillRefs, flow.setSkillRefs]);
    const sessionConfig = useMemo(() => ({
        agentRef: flow.agentRef,
        isDefaultAgent: false,
        mcpServerUsages: flow.mcpServerUsages,
        skillRefs: flow.skillRefs,
        sessionVariables: flow.sessionVariables,
        harness: flow.harness,
        executionTarget: undefined,
        modelId: flow.modelId,
        mutations: {
            onRemoveAgent: flow.agentRef ? handleRemoveAgent : undefined,
            onRemoveMcp: handleRemoveMcp,
            onRemoveSkill: handleRemoveSkill,
        },
    }), [
        flow.agentRef, flow.mcpServerUsages, flow.skillRefs,
        flow.sessionVariables, flow.harness, flow.modelId,
        handleRemoveAgent, handleRemoveMcp, handleRemoveSkill,
    ]);
    const workspaceConfig = useMemo(() => ({
        actions: {
            workspace: flow.workspace,
            enableGitHub,
            enableLocal,
            gitHubConnection,
            onBrowseLocalFolder,
            workspaceFileLister,
        },
    }), [flow.workspace, enableGitHub, enableLocal, gitHubConnection, onBrowseLocalFolder, workspaceFileLister]);
    const composerNode = (_jsx("div", { className: "flex h-full flex-col items-center overflow-y-auto px-4", children: _jsxs("div", { className: cn("w-full max-w-2xl space-y-6", hasContext ? "my-6" : "my-auto"), children: [_jsx("h1", { className: "text-center text-lg font-medium text-foreground", children: heading }), _jsx(SessionComposer, { onSubmit: flow.submit, isSubmitting: flow.isSubmitting, org: org, workspace: flow.workspace, gitHubConnection: enableGitHub ? gitHubConnection : undefined, enableGitHub: enableGitHub, enableLocal: enableLocal, onBrowseLocalFolder: onBrowseLocalFolder, agentRef: flow.agentRef, onAgentRefChange: flow.setAgentRef, onAgentResolutionChange: flow.setResolution, initialAgentRef: initialAgentRef, initialInstanceId: initialInstanceId, initialAttachments: initialAttachments, mcpServerUsages: flow.mcpServerUsages, onMcpServerUsagesChange: flow.setMcpServerUsages, skillRefs: flow.skillRefs, onSkillRefsChange: flow.setSkillRefs, sessionVariables: flow.sessionVariables, showHarnessSelector: true, harness: flow.harness, onHarnessChange: flow.setHarness, interactionMode: interactionMode, onInteractionModeChange: setInteractionMode, showInteractionModePicker: true, defaultModelId: flow.modelId, onModelChange: flow.setModelId, placeholder: placeholder, initialRows: initialRows, autoFocus: autoFocus, ariaLabel: "Start a new session" }), footerContent, _jsx("p", { className: "text-center text-[0.65rem] text-muted-foreground", children: "Press Enter to send, Shift+Enter for a new line" })] }) }));
    if (!hasContext) {
        return (_jsx("div", { className: cn("flex h-full w-full flex-col", className), children: composerNode }));
    }
    return (_jsx("div", { className: cn("flex h-full w-full flex-col", className), children: _jsx(ResizableSplit, { defaultSize: 320, minSize: 240, maxSize: 480, storageKey: "stgm-new-session-inspector-width", className: cn("min-h-0 flex-1", "[&>*:nth-child(2)]:max-lg:hidden [&>*:nth-child(3)]:max-lg:hidden"), primary: composerNode, secondary: _jsx("aside", { className: "flex h-full flex-col overflow-hidden", children: _jsx(SessionInspector, { displayExecution: null, allExecutions: [], org: org, selectedItem: null, sessionConfig: sessionConfig, workspaceConfig: workspaceConfig, className: "min-h-0 flex-1" }) }) }) }));
}
//# sourceMappingURL=NewSessionViewer.js.map