import { ContextConsumer } from "@videojs/element/context";
import { StoreController } from "@videojs/store/html";

//#region src/player/player-controller.ts
/**
* Reactive controller for accessing player store state.
*
* Without selector: Returns the store, does NOT subscribe to changes.
* With selector: Returns selected state, subscribes with shallowEqual comparison.
*
* @example
* ```ts
* // Store access (no subscription)
* class Controls extends MediaElement {
*   #player = new PlayerController(this, playerContext);
*
*   handleClick() {
*     this.#player.value.setVolume(0.5);
*   }
* }
*
* // Selector-based subscription
* class PlayButton extends MediaElement {
*   #playback = new PlayerController(this, playerContext, selectPlayback);
* }
* ```
*/
var PlayerController = class {
	#host;
	#selector;
	#consumer;
	#store = null;
	constructor(host, context, selector) {
		this.#host = host;
		this.#selector = selector;
		this.#consumer = new ContextConsumer(host, {
			context,
			callback: (ctx) => this.#connect(ctx),
			subscribe: true
		});
		host.addController(this);
	}
	get value() {
		const store = this.#consumer.value;
		if (!store) return void 0;
		if (!this.#selector) return store;
		return this.#store?.value;
	}
	get displayName() {
		return this.#selector?.displayName;
	}
	hostConnected() {
		const store = this.#consumer.value;
		if (store) this.#connect(store);
	}
	hostDisconnected() {
		this.#store = null;
	}
	#connect(store) {
		if (!this.#store && this.#selector) this.#store = new StoreController(this.#host, store, this.#selector);
	}
};

//#endregion
export { PlayerController };
//# sourceMappingURL=player-controller.js.map