(function ($) {
	$(document).ready(function () {
		var container_width_elements =
			'body .wp-block,\
			.editor-styles-wrapper .editor-post-title__input,\
			.editor-styles-wrapper .wp-block-post-title,\
			.editor-styles-wrapper .editor-visual-editor__post-title-wrapper > :where(:not(.alignleft):not(.alignright):not(.alignfull)),\
			.editor-styles-wrapper .editor-default-block-appender,\
			.editor-styles-wrapper .editor-block-list__block,\
			.editor-styles-wrapper .block-editor-block-list__block,\
			.editor-styles-wrapper .block-editor-block-list__layout.is-root-container > :where(:not(.alignleft):not(.alignright):not(.alignfull))';

		function addStylesToIframe(css, id) {
			var iframe = document.querySelector('iframe[name="editor-canvas"]');
			if (iframe && iframe.contentDocument) {
				var iframeDoc = iframe.contentDocument;
				// Remove existing styles with the same ID
				var existingStyle = iframeDoc.getElementById(id);
				if (existingStyle) {
					existingStyle.remove();
				}
				// Add new style
				var style = iframeDoc.createElement('style');
				style.id = id;
				style.textContent = css;
				iframeDoc.head.appendChild(style);
			}
		}

		$(document).on('change', 'input[name="nevo_page_settings[content_container]"], [name="nevo_page_settings[sidebar_layout]"] input[type="radio"]', function () {
			var sidebarLayoutValue = $('input[name="nevo_page_settings[sidebar_layout]"]:checked').val();
			var contentContainerValue = $('input[name="nevo_page_settings[content_container]"]:checked').val();

			if (sidebarLayoutValue !== 'content-narrow' && contentContainerValue === 'true') {
				var css = container_width_elements + '{max-width: 100%;}';
				addStylesToIframe(css, 'container_width');
			} else {
				addStylesToIframe('', 'container_width');
			}
		});

		$('[name="nevo_page_settings[sidebar_layout]"]').on('change', function () {
			var this_value = this.value,
				content_width = '',
				calc = '',
				narrow_width = nevo_block_editor.narrow_width,
				container_width = nevo_block_editor.container_width,
				right_sidebar_width = nevo_block_editor.right_sidebar_width,
				left_sidebar_width = nevo_block_editor.left_sidebar_width,
				right_content_padding = nevo_block_editor.content_padding_right,
				left_content_padding = nevo_block_editor.content_padding_left;

			if ('' === this_value) {
				this_value = nevo_block_editor.global_sidebar_layout;
			}

			if ('content-narrow' == this_value) {
				content_width = narrow_width;
			} else if ('left-sidebar' == this_value) {
				content_width = container_width * ((100 - left_sidebar_width) / 100);
			} else if ('right-sidebar' == this_value) {
				content_width = container_width * ((100 - right_sidebar_width) / 100);
			} else if ('no-sidebar' == this_value) {
				content_width = container_width;
			} else {
				content_width = container_width * ((100 - (Number(left_sidebar_width) + Number(right_sidebar_width))) / 100);
			}

			calc = 'max-width: calc(' + content_width + 'px - ' + right_content_padding + ' - ' + left_content_padding + ')';

			var contentCSS = container_width_elements + '{' + calc + '}';
			addStylesToIframe(contentCSS, 'content_width');

			var wideCSS = '.edit-post-visual-editor .editor-block-list__block[data-align=wide]{max-width:' + content_width + 'px}';
			addStylesToIframe(wideCSS, 'wide_width');
		});
	});
})(jQuery);
