import { mediaContext } from "../player/context.js";
import { ContextEvent } from "@videojs/element/context";

//#region src/store/media-attach-mixin.ts
/**
* Create a mixin that consumes `mediaContext` and registers the
* element as the media with the provider.
*
* Uses the raw context-request protocol so it works with any
* `HTMLElement` subclass — no `ReactiveControllerHost` required.
*
* @param context - The media context to consume.
*/
function createMediaAttachMixin(context) {
	return (BaseClass) => {
		class MediaAttachElement extends BaseClass {
			#setMedia = null;
			#unsubscribe = null;
			getMediaTarget() {
				return this;
			}
			connectedCallback() {
				super.connectedCallback?.();
				this.dispatchEvent(new ContextEvent(context, this, (value, unsubscribe) => {
					if (unsubscribe) this.#unsubscribe = unsubscribe;
					this.#setMedia = value?.setMedia ?? null;
					if (this.isConnected) this.#setMedia?.(this.getMediaTarget());
				}, true));
			}
			disconnectedCallback() {
				super.disconnectedCallback?.();
				this.#setMedia?.(null);
				this.#unsubscribe?.();
				this.#unsubscribe = null;
				this.#setMedia = null;
			}
		}
		return MediaAttachElement;
	};
}
const MediaAttachMixin = createMediaAttachMixin(mediaContext);

//#endregion
export { MediaAttachMixin, createMediaAttachMixin };
//# sourceMappingURL=media-attach-mixin.js.map