/**
 * @hypen-space/core - Hypen Core Engine
 *
 * Platform-agnostic reactive UI runtime.
 * Use with @hypen/web for browser rendering, or build your own renderer.
 *
 * ## Quick Start (Single-File Component)
 *
 * ```typescript
 * import { app, hypen, state } from "@hypen-space/core";
 *
 * export default app
 *   .defineState({ count: 0 })
 *   .onAction("increment", ({ state }) => {
 *     state.count += 1;
 *   })
 *   .ui(hypen`
 *     Column {
 *       Text("Count: ${state.count}")
 *       Button { Text("+") }
 *         .onClick("@actions.increment")
 *     }
 *   `);
 * ```
 *
 * ## Two-File Component (Legacy)
 *
 * ```typescript
 * // component.ts
 * import { app } from "@hypen-space/core";
 *
 * export default app
 *   .defineState({ count: 0 })
 *   .onAction("increment", ({ state }) => {
 *     state.count++;
 *   })
 *   .build();
 *
 * // component.hypen (separate file)
 * // Column { Text("Count: ${state.count}") ... }
 * ```
 */

// ============================================================================
// CORE TYPES (WASM-free)
// ============================================================================

export type {
  Patch,
  Action,
  RenderCallback,
  ActionHandler as EngineActionHandler,
  ResolvedComponent,
  ComponentResolver as EngineComponentResolver,
} from "./types.js";

// NOTE: Engine and BrowserEngine are NOT exported from the barrel.
// Import them explicitly from their subpaths to avoid pulling in WASM:
//   import { Engine } from "@hypen-space/core/engine"
//   import { Engine } from "@hypen-space/core/engine/browser"

// ============================================================================
// APP / MODULE SYSTEM
// ============================================================================

export { app, HypenApp, HypenAppBuilder, HypenModuleInstance } from "./app.js";

// ============================================================================
// TEMPLATE SYSTEM (Single-File Components)
// ============================================================================

export { hypen, state, item, index } from "./hypen.js";
export type { StateProxy, ItemProxy } from "./hypen.js";

export type {
  IEngine,
  ActionContext,
  GlobalContext,
  LifecycleHandler,
  ActionHandlerContext,
  ActionHandler,
  HypenModuleDefinition,
  HypenModule,
  // Session lifecycle hooks
  DisconnectContext,
  ReconnectContext,
  ExpireContext,
  DisconnectHandler,
  ReconnectHandler,
  ExpireHandler,
  // Error handling
  ErrorContext,
  ErrorHandler,
  ErrorHandlerResult,
  // Data source access in action handlers
  DataSourceAccessor,
} from "./app.js";

// ============================================================================
// DATA SOURCE PLUGIN SYSTEM
// ============================================================================

export { DataSourceManager } from "./datasource.js";
export type {
  DataSourcePlugin,
  DataSourceStatus,
  DataSourceQuery,
  DataSourceSubscription,
  DataSourceChange,
  IDataSourceEngine,
} from "./datasource.js";

// ============================================================================
// STATE MANAGEMENT
// ============================================================================

export {
  createObservableState,
  batchStateUpdates,
  getStateSnapshot,
  isStateProxy,
  unwrapProxy,
} from "./state.js";
export type {
  StatePath,
  StateChange,
  StateObserverOptions,
} from "./state.js";

// ============================================================================
// RENDERER ABSTRACTION
// ============================================================================

export { BaseRenderer, ConsoleRenderer } from "./renderer.js";
export type { Renderer } from "./renderer.js";

// ============================================================================
// ROUTING
// ============================================================================

export { HypenRouter } from "./router.js";
export type {
  RouteMatch,
  RouteState,
  RouteChangeCallback,
} from "./router.js";

// ============================================================================
// EVENTS
// ============================================================================

export { TypedEventEmitter, createEventEmitter } from "./events.js";
export type { EventHandler, HypenFrameworkEvents } from "./events.js";

// ============================================================================
// GLOBAL CONTEXT
// ============================================================================

export { HypenGlobalContext } from "./context.js";
export type { ModuleReference } from "./context.js";

// ============================================================================
// REMOTE UI
// ============================================================================

// Client (browser-safe)
export { RemoteEngine } from "./remote/client.js";
export type {
  RemoteConnectionState,
  RemoteEngineOptions,
  SessionOptions,
  SessionInfo,
} from "./remote/client.js";

// NOTE: RemoteServer and serve are NOT exported from the barrel because they
// import the Node.js WASM engine. Import them from the subpath instead:
//   import { RemoteServer, serve } from "@hypen-space/core/remote/server"

// Session Management
export { SessionManager } from "./remote/session.js";
export type { SessionExpireCallback } from "./remote/session.js";

// Types
export type {
  RemoteMessage,
  InitialTreeMessage,
  PatchMessage,
  DispatchActionMessage,
  StateUpdateMessage,
  HelloMessage,
  SessionAckMessage,
  SessionExpiredMessage,
  Session,
  SessionConfig,
  RemoteClient,
  RemoteServerConfig,
} from "./remote/types.js";

// ============================================================================
// COMPONENT RESOLVER (browser-safe, no fs dependency)
// ============================================================================

export { ComponentResolver } from "./resolver.js";
export type {
  ImportStatement,
  ImportClause,
  ImportSource,
  ComponentDefinition as ResolverComponentDefinition,
  ResolverOptions,
} from "./resolver.js";

// ============================================================================
// NODE-ONLY MODULES (NOT exported from barrel — use subpath imports)
// ============================================================================
//
// These modules use Node.js APIs (fs.readFileSync) and must NOT be in the
// browser-safe barrel. Import them from their subpaths instead:
//
//   import { ComponentLoader, componentLoader } from "@hypen-space/core/loader"
//   import { discoverComponents, ... } from "@hypen-space/core/discovery"
//   import { hypenPlugin, ... } from "@hypen-space/core/plugin"
//

// Re-export types only (types are always safe)
export type { ComponentDefinition } from "./loader.js";
export type {
  DiscoveredComponent,
  DiscoveryOptions,
  WatchOptions,
} from "./discovery.js";
export type { HypenPluginOptions } from "./plugin.js";

// ============================================================================
// BUILT-IN COMPONENTS
// ============================================================================

export { Router, Route, Link } from "./components/builtin.js";

// ============================================================================
// RESULT TYPE (Error Handling)
// ============================================================================

export {
  Ok,
  Err,
  isOk,
  isErr,
  fromPromise,
  fromTry,
  map,
  mapErr,
  flatMap,
  unwrap,
  unwrapOr,
  unwrapOrElse,
  match,
  all,
  HypenError,
  ActionError,
  ConnectionError,
  StateError,
  ParseError,
  RenderError,
  classifyEngineError,
} from "./result.js";
export type { Result } from "./result.js";

// ============================================================================
// DISPOSABLE PATTERN (Resource Management)
// ============================================================================

export {
  DisposableStack,
  DisposableMixin,
  isDisposable,
  disposableListener,
  disposableTimeout,
  disposableInterval,
  disposableWebSocket,
  disposableAbortController,
  disposableSubscription,
  getElementDisposables,
  disposeElement,
  hasElementDisposables,
  compositeDisposable,
  using,
  usingSync,
} from "./disposable.js";
export type { Disposable } from "./disposable.js";

// ============================================================================
// Retry Utility
// ============================================================================

export {
  retry,
  retryResult,
  withRetry,
  RetryConditions,
  RetryPresets,
} from "./retry.js";
export type { RetryOptions } from "./retry.js";

// ============================================================================
// Logger
// ============================================================================

export {
  Logger,
  createLogger,
  logger,
  log,
  frameworkLoggers,
  setLogLevel,
  getLogLevel,
  configureLogger,
  enableLogging,
  disableLogging,
  setDebugMode,
  isDebugMode,
} from "./logger.js";
export type { LogLevel, LoggerConfig, LogHandler } from "./logger.js";
