<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template-scope locals in an included view are not plugin globals.
$toolbar_model    = isset( $toolbar_view_model ) && is_array( $toolbar_view_model ) ? $toolbar_view_model : [];
$toolbar_surface  = 'settings' === ( $toolbar_model['surface'] ?? '' ) ? 'settings' : 'studio';
$toolbar_route    = isset( $toolbar_model['route'] ) && is_array( $toolbar_model['route'] ) ? $toolbar_model['route'] : [];
$toolbar_options  = isset( $toolbar_model['workflowOptions'] ) && is_array( $toolbar_model['workflowOptions'] ) ? $toolbar_model['workflowOptions'] : [];
$toolbar_workflow = sanitize_key( (string) ( $toolbar_route['workflow'] ?? '' ) );
$toolbar_selected = isset( $toolbar_options[ $toolbar_workflow ] ) && is_array( $toolbar_options[ $toolbar_workflow ] )
	? $toolbar_options[ $toolbar_workflow ]
	: null;
$toolbar_available = ! empty( $toolbar_model['available'] ) && null !== $toolbar_selected;
$toolbar_label     = $toolbar_available
	? (string) ( $toolbar_selected['label'] ?? '' )
	: __( 'No workflow available', 'fotogenic-ai' );
$fotogenic_ai_header_settings_url = (string) ( $toolbar_model['settingsUrl'] ?? \Fotogenic_AI\fotogenic_ai_get_settings_url() );
?>
<header
	data-fa-shared-toolbar
	data-fa-toolbar-mode="<?php echo esc_attr( $toolbar_surface ); ?>"
	data-fa-unavailable-label="<?php esc_attr_e( 'No workflow available', 'fotogenic-ai' ); ?>"
	data-fa-settings-base-url="<?php echo esc_url( (string) ( $toolbar_model['settingsBaseUrl'] ?? '' ) ); ?>"
>
	<div data-fa-studio-toolbar>
		<div data-fa-studio-identity>
			<img
				data-fa-studio-logo
				src="<?php echo esc_url( \Fotogenic_AI\Settings_Page::get_logo_url() ); ?>"
				width="178"
				alt="<?php esc_attr_e( 'Fotogenic AI', 'fotogenic-ai' ); ?>"
			/>
			<span data-fa-studio-divider aria-hidden="true"></span>
			<span data-fa-studio-name><?php esc_html_e( 'Studio', 'fotogenic-ai' ); ?></span>
		</div>

		<div data-fa-workflow-selector>
			<select
				id="fotogenic-ai-studio-target"
				data-fa-control="workflow"
				aria-hidden="true"
				tabindex="-1"
				hidden
				<?php disabled( ! $toolbar_available ); ?>
			>
				<?php foreach ( $toolbar_options as $workflow_id => $workflow_option ) : ?>
					<option
						value="<?php echo esc_attr( (string) $workflow_id ); ?>"
						<?php selected( $toolbar_workflow, (string) $workflow_id ); ?>
						<?php disabled( empty( $workflow_option['enabled'] ) ); ?>
					>
						<?php echo esc_html( (string) ( $workflow_option['label'] ?? $workflow_id ) ); ?>
					</option>
				<?php endforeach; ?>
			</select>
			<button
				type="button"
				data-fa-workflow-trigger
				aria-haspopup="menu"
				aria-expanded="false"
				aria-controls="fotogenic-ai-studio-workflow-menu"
				<?php disabled( ! $toolbar_available ); ?>
			>
				<span data-fa-workflow-label><?php echo esc_html( $toolbar_label ); ?></span>
				<span class="fotogenic-ai-icon fotogenic-ai-icon--caret-down fotogenic-ai-icon--bold fotogenic-ai-icon--size-17" data-fa-workflow-caret aria-hidden="true"></span>
			</button>
			<div
				id="fotogenic-ai-studio-workflow-menu"
				data-fa-workflow-menu
				role="menu"
				aria-label="<?php esc_attr_e( 'Choose workflow', 'fotogenic-ai' ); ?>"
				hidden
			>
				<?php foreach ( $toolbar_options as $workflow_id => $workflow_option ) : ?>
					<button
						type="button"
						data-fa-workflow-option="<?php echo esc_attr( (string) $workflow_id ); ?>"
						data-fa-server-enabled="<?php echo empty( $workflow_option['enabled'] ) ? '0' : '1'; ?>"
						data-fa-workflow-url="<?php echo esc_url( (string) ( $workflow_option['url'] ?? '' ) ); ?>"
						role="menuitemradio"
						aria-checked="<?php echo $toolbar_workflow === (string) $workflow_id ? 'true' : 'false'; ?>"
						<?php disabled( empty( $workflow_option['enabled'] ) ); ?>
					>
						<span class="fotogenic-ai-icon fotogenic-ai-icon--<?php echo esc_attr( (string) ( $workflow_option['icon'] ?? 'image-square' ) ); ?> fotogenic-ai-icon--regular fotogenic-ai-icon--size-30" data-fa-workflow-option-icon aria-hidden="true"></span>
						<span data-fa-workflow-option-copy>
							<strong><?php echo esc_html( (string) ( $workflow_option['label'] ?? $workflow_id ) ); ?></strong>
							<small><?php echo esc_html( (string) ( $workflow_option['description'] ?? '' ) ); ?></small>
						</span>
						<span class="fotogenic-ai-icon fotogenic-ai-icon--check fotogenic-ai-icon--bold fotogenic-ai-icon--size-21" data-fa-workflow-option-check aria-hidden="true"></span>
					</button>
				<?php endforeach; ?>
			</div>
		</div>

		<?php require FOTOGENIC_AI_PATH . 'templates/shared/header-actions.php'; ?>
	</div>
</header>
<?php // phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound ?>
