import { ContextProvider } from "@videojs/element/context";
import { isNull } from "@videojs/utils/predicate";

//#region src/store/provider-mixin.ts
/**
* Create a mixin that provides player context to descendant elements.
*
* @param context - Player context to provide to descendants.
* @param factory - Factory function that creates a store instance.
*/
function createProviderMixin(context, factory) {
	return (BaseClass) => {
		class PlayerProviderElement extends BaseClass {
			#store = factory();
			#provider = new ContextProvider(this, {
				context,
				initialValue: this.store
			});
			get store() {
				if (isNull(this.#store)) this.#store = factory();
				return this.#store;
			}
			connectedCallback() {
				super.connectedCallback();
				this.#provider.setValue(this.store);
			}
			destroyCallback() {
				this.#store?.destroy();
				this.#store = null;
				super.destroyCallback();
			}
		}
		return PlayerProviderElement;
	};
}

//#endregion
export { createProviderMixin };
//# sourceMappingURL=provider-mixin.js.map