/**
 * Player adapter factory + public exports.
 *
 * `createAdapter` selects the concrete PlayerAdapter for the current platform.
 * It is the single place that encodes the INTERIM Android override: the Rust
 * backend may report a native ExoPlayer backend, but native Android video
 * rendering is blocked upstream (tauri#10152 — transparent webview / SurfaceView
 * compositing), so we render Android video through the HTML5 adapter for now.
 * When that upstream limitation is resolved, flip this to honor `backendKind`.
 *
 * TRACES: UR-003 | DR-004
 */

import { Html5PlayerAdapter, type Html5ElementBridge } from "./html5Adapter";
import { NativePlayerAdapter } from "./nativeAdapter";
import type { AdapterHost, PlayerAdapter } from "./types";

export type { PlayerAdapter, AdapterHost, PlayerLoadOptions, SubtitleTrackInput } from "./types";
export type { Html5ElementBridge } from "./html5Adapter";
export { Html5PlayerAdapter } from "./html5Adapter";
export { NativePlayerAdapter } from "./nativeAdapter";

/** What the Rust `player_play_item` response says it chose. */
export type BackendKind = "html5" | "native";

export interface CreateAdapterArgs {
  /** Backend kind reported by `player_play_item` (`useHtml5Element`). */
  backendKind: BackendKind;
  host: AdapterHost;
  /** Required for the HTML5 adapter; ignored by the native adapter. */
  bridge?: Html5ElementBridge;
}

/**
 * Build the adapter for this platform/stream.
 *
 * INTERIM: always returns the HTML5 adapter, because the native surface is not
 * visible through the webview on current Tauri (see module docs). The bridge is
 * therefore required.
 */
export function createAdapter({ backendKind, host, bridge }: CreateAdapterArgs): PlayerAdapter {
  // INTERIM OVERRIDE: force HTML5 rendering even when the backend reports native.
  const effectiveKind: BackendKind = "html5";

  if (effectiveKind === "html5") {
    if (!bridge) {
      throw new Error("createAdapter: Html5ElementBridge is required for the HTML5 adapter");
    }
    return new Html5PlayerAdapter(host, bridge);
  }

  // Reached only once the interim override is lifted (native Android unblocked).
  void backendKind;
  return new NativePlayerAdapter(host);
}
