/**
 * @file assets/js/mbs-front-cover-video.js
 * Hover-play behavior for spinning book cover videos (PRO).
 *
 * On mouseenter: plays the <video> child (opacity transition handled by CSS).
 * On mouseleave: pauses and resets to frame 0.
 * Uses event delegation so covers injected after DOMContentLoaded are handled.
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

/* global jQuery */

( function( MBS, $, undefined ) {
	'use strict';

	/**
	 * Hover-play for .mbs-cover-video-hover covers.
	 *
	 * @namespace MBS.CoverVideo
	 */
	MBS.CoverVideo = {

		/**
		 * Bind mouse enter/leave event delegation.
		 *
		 * @return {void}
		 */
		init: function() {
			$( document ).on( 'mouseenter', '.mbs-cover-video-hover', this.onEnter );
			$( document ).on( 'mouseleave', '.mbs-cover-video-hover', this.onLeave );
		},

		/**
		 * Play the cover video on hover.
		 *
		 * @return {void}
		 */
		onEnter: function() {
			var video = this.querySelector( 'video.mbs-cover-video' );
			if ( ! video ) { return; }
			video.play().catch( function() {} ); // suppress blocked-autoplay DOMException
		},

		/**
		 * Pause and reset the cover video on mouse leave.
		 *
		 * @return {void}
		 */
		onLeave: function() {
			var video = this.querySelector( 'video.mbs-cover-video' );
			if ( ! video ) { return; }
			video.pause();
			video.currentTime = 0;
		}
	};

	MBS.register( function() {
		MBS.CoverVideo.init();
	} );

} )( window.MBS = window.MBS || {}, jQuery );
