"use client";
'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const button = require('../../../../../../ui/src/components/button.cjs');
const badge = require('../../../../../../ui/src/components/badge.cjs');
const sheet = require('../../../../../../ui/src/components/sheet.cjs');
const LucideIcons = require('lucide-react');
const utils = require('../../../../../../ui/src/lib/utils.cjs');
const chatSidebar = require('./chat-sidebar.cjs');
const chatInterface = require('./chat-interface.cjs');
const plugins_aiChat_client_context_pageAiContext = require('../../../../../../../plugins/ai-chat/client/context/page-ai-context.cjs');

function ChatLayout(props) {
  const {
    apiBaseURL,
    apiBasePath,
    conversationId,
    layout = "full",
    className,
    showSidebar = true,
    initialMessages,
    onMessagesChange,
    onClear
  } = props;
  const widgetHeight = props.layout === "widget" ? props.widgetHeight ?? "600px" : "600px";
  const widgetWidth = props.layout === "widget" ? props.widgetWidth ?? "380px" : "380px";
  const defaultOpen = props.layout === "widget" ? props.defaultOpen ?? false : false;
  const showTrigger = props.layout === "widget" ? props.showTrigger ?? true : true;
  const [sidebarOpen, setSidebarOpen] = React.useState(true);
  const [mobileSidebarOpen, setMobileSidebarOpen] = React.useState(false);
  const [chatResetKey, setChatResetKey] = React.useState(0);
  const [widgetOpen, setWidgetOpen] = React.useState(defaultOpen);
  const [widgetResetKey, setWidgetResetKey] = React.useState(0);
  const [widgetEverOpened, setWidgetEverOpened] = React.useState(defaultOpen);
  const pageAIContext = plugins_aiChat_client_context_pageAiContext.usePageAIContext();
  const apiPath = `${apiBaseURL}${apiBasePath}/chat`;
  const handleNewChat = React.useCallback(() => {
    if (!conversationId) {
      setChatResetKey((prev) => prev + 1);
    }
  }, [conversationId]);
  if (layout === "widget") {
    return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: utils.cn("flex flex-col items-end gap-3", className), children: [
      /* @__PURE__ */ jsxRuntime.jsxs(
        "div",
        {
          className: utils.cn(
            "flex flex-col border rounded-xl overflow-hidden bg-background shadow-xl",
            widgetOpen ? "flex" : "hidden"
          ),
          style: { height: widgetHeight, width: widgetWidth },
          children: [
            /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5 px-3 py-1.5 border-b bg-muted/40", children: [
              /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Sparkles, { className: "h-3 w-3 text-muted-foreground" }),
              pageAIContext ? /* @__PURE__ */ jsxRuntime.jsx(
                badge.Badge,
                {
                  variant: "secondary",
                  className: "text-xs",
                  "data-testid": "page-context-badge",
                  children: pageAIContext.routeName
                }
              ) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-xs text-muted-foreground font-medium", children: "AI Chat" }),
              /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1" }),
              /* @__PURE__ */ jsxRuntime.jsx(
                button.Button,
                {
                  variant: "ghost",
                  size: "icon",
                  className: "h-5 w-5",
                  onClick: () => {
                    onClear?.();
                    setWidgetResetKey((prev) => prev + 1);
                  },
                  "aria-label": "Clear chat",
                  title: "Clear chat",
                  children: /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Trash2, { className: "h-3.5 w-3.5" })
                }
              ),
              /* @__PURE__ */ jsxRuntime.jsx(
                button.Button,
                {
                  variant: "ghost",
                  size: "icon",
                  className: "h-5 w-5",
                  onClick: () => setWidgetOpen(false),
                  "aria-label": "Close chat",
                  children: /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.X, { className: "h-3.5 w-3.5" })
                }
              )
            ] }),
            widgetEverOpened && /* @__PURE__ */ jsxRuntime.jsx(
              chatInterface.ChatInterface,
              {
                apiPath,
                id: conversationId,
                variant: "widget",
                initialMessages,
                onMessagesChange
              },
              `widget-${conversationId ?? "new"}-${widgetResetKey}`
            )
          ]
        }
      ),
      showTrigger && /* @__PURE__ */ jsxRuntime.jsx(
        button.Button,
        {
          size: "icon",
          className: "h-12 w-12 rounded-full shadow-lg",
          onClick: () => {
            setWidgetOpen((prev) => !prev);
            setWidgetEverOpened(true);
          },
          "aria-label": widgetOpen ? "Close chat" : "Open chat",
          "data-testid": "widget-trigger",
          children: widgetOpen ? /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.X, { className: "h-5 w-5" }) : /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Sparkles, { className: "h-5 w-5" })
        }
      )
    ] });
  }
  return /* @__PURE__ */ jsxRuntime.jsxs(
    "div",
    {
      className: utils.cn(
        "flex h-[calc(100vh-4rem)] w-full overflow-hidden",
        className
      ),
      "data-testid": "chat-layout",
      children: [
        showSidebar && /* @__PURE__ */ jsxRuntime.jsx(
          "div",
          {
            className: utils.cn(
              "hidden md:flex transition-all duration-300 ease-in-out",
              sidebarOpen ? "w-72" : "w-0"
            ),
            children: sidebarOpen && /* @__PURE__ */ jsxRuntime.jsx(
              chatSidebar.ChatSidebar,
              {
                currentConversationId: conversationId,
                onNewChat: handleNewChat,
                className: "w-72"
              }
            )
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex-1 flex flex-col min-w-0", children: [
          /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2 p-2 border-b bg-background/95 backdrop-blur supports-backdrop-filter:bg-background/60", children: [
            showSidebar && /* @__PURE__ */ jsxRuntime.jsxs(sheet.Sheet, { open: mobileSidebarOpen, onOpenChange: setMobileSidebarOpen, children: [
              /* @__PURE__ */ jsxRuntime.jsx(sheet.SheetTrigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(
                button.Button,
                {
                  variant: "ghost",
                  size: "icon",
                  className: "md:hidden",
                  "aria-label": "Open menu",
                  children: /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Menu, { className: "h-5 w-5" })
                }
              ) }),
              /* @__PURE__ */ jsxRuntime.jsx(sheet.SheetContent, { side: "left", className: "p-0 w-72", children: /* @__PURE__ */ jsxRuntime.jsx(
                chatSidebar.ChatSidebar,
                {
                  currentConversationId: conversationId,
                  onNewChat: () => {
                    handleNewChat();
                    setMobileSidebarOpen(false);
                  }
                }
              ) })
            ] }),
            showSidebar && /* @__PURE__ */ jsxRuntime.jsx(
              button.Button,
              {
                variant: "ghost",
                size: "icon",
                className: "hidden md:flex",
                onClick: () => setSidebarOpen(!sidebarOpen),
                "aria-label": sidebarOpen ? "Close sidebar" : "Open sidebar",
                children: sidebarOpen ? /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.PanelLeftClose, { className: "h-5 w-5" }) : /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.PanelLeft, { className: "h-5 w-5" })
              }
            ),
            /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1" }),
            pageAIContext && /* @__PURE__ */ jsxRuntime.jsxs(
              badge.Badge,
              {
                variant: "secondary",
                className: "text-xs gap-1 mr-2",
                "data-testid": "page-context-badge",
                children: [
                  /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Sparkles, { className: "h-3 w-3" }),
                  pageAIContext.routeName
                ]
              }
            ),
            onClear && /* @__PURE__ */ jsxRuntime.jsx(
              button.Button,
              {
                variant: "ghost",
                size: "icon",
                onClick: () => {
                  onClear();
                  setChatResetKey((prev) => prev + 1);
                },
                "aria-label": "Clear chat",
                title: "Clear chat",
                children: /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Trash2, { className: "h-4 w-4" })
              }
            )
          ] }),
          /* @__PURE__ */ jsxRuntime.jsx(
            chatInterface.ChatInterface,
            {
              apiPath,
              id: conversationId,
              variant: "full",
              initialMessages,
              onMessagesChange
            },
            `chat-${conversationId ?? "new"}-${chatResetKey}`
          )
        ] })
      ]
    }
  );
}

exports.ChatLayout = ChatLayout;
