(function ($) {
	"use strict";
	var WidgetToc = function ($scope, $) {
			var content = $scope.find( '.ecafe-table-of-content' ).eq( 0 );
		if (content.length > 0) {
			var datacollects = content.data( 'datacollects' );
			tocbot.init(
				{
						...datacollects
				}
			);

			if (datacollects.positionFixedSelector === null) {
				if (datacollects.positionFixedSelectorCustom === 'container') {
					var container       = $scope.closest( '.elementor-element.e-con.e-parent' ).eq( 0 );
					var containerTop    = container.offset().top;
					var containerBottom = containerTop + container.outerHeight();

					$( window ).on(
						'scroll',
						function () {
							var scrollTop = $( window ).scrollTop();
							var tocHeight = content.outerHeight();
							if (scrollTop >= containerTop && scrollTop <= containerBottom - tocHeight) {
								content.addClass( 'is-position-fixed' );
							} else {
								content.removeClass( 'is-position-fixed' );
							}
						}
					);
				} else if (datacollects.positionFixedSelectorCustom === 'active') {
					$( window ).on(
						'scroll',
						function () {
							var customOffset = datacollects.afterScrollOffset;
							if (window.scrollY > customOffset) {
								content.addClass( 'is-position-fixed' );
							} else {
								content.removeClass( 'is-position-fixed' );
							}
						}
					);
				}
			}
		}
	};
	$( window ).on(
		'elementor/frontend/init',
		function () {
			elementorFrontend.hooks.addAction( 'frontend/element_ready/ecafe-toc.default', WidgetToc );
		}
	);
})( jQuery );