<?php
/**
 * Header context: single entry for all header template data (menu, current page, align, site identity, fullpage UI).
 *
 * @package PufferGo
 */

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

/**
 * 向后兼容别名：将旧调用方委托到全局品牌标识函数。
 * 新代码请直接调用 puffergo_get_site_brand_identity()。
 *
 * @deprecated 使用 puffergo_get_site_brand_identity() 替代。
 * @return array 与 puffergo_get_site_brand_identity() 返回值结构相同。
 */
function puffergo_get_header_logo_identity() {
	return puffergo_get_site_brand_identity();
}

/**
 * Get header context for template use. Single entry; optional blocks via $options.
 *
 * @param array $options Optional. Keys: with_nav_align (bool), with_site_identity (bool), with_fullpage_ui (bool).
 * @return array menu_items, current_page_id, home_url, is_current (callable), and optional blocks.
 */
function puffergo_get_header_context( array $options = array() ) {
	$current_page_id = get_queried_object_id();
	$home_url        = trailingslashit( home_url( '/' ) );

	$front_page_id = (int) get_option( 'page_on_front', 0 );
	$is_current = function( $item ) use ( $current_page_id, $home_url, $front_page_id ) {
		if ( in_array( $item->object, array( 'post', 'page' ), true ) ) {
			if ( (int) $item->object_id === (int) $current_page_id ) {
				return true;
			}
		}
		if ( is_front_page() ) {
			$item_url = trailingslashit( $item->url );
			if ( $item_url === $home_url ) {
				return true;
			}
			if ( $front_page_id > 0 && in_array( $item->object, array( 'post', 'page' ), true ) && (int) $item->object_id === $front_page_id ) {
				return true;
			}
		}
		return false;
	};

	$menu_items = puffergo_get_header_menu_items();

	$context = array(
		'menu_items'       => $menu_items,
		'has_menu'         => ! empty( $menu_items ),
		'current_page_id'  => $current_page_id,
		'home_url'         => $home_url,
		'is_current'        => $is_current,
	);

	if ( ! empty( $options['with_nav_align'] ) ) {
		$nav_align = get_option( 'puffergo_header_nav_align', 'right' );
		$nav_align = in_array( $nav_align, array( 'left', 'center', 'right' ), true ) ? $nav_align : 'right';
		$context['nav_align']         = $nav_align;
		$context['nav_align_class']  = 'puffergo-nav-align-' . $nav_align;
		$context['spacer_left_class'] = ( 'left' === $nav_align ) ? 'flex-none' : 'flex-1';
		$context['spacer_right_class'] = 'hidden lg:flex ' . ( ( 'right' === $nav_align ) ? 'flex-none' : 'flex-1' );
	}

	if ( ! empty( $options['with_site_identity'] ) ) {
		$context['site_name']        = get_bloginfo( 'name' );
		$context['site_description'] = get_bloginfo( 'description' );
		$logo_identity               = puffergo_get_header_logo_identity();
		$context['logo_id']    = $logo_identity['logo_id'];
		$context['logo_url']   = $logo_identity['logo_url'];
		$context['logo_alt']   = $logo_identity['logo_alt'];
		$context['brand_name'] = $logo_identity['brand_name'];
	}

	if ( ! empty( $options['with_fullpage_ui'] ) ) {
		$context['show_header_style_selector'] = ! empty( $GLOBALS['puffergo_fullpage_template'] ) && current_user_can( 'edit_posts' );
		$context['current_footer_template'] = class_exists( 'PufferGo_Template_Loader' )
			? PufferGo_Template_Loader::get_active_footer()
			: get_option( 'puffergo_footer_template', 'footer-3' );
	}

	return apply_filters( 'puffergo_header_context', $context, $options );
}

