<?php
/**
 * For some pages, this will hide the admin menus and bars.
 *
 * @package EaLab\WP_Deployer
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$page = sanitize_text_field( wp_unslash( $_GET['page'] ?? '' ) );
$tab = sanitize_text_field( wp_unslash( $_GET['tab'] ?? '' ) );
$embed_mode = sanitize_text_field( wp_unslash( $_GET['embed_mode'] ?? '' ) );

if ( 'deploy-sync-content' === $page
	&& in_array( $tab, array( 'diff', 'merge', 'tracking-queue-progress' ), true )
	&& '1' === $embed_mode ) {
	add_action(
		'admin_body_class',
		static function ( $classes ) {
			$classes .= ' deploy-sync-content--invisible-during-load';

			return $classes;
		}
	);

	add_action(
		'admin_print_styles',
		'deploy_sync_content__admin_print_styles__menuless_mode'
	);
}

/**
 * Action callback that adds styles for the menuless mode used by Deploy & Sync Content.
 */
function deploy_sync_content__admin_print_styles__menuless_mode(): void {
	/*
	 * This style block is injected into the <head> section to prevent content flashing
	 * during page load and ensure proper layout in menuless mode.
	 *
	 * The menuless mode is primarily used for embedding specific functionality
	 * (like diff/merge operations) within modal dialogs. While these features
	 * can be accessed as standalone pages with full admin menus, embedding them
	 * requires hiding the WordPress admin interface elements.
	 *
	 * The styles below:
	 * - Remove default WordPress admin padding and margins
	 * - Hide admin bars, menus, and navigation tabs
	 * - Adjust sticky positioning for elements
	 */
	?>
	<style>
        /* Deploy & Sync Content, menuless-mode. */
        html.wp-toolbar {
            /* Reset the padding-top because the top bar is hidden. */
            padding-top: initial;
        }

        div#wpcontent {
            /* Reset the margin-left because the admin menu bar is hidden. */
            margin-left: initial;
        }

        div#wpbody-content .wrap > :not(.deploy-sync-content--container),
        div#wpbody-content .wrap .deploy-sync-content--container .nav-tab-wrapper,
        #wpadminbar, #adminmenumain {
            display: none;
        }

        .deploy-sync-content--sticky {
            /* There is no top bar, so we stick to the top. */
            top: 0;
        }
	</style>
	<?php
}