"use client";
'use strict';

const jsxRuntime = require('react/jsx-runtime');
const React = require('react');
const button = require('../../../../../../ui/src/components/button.cjs');
const input = require('../../../../../../ui/src/components/input.cjs');
const scrollArea = require('../../../../../../ui/src/components/scroll-area.cjs');
const dropdownMenu = require('../../../../../../ui/src/components/dropdown-menu.cjs');
const alertDialog = require('../../../../../../ui/src/components/alert-dialog.cjs');
const dialog = require('../../../../../../ui/src/components/dialog.cjs');
const LucideIcons = require('lucide-react');
const utils = require('../../../../../../ui/src/lib/utils.cjs');
const context = require('@btst/stack/context');
const chatHooks = require('../hooks/chat-hooks.cjs');
const index = require('../localization/index.cjs');

function ChatSidebar({
  currentConversationId,
  onNewChat,
  className
}) {
  const { navigate, localization: customLocalization } = context.usePluginOverrides("ai-chat", {});
  const basePath = context.useBasePath();
  const localization = { ...index.AI_CHAT_LOCALIZATION, ...customLocalization };
  const { conversations, isLoading } = chatHooks.useConversations();
  const renameMutation = chatHooks.useRenameConversation();
  const deleteMutation = chatHooks.useDeleteConversation();
  const [renameDialogOpen, setRenameDialogOpen] = React.useState(false);
  const [deleteDialogOpen, setDeleteDialogOpen] = React.useState(false);
  const [selectedConversation, setSelectedConversation] = React.useState(null);
  const [newTitle, setNewTitle] = React.useState("");
  const handleNewChat = () => {
    if (navigate) {
      navigate(`${basePath}/chat`);
    }
    if (onNewChat) {
      onNewChat();
    }
  };
  const handleConversationClick = (conversation) => {
    if (navigate) {
      navigate(`${basePath}/chat/${conversation.id}`);
    }
  };
  const handleRenameClick = (conversation) => {
    setSelectedConversation(conversation);
    setNewTitle(conversation.title);
    setRenameDialogOpen(true);
  };
  const handleDeleteClick = (conversation) => {
    setSelectedConversation(conversation);
    setDeleteDialogOpen(true);
  };
  const handleRenameConfirm = async () => {
    if (selectedConversation && newTitle.trim()) {
      await renameMutation.mutateAsync({
        id: selectedConversation.id,
        title: newTitle.trim()
      });
      setRenameDialogOpen(false);
      setSelectedConversation(null);
      setNewTitle("");
    }
  };
  const handleDeleteConfirm = async () => {
    if (selectedConversation) {
      await deleteMutation.mutateAsync({ id: selectedConversation.id });
      setDeleteDialogOpen(false);
      setSelectedConversation(null);
      if (selectedConversation.id === currentConversationId && navigate) {
        navigate(`${basePath}/chat`);
      }
    }
  };
  const formatDate = (dateString) => {
    const date = new Date(dateString);
    const now = /* @__PURE__ */ new Date();
    const diffMs = now.getTime() - date.getTime();
    const diffMins = Math.floor(diffMs / 6e4);
    const diffHours = Math.floor(diffMs / 36e5);
    const diffDays = Math.floor(diffMs / 864e5);
    if (diffMins < 1) return localization.TIME_JUST_NOW;
    if (diffMins < 60)
      return localization.TIME_MINUTES_AGO.replace("{count}", String(diffMins));
    if (diffHours < 24)
      return localization.TIME_HOURS_AGO.replace("{count}", String(diffHours));
    if (diffDays === 1) return localization.TIME_YESTERDAY;
    if (diffDays < 7)
      return localization.TIME_DAYS_AGO.replace("{count}", String(diffDays));
    return date.toLocaleDateString();
  };
  return /* @__PURE__ */ jsxRuntime.jsxs(
    "div",
    {
      "data-testid": "chat-sidebar",
      className: utils.cn("flex flex-col h-full bg-muted/30 border-r", className),
      children: [
        /* @__PURE__ */ jsxRuntime.jsx("div", { className: "p-4 border-b", children: /* @__PURE__ */ jsxRuntime.jsxs(
          button.Button,
          {
            onClick: handleNewChat,
            className: "w-full justify-start gap-2",
            variant: "outline",
            children: [
              /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.MessageSquarePlus, { className: "h-4 w-4" }),
              localization.SIDEBAR_NEW_CHAT
            ]
          }
        ) }),
        /* @__PURE__ */ jsxRuntime.jsx(scrollArea.ScrollArea, { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "p-2", children: isLoading ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "p-4 text-center text-sm text-muted-foreground", children: localization.CHAT_LOADING }) : conversations.length === 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "p-4 text-center text-sm text-muted-foreground", children: localization.SIDEBAR_NO_CONVERSATIONS }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "space-y-1", children: conversations.map((conversation) => /* @__PURE__ */ jsxRuntime.jsxs(
          "div",
          {
            className: utils.cn(
              "group flex items-center gap-2 rounded-lg px-3 py-2 hover:bg-accent cursor-pointer transition-colors overflow-hidden",
              currentConversationId === conversation.id && "bg-accent"
            ),
            children: [
              /* @__PURE__ */ jsxRuntime.jsxs(
                "button",
                {
                  type: "button",
                  className: "flex-1 flex items-center gap-2 text-left min-w-0 overflow-hidden",
                  onClick: () => handleConversationClick(conversation),
                  children: [
                    /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.MessageSquare, { className: "h-4 w-4 shrink-0 text-muted-foreground" }),
                    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex-1 min-w-0 overflow-hidden", children: [
                      /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-sm font-medium truncate", children: conversation.title }),
                      /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-xs text-muted-foreground truncate", children: formatDate(conversation.updatedAt) })
                    ] })
                  ]
                }
              ),
              /* @__PURE__ */ jsxRuntime.jsxs(dropdownMenu.DropdownMenu, { children: [
                /* @__PURE__ */ jsxRuntime.jsx(dropdownMenu.DropdownMenuTrigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(
                  button.Button,
                  {
                    variant: "ghost",
                    size: "icon",
                    className: "h-8 w-8 opacity-0 group-hover:opacity-100 transition-opacity",
                    children: /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.MoreHorizontal, { className: "h-4 w-4" })
                  }
                ) }),
                /* @__PURE__ */ jsxRuntime.jsxs(dropdownMenu.DropdownMenuContent, { align: "end", children: [
                  /* @__PURE__ */ jsxRuntime.jsxs(
                    dropdownMenu.DropdownMenuItem,
                    {
                      onClick: (e) => {
                        e.stopPropagation();
                        handleRenameClick(conversation);
                      },
                      children: [
                        /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Pencil, { className: "h-4 w-4 mr-2" }),
                        localization.CONVERSATION_RENAME
                      ]
                    }
                  ),
                  /* @__PURE__ */ jsxRuntime.jsxs(
                    dropdownMenu.DropdownMenuItem,
                    {
                      onClick: (e) => {
                        e.stopPropagation();
                        handleDeleteClick(conversation);
                      },
                      className: "text-destructive focus:text-destructive",
                      children: [
                        /* @__PURE__ */ jsxRuntime.jsx(LucideIcons.Trash2, { className: "h-4 w-4 mr-2" }),
                        localization.CONVERSATION_DELETE
                      ]
                    }
                  )
                ] })
              ] })
            ]
          },
          conversation.id
        )) }) }) }),
        /* @__PURE__ */ jsxRuntime.jsx(dialog.Dialog, { open: renameDialogOpen, onOpenChange: setRenameDialogOpen, children: /* @__PURE__ */ jsxRuntime.jsxs(dialog.DialogContent, { children: [
          /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogHeader, { children: /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogTitle, { children: localization.CONVERSATION_RENAME }) }),
          /* @__PURE__ */ jsxRuntime.jsx(
            input.Input,
            {
              value: newTitle,
              onChange: (e) => setNewTitle(e.target.value),
              placeholder: localization.CONVERSATION_RENAME_PLACEHOLDER,
              onKeyDown: (e) => {
                if (e.key === "Enter") {
                  void handleRenameConfirm();
                }
              }
            }
          ),
          /* @__PURE__ */ jsxRuntime.jsxs(dialog.DialogFooter, { children: [
            /* @__PURE__ */ jsxRuntime.jsx(
              button.Button,
              {
                variant: "outline",
                onClick: () => setRenameDialogOpen(false),
                children: localization.CONVERSATION_RENAME_CANCEL
              }
            ),
            /* @__PURE__ */ jsxRuntime.jsx(
              button.Button,
              {
                onClick: handleRenameConfirm,
                disabled: !newTitle.trim() || renameMutation.isPending,
                children: localization.CONVERSATION_RENAME_SAVE
              }
            )
          ] })
        ] }) }),
        /* @__PURE__ */ jsxRuntime.jsx(alertDialog.AlertDialog, { open: deleteDialogOpen, onOpenChange: setDeleteDialogOpen, children: /* @__PURE__ */ jsxRuntime.jsxs(alertDialog.AlertDialogContent, { children: [
          /* @__PURE__ */ jsxRuntime.jsxs(alertDialog.AlertDialogHeader, { children: [
            /* @__PURE__ */ jsxRuntime.jsx(alertDialog.AlertDialogTitle, { children: localization.CONVERSATION_DELETE_CONFIRM_TITLE }),
            /* @__PURE__ */ jsxRuntime.jsx(alertDialog.AlertDialogDescription, { children: localization.CONVERSATION_DELETE_CONFIRM_DESCRIPTION })
          ] }),
          /* @__PURE__ */ jsxRuntime.jsxs(alertDialog.AlertDialogFooter, { children: [
            /* @__PURE__ */ jsxRuntime.jsx(alertDialog.AlertDialogCancel, { children: localization.CONVERSATION_DELETE_CANCEL }),
            /* @__PURE__ */ jsxRuntime.jsx(
              alertDialog.AlertDialogAction,
              {
                onClick: handleDeleteConfirm,
                className: "bg-destructive text-destructive-foreground hover:bg-destructive/90",
                children: localization.CONVERSATION_DELETE_CONFIRM_BUTTON
              }
            )
          ] })
        ] }) })
      ]
    }
  );
}

exports.ChatSidebar = ChatSidebar;
