/* global FusionApp, FusionEvents */
(function () {
	jQuery(document).ready(function () {
		FusionApp.listenTo(FusionEvents, 'fusion-settings-modal-open', fusionWooShortcodesSettingsHandler);

		function fusionWooShortcodesSettingsHandler() {
			setTimeout(function () {

				// Add a small delay in case the browser takes more than expected to render things.
				let fusionWooShortcodeSelector = jQuery('#fusion_fibofilters_shortcodes'),
					fusionWooShortcodeTextarea, fusionWooShortcodeDiv;
				if (fusionWooShortcodeSelector.length) {
					fusionWooShortcodeDiv = fusionWooShortcodeSelector.next();

					// Hide option with shortcode
					fusionWooShortcodeSelector.closest('ul').find('.fusion-builder-option.textarea').hide();

					fusionWooShortcodeTextarea = jQuery(fusionWooShortcodeSelector.parents('li')[0]).parent().find('textarea');
					fusionWooShortcodeSelector.on('change', function () {
						const wooShortCodes = ['[fibofilters]', '[fibofilters layout="horizontal"]'],
							selected = wooShortCodes[jQuery(this).val()];

						// Update content.
						fusionWooShortcodeTextarea.val(selected).html(selected).trigger('change');
					});
				}
			}, 50);
		}
	});
})(jQuery);
