/**
 * UpSolution Element: Add to Favorites
 */
! function( $, _undefined ) {
	"use strict";

	window.$us = window.$us || {};

	/**
	 * @class AddToFavorites
	 * @param {String|Node} container
	 */
	function AddToFavorites( container ) {
		let self = this;

		self.$wrapperFavs = $( container );
		self.$btnFavs = $( '.w-btn.us_add_to_favs', container );
		self.$tooltipNotLoggedIn = $( '.us-add-to-favs-tooltip.not-logged-in', container );
		self.$tooltipAfterAdding = $( '.us-add-to-favs-tooltip.message-after-adding', container );
		self.$btnFavsLabel = $( '.w-btn-label', self.$btnFavs );

		self.xhr = _undefined;
		self.data = {
			post_ID: 0,
			labelAfterAdding: '',
			labelBeforeAdding: '',
		};
		if ( self.$btnFavs.is( '[onclick]' ) ) {
			$.extend( self.data, self.$btnFavs[0].onclick() || {} );
		}

		// Delete data everywhere except for the preview of the usbuilder,
		// the data may be needed again to restore the elements.
		if ( ! $us.usbPreview() ) {
			self.$btnFavs.removeAttr( 'onclick' );
		}

		self._events = {
			toggleFavorites: self._toggleFavorites.bind( self ),
		};

		self.$btnFavs.on( 'click', self._events.toggleFavorites);

		self.setButtonState( self.getPostIDs().includes( self.data.post_ID ) );
	};

	// Add to Favorites API
	$.extend( AddToFavorites.prototype, {

		getPostIDs: function() {
			return $ush.toString( $us.userFavoritePostIds )
				.split( ',' )
				.map( $ush.parseInt )
				.filter( ( v ) => { return v } );
		},

		setButtonState: function( isAdded ) {
			let self = this;
			self.$btnFavs
				.removeAttr( 'disabled' )
				.removeClass( 'loading' );
			if ( isAdded ) {
				self.$btnFavs.addClass( 'added' );
				self.$btnFavsLabel.text( self.data.labelAfterAdding );
			} else {
				self.$btnFavs.removeClass( 'added' );
				self.$btnFavsLabel.text( self.data.labelBeforeAdding );
			}
		},

		_toggleFavorites: function( e ) {
			e.preventDefault();
			e.stopPropagation();
			let self = this;

			if ( self.$tooltipNotLoggedIn.length ) {
				self.toggleTooltip( self.$tooltipNotLoggedIn );
				return;
			}

			let postIDs = self.getPostIDs(),
				index = postIDs.indexOf( self.data.post_ID );
			if ( index > -1 ) {
				postIDs.splice( index, 1 );
			} else {
				postIDs.push( self.data.post_ID );
			}
			$us.userFavoritePostIds = postIDs.join( ',' );

			self.setButtonState( ! self.$btnFavs.hasClass( 'added' ) );

			if ( self.$btnFavs.hasClass( 'added' ) && self.$tooltipAfterAdding.length ) {
				self.toggleTooltip( self.$tooltipAfterAdding );
			} else {
				self.$tooltipAfterAdding.removeClass( 'show' );
			}

			if ( ! $ush.isUndefined( self.xhr ) ) {
				self.xhr.abort();
			}
			self.xhr = $.ajax( {
				url: $us.ajaxUrl,
				type: 'POST',
				data: {
					action: 'us_add_post_to_favorites',
					post_id: self.data.post_ID,
				}
			} );
		},

		toggleTooltip: function( tooltip ) {
			var removeTooltip = function() {
				$us.$window.off( 'click', removeTooltip );
				if ( tooltip.hasClass( 'show' ) ) {
					tooltip.removeClass( 'show' );
				}
			};

			tooltip.addClass( 'show' );

			$ush.timeout( () => {
				$us.$window.on( 'click', removeTooltip );
			}, 1 );
		}
	} );

	$.fn.usAddToFavorites = function() {
		return this.each( function() {
			$( this ).data( 'usAddToFavorites', new AddToFavorites( this ) );
		} );
	};

	$( function() {
		$( '.w-btn-wrapper.for_add_to_favs' ).usAddToFavorites();
	} );

}( jQuery );
