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

if (! function_exists('puffergo_render_menu_branch_desktop')) {
    function puffergo_render_menu_branch_desktop(array $pgo_items, $pgo_depth, $pgo_style, $pgo_path)
    {
        $max_depth = 2;

        if (in_array($pgo_style, array('default', 'stacked'), true) && 1 === $pgo_depth) {
            $pgo_has_subtree = false;

            foreach ($pgo_items as $pgo_probe) {
                if (! empty($pgo_probe->children) && is_array($pgo_probe->children)) {
                    $pgo_has_subtree = true;
                    break;
                }
            }

if ($pgo_has_subtree) { ?>
				<div style="display: flex; align-items: stretch;">
					<div style="flex: 0 0 15rem; width: 15rem; padding: 0.25rem 0; border-right: 1px solid color-mix(in srgb, var(--puffergo-header-text, #171717) 10%, transparent);">
						<?php
            foreach ($pgo_items as $pgo_li => $pgo_litem) : $pgo_lpath = $pgo_path . '-' . $pgo_li;
                $pgo_lkids = ! empty($pgo_litem->children) && is_array($pgo_litem->children); ?>
							<a
								href="<?php echo esc_url($pgo_litem->url); ?>"
								class="puffergo-default-header__dropdown-item flex items-center justify-between gap-3 px-4 py-3 text-sm font-semibold no-underline transition-colors"
								style="color: var(--puffergo-header-text, #171717);"
								<?php if ($pgo_lkids) : ?>
								@mouseenter="activeSub = '<?php echo esc_js($pgo_lpath); ?>'"
								@focusin="activeSub = '<?php echo esc_js($pgo_lpath); ?>'"
								:style="{ backgroundColor: activeSub === '<?php echo esc_js($pgo_lpath); ?>' ? 'color-mix(in srgb, var(--puffergo-header-text, #171717) 8%, transparent)' : '' }"
								<?php endif; ?>
							>
								<span class="flex items-center gap-3">
									<?php
                $pgo_licon = puffergo_get_menu_icon_markup($pgo_litem->icon, 'size-5 shrink-0 opacity-50');

                if ('' !== $pgo_licon) {
                    echo $pgo_licon;
                } ?>
									<span><?php echo esc_html($pgo_litem->title); ?></span>
								</span>
								<?php if ($pgo_lkids) : ?>
									<svg viewBox="0 0 20 20" fill="currentColor" aria-hidden="true" class="size-5 flex-none opacity-40">
										<path d="M7.21 14.77a.75.75 0 0 1 .02-1.06L11.168 10 7.23 6.29a.75.75 0 1 1 1.04-1.08l4.5 4.25a.75.75 0 0 1 0 1.08l-4.5 4.25a.75.75 0 0 1-1.06-.02Z" clip-rule="evenodd" fill-rule="evenodd" />
									</svg>
								<?php endif; ?>
							</a>
							<?php
            endforeach; ?>
					</div>
					<div style="flex: 1 1 auto; min-width: 0; padding: 0.25rem 0.5rem; min-height: 16rem;">
						<?php
            foreach ($pgo_items as $pgo_ri => $pgo_ritem) : $pgo_rkids = ! empty($pgo_ritem->children) && is_array($pgo_ritem->children);

                if (! $pgo_rkids) {
                    continue;
                } $pgo_rpath = $pgo_path . '-' . $pgo_ri; ?>
							<div x-cloak x-show="activeSub === '<?php echo esc_js($pgo_rpath); ?>'">
								<?php puffergo_render_menu_branch_desktop($pgo_ritem->children, $max_depth, $pgo_style, $pgo_rpath); ?>
							</div>
							<?php
            endforeach; ?>
					</div>
				</div>
				<?php
            return;
            }
        }

foreach ($pgo_items as $pgo_i => $pgo_item) {
            $pgo_item_path = $pgo_path . '-' . $pgo_i;
            $pgo_has_kids = ! empty($pgo_item->children) && is_array($pgo_item->children);

            if ('mega' === $pgo_style) {
                if ($pgo_has_kids) { ?>
					<div>
						<?php if (puffergo_is_clickable_parent($pgo_item)) : ?>
							<a href="<?php echo esc_url($pgo_item->url); ?>" class="block text-sm font-semibold no-underline" style="color: var(--puffergo-header-text);">
								<?php echo esc_html($pgo_item->title); ?>
							</a>
						<?php else : ?>
							<span class="block text-sm font-semibold" style="color: var(--puffergo-header-text);">
								<?php echo esc_html($pgo_item->title); ?>
							</span>
						<?php endif; ?>
						<div class="mt-4 space-y-3">
							<?php puffergo_render_menu_branch_desktop($pgo_item->children, $pgo_depth + 1, 'mega-column', $pgo_item_path); ?>
						</div>
					</div>
					<?php
                } else { ?>
					<?php $pgo_icon = puffergo_get_menu_icon_markup($pgo_item->icon, 'size-6'); ?>
					<a href="<?php echo esc_url($pgo_item->url); ?>" class="group pgo-header-flyout-item relative rounded-lg p-6 text-sm/6">
						<?php if ('' !== $pgo_icon) : ?>
							<div class="pgo-header-flyout-icon-tile flex size-11 items-center justify-center rounded-lg">
								<?php
                echo $pgo_icon; ?>
							</div>
						<?php endif; ?>
						<span class="<?php echo '' !== $pgo_icon ? 'mt-6' : ''; ?> block font-semibold" style="color: var(--puffergo-header-text);"><?php echo esc_html($pgo_item->title); ?></span>
						<?php if (! empty($pgo_item->description)) : ?>
							<p class="mt-1 opacity-60" style="color: var(--puffergo-header-text);"><?php echo esc_html($pgo_item->description); ?></p>
						<?php endif; ?>
					</a>
					<?php
                } continue;
            }

if ('mega-column' === $pgo_style) { ?>
				<a href="<?php echo esc_url($pgo_item->url); ?>" class="flex items-center gap-2 text-sm no-underline opacity-80 hover:opacity-100" style="color: var(--puffergo-header-text);">
					<?php echo esc_html($pgo_item->title); ?>
				</a>
				<?php
            continue;
            } $pgo_can_expand = $pgo_has_kids && $pgo_depth < $max_depth;

            if ($pgo_can_expand) { ?>
				<div
					x-data="{ pgoSubOpen: false }"
					@mouseenter="pgoSubOpen = true"
					@mouseleave="pgoSubOpen = false"
					@focusin="pgoSubOpen = true"
					@focusout="pgoSubOpen = false"
				>
					<?php if ('stacked' === $pgo_style) : ?>
						<?php $pgo_icon = puffergo_get_menu_icon_markup($pgo_item->icon, 'size-6 shrink-0 opacity-80 group-hover:opacity-100'); ?>
						<a href="<?php echo esc_url($pgo_item->url); ?>" class="group pgo-header-submenu-item" style="justify-content: space-between;">
							<span class="flex items-center gap-x-6">
								<?php if ('' !== $pgo_icon) : ?>
									<span class="pgo-header-icon-tile">
										<?php
            echo $pgo_icon; ?>
									</span>
								<?php endif; ?>
								<span class="block font-semibold" style="color: var(--puffergo-header-text, #f5f7ff);">
									<?php echo esc_html($pgo_item->title); ?>
								</span>
							</span>
							<svg viewBox="0 0 20 20" fill="currentColor" aria-hidden="true" class="size-5 flex-none opacity-60 transition-transform duration-200" :class="pgoSubOpen ? 'rotate-180' : ''">
								<path d="M5.22 8.22a.75.75 0 0 1 1.06 0L10 11.94l3.72-3.72a.75.75 0 1 1 1.06 1.06l-4.25 4.25a.75.75 0 0 1-1.06 0L5.22 9.28a.75.75 0 0 1 0-1.06Z" clip-rule="evenodd" fill-rule="evenodd" />
							</svg>
						</a>
						<div
							x-cloak
							x-show="pgoSubOpen"
							x-transition:enter="transition ease-out duration-150"
							x-transition:enter-start="opacity-0"
							x-transition:enter-end="opacity-100"
							class="mt-1 space-y-1 border-l pl-4 ml-6"
							style="border-color: color-mix(in srgb, var(--puffergo-header-text, #f5f7ff) 15%, transparent);"
						>
							<?php puffergo_render_menu_branch_desktop($pgo_item->children, $pgo_depth + 1, 'stacked', $pgo_item_path); ?>
						</div>
					<?php else : ?>
						<a
							href="<?php echo esc_url($pgo_item->url); ?>"
							class="puffergo-default-header__dropdown-item flex items-center justify-between gap-3 px-4 py-3 text-sm font-semibold no-underline transition-colors"
							style="color: var(--puffergo-header-text, #171717);"
						>
							<span class="flex items-center gap-3">
								<?php
            $pgo_icon = puffergo_get_menu_icon_markup($pgo_item->icon, 'size-5 shrink-0 opacity-50');

					    if ('' !== $pgo_icon) {
					        echo $pgo_icon;
					    } ?>
								<span><?php echo esc_html($pgo_item->title); ?></span>
							</span>
							<svg viewBox="0 0 20 20" fill="currentColor" aria-hidden="true" class="size-5 flex-none opacity-50 transition-transform duration-200" :class="pgoSubOpen ? 'rotate-180' : ''">
								<path d="M5.22 8.22a.75.75 0 0 1 1.06 0L10 11.94l3.72-3.72a.75.75 0 1 1 1.06 1.06l-4.25 4.25a.75.75 0 0 1-1.06 0L5.22 9.28a.75.75 0 0 1 0-1.06Z" clip-rule="evenodd" fill-rule="evenodd" />
							</svg>
						</a>
						<div
							x-cloak
							x-show="pgoSubOpen"
							x-transition:enter="transition ease-out duration-150"
							x-transition:enter-start="opacity-0"
							x-transition:enter-end="opacity-100"
							class="border-l pl-4"
							style="margin-left: 1.25rem; border-color: color-mix(in srgb, var(--puffergo-header-text, #171717) 12%, transparent);"
						>
							<?php puffergo_render_menu_branch_desktop($pgo_item->children, $pgo_depth + 1, 'default', $pgo_item_path); ?>
						</div>
					<?php endif; ?>
				</div>
				<?php
            } elseif ('stacked' === $pgo_style) { ?>
				<?php $pgo_icon = puffergo_get_menu_icon_markup($pgo_item->icon, 'size-6 shrink-0 opacity-80 group-hover:opacity-100'); ?>
				<a href="<?php echo esc_url($pgo_item->url); ?>" class="group pgo-header-submenu-item">
					<?php if ('' !== $pgo_icon) : ?>
						<span class="pgo-header-icon-tile">
							<?php
            echo $pgo_icon; ?>
						</span>
					<?php endif; ?>
					<span class="flex-auto">
						<span class="block font-semibold" style="color: var(--puffergo-header-text, #f5f7ff);">
							<?php echo esc_html($pgo_item->title); ?>
						</span>
					</span>
				</a>
				<?php
            } else { ?>
				<a href="<?php echo esc_url($pgo_item->url); ?>" class="puffergo-default-header__dropdown-item block px-4 py-3 text-sm font-semibold no-underline transition-colors" style="color: var(--puffergo-header-text, #171717);">
					<span class="flex items-center gap-3">
						<?php
            $pgo_icon = puffergo_get_menu_icon_markup($pgo_item->icon, 'size-5 shrink-0 opacity-50');

                if ('' !== $pgo_icon) {
                    echo $pgo_icon;
                } ?>
						<span><?php echo esc_html($pgo_item->title); ?></span>
					</span>
				</a>
				<?php
            }
        }
    }
} puffergo_render_menu_branch_desktop($pgo_branch_items, $pgo_branch_depth, $pgo_branch_style, $pgo_branch_path);
