import '../../../core/infra/player_engine/player_engine.dart';
import 'player_engine_resolution.dart';


class PlayerEngineHolder {
  PlayerEngine? _engine;
  PlayerEngineKind? _kind;


  PlayerEngine? get current => _engine;


  Future<PlayerEngine> acquire(
    PlayerEngineKind kind,
    PlayerEngine Function() create,
  ) async {
    final existing = _engine;
    if (existing != null && _kind == kind) {
      return existing;
    }

    _engine = null;
    _kind = null;
    if (existing != null) {
      await existing.dispose();
    }

    final next = create();
    _engine = next;
    _kind = kind;
    return next;
  }


  Future<void> dispose() async {
    final engine = _engine;
    _engine = null;
    _kind = null;
    if (engine != null) {
      await engine.dispose();
    }
  }
}
