(function ($) {
	var WidgetCouponCode = function ($scope, $) {
		if ($scope.find( '.ec-coupon-code-wrapper' ).length) {
			var copyButton = $scope.find( ".ec-cc-button" )[0],
				copyCoupon = $scope.find( ".ec-cc-coupon" )[0],
				ccoptions  = $scope.find( '.ec-coupon-code-wrapper' ).data( "ccoptions" );

			copyButton.onclick = function () {
				navigator.clipboard.writeText( copyCoupon.innerHTML );
				copyButton.innerHTML = ccoptions['buttonCopiedText'];
				if (ccoptions['couponRedirectUrl'] !== undefined && ccoptions['couponRedirectUrl'] !== '') {
					if (ccoptions['couponRedirectUrlType'] !== undefined && ccoptions['couponRedirectUrlType'] !== '' && ccoptions['couponRedirectUrlType'] == 'redirect') {
						window.location.href = ccoptions['couponRedirectUrl'];
					} else {
						window.open( ccoptions['couponRedirectUrl'] );
					}
				}
				setTimeout(
					function () {
						copyButton.innerHTML = ccoptions['buttonText'];
					},
					3000
				);
			};
		}
	};

	$( window ).on(
		'elementor/frontend/init',
		function () {
			elementorFrontend.hooks.addAction( 'frontend/element_ready/ecafe-coupon-code.default', WidgetCouponCode );
		}
	);
})( jQuery );