(function ($) {
	'use strict';

	const presetAlignmentMap = {
		'clean-minimal': 'center',
		'underline-focus': 'left'
	};

	const applyPresetAlignment = function (model) {
		const preset = model.getSetting('design_preset');
		const alignment = presetAlignmentMap[preset];

		if (!alignment) {
			return;
		}

		model.setSetting('alignment', alignment);
	};

	const applyDownloadableText = function (model) {
		const style = model.getSetting('design_style');
		const text = model.getSetting('button_text');

		if (style !== 'downloadable') {
			return;
		}

		if (!text || text === 'Read More' || text === 'read more') {
			model.setSetting('button_text', 'Download');
		}
	};

	elementor.hooks.addAction('panel/open_editor/widget', function (panel, model) {
		if (!model || model.get('widgetType') !== 'impact_widgets_elementor_advanced-heading') {
			if (!model || model.get('widgetType') !== 'impact_widgets_elementor_advanced-button') {
				return;
			}

			applyDownloadableText(model);
			model.off('change:design_style.impact-widgets-elementor');
			model.on('change:design_style.impact-widgets-elementor', function () {
				applyDownloadableText(model);
			});
			return;
		}

		applyPresetAlignment(model);

		model.off('change:design_preset.impact-widgets-elementor');
		model.on('change:design_preset.impact-widgets-elementor', function () {
			applyPresetAlignment(model);
		});
	});
})(jQuery);
