'use client';

/**
 * `@djangocfg/ui-tools/chat` subpath entrypoint.
 *
 * This file is the public surface consumers see when they
 * `import … from '@djangocfg/ui-tools/chat'`. It is designed so that the
 * heavy chat UI (~hundreds of KB across MessageList, Composer, ToolCalls,
 * Attachments, MarkdownMessage transitively) loads only when one of the
 * `Lazy*` wrappers actually mounts.
 *
 * Rules of thumb:
 * - **Heavy** (ChatRoot, ChatLauncher, ChatFAB, ChatDock, MessageList,
 *   MessageBubble, Composer, ToolCalls, Attachments*, ChatHeader*, …) —
 *   loaded only via `Lazy*` wrappers below for maximum laziness. They are
 *   ALSO re-exported synchronously (composable surface, see note below)
 *   so custom chat shells can import them. The sync re-export is a flat
 *   `export *` from the domain barrels — never a hand-listed copy.
 * - **Light** (types, config constants, pure core reducer/utils,
 *   transports, hooks without UI, audio prefs, draft sanitizer) —
 *   re-exported synchronously via `./public`. Types are erased at compile
 *   time; helpers and hooks pull in no UI components.
 *
 * The light surface lives in `./public` and is shared verbatim with the
 * root barrel `index.ts` — one list, edited once.
 *
 * Consumers that want maximum laziness should use `LazyChat` /
 * `LazyChatLauncher` and skip the synchronous component re-exports.
 */

import { createLazyComponent, LoadingFallback } from '../../common/lazy-wrapper';
import type { ChatRootProps } from './shell/ChatRoot';
import type { ChatLauncherProps } from './launcher';

// ============================================================================
// Light surface — shared with the root barrel via ./public
// ============================================================================

export * from './public';

// ============================================================================
// Lazy UI components
// ============================================================================

export const LazyChat = createLazyComponent<ChatRootProps>(
  () => import('./shell/ChatRoot').then((m) => ({ default: m.ChatRoot })),
  {
    displayName: 'LazyChat',
    fallback: <LoadingFallback minHeight={320} text="Loading chat…" />,
  },
);

export const LazyChatLauncher = createLazyComponent<ChatLauncherProps>(
  () => import('./launcher').then((m) => ({ default: m.ChatLauncher })),
  {
    displayName: 'LazyChatLauncher',
    // Launcher renders a floating FAB by default — no inline placeholder.
    fallback: null,
  },
);

// ============================================================================
// Composable surface — synchronously re-exported components and their prop
// types. These are needed by consumers that build custom chat shells
// (e.g. `@djangocfg/crm`'s AdminChat / PublicChat, Storybook stories)
// rather than rendering `LazyChat` directly.
//
// They DO transitively pull in MarkdownMessage / Sources / StreamingIndicator
// when actually rendered. Importing the types alone is free. Importing the
// component costs the dependency graph — that's the price of a custom shell.
// ============================================================================

export * from './messages';
export * from './composer';
export * from './shell';
export * from './launcher';

// Markdown renderer + plain-text helper — also surfaced from index.ts; the
// `./chat` package entry resolves to THIS file (lazy.tsx), so a host that
// composes a custom mention-aware bubble needs them re-exported here too
// (otherwise the symbols typecheck via index.ts but fail to bundle at runtime).
export {
  MarkdownMessage,
  extractTextFromChildren,
  type MarkdownMessageProps,
} from '../dev/code/MarkdownMessage';

// Same gotcha as above: a host that owns its own tool-call card (cmdop's
// `<ToolResult>` via the `renderToolCall` override) imports ToolPayloadValue
// from `@djangocfg/ui-tools/chat` → THIS file. The `export * from './messages'`
// above re-exports it for TYPES, but under `'use client'` + Vite the runtime
// barrel drops the freshly-added symbol — so list it explicitly, like
// MarkdownMessage. (Symbols imported directly by MessageBubble — e.g.
// AutoLinkPreview — ride its chunk and don't need this.)
export { ToolPayloadValue } from './messages/ToolPayloadValue';

// Bridge — AI-driven `point` directives (lightweight, no heavy deps).
export {
  HighlightOverlay,
  SpotlightCanvas,
  useHighlightTargets,
  resolveRefs,
  useChatDirectives,
  pushDirectives,
  clearDirectives,
  parseDirectives,
  installChatBridge,
  registerBridgeCommand,
  getBridgeCommand,
  setBridgeResolver,
  setBridgeSender,
  type BridgeCommand,
  type BridgeSender,
  type ChatBridge,
  type HighlightOverlayProps,
  type SpotlightCanvasProps,
  type RefResolver,
  type PointDirective,
  type HighlightTarget,
  type SpotlightRect,
  type CSTRefId,
} from '../../lib/browser-bridge';

// Page-context snapshot — capture engine + React surface. The host
// wires `getChatMetadata` into `getDynamicMetadata` so the assistant
// receives a snapshot of the page the user is looking at.
export {
  PageSnapshotProvider,
  usePageSnapshot,
  usePageSnapshotToggle,
  PageSnapshotChip,
  PageSnapshotPreview,
  PageSnapshotEngine,
  type PageSnapshotProviderProps,
  type PageSnapshotContextValue,
  type PageSnapshotToggle,
  type PageSnapshotChipProps,
  type PageSnapshotPreviewProps,
  type CaptureEngineOptions,
  type CaptureResult,
  type PageContextPayload,
} from '../../lib/page-snapshot';
