<?php

use MintyPHP\Module\Bookmarks\Support\BookmarkUrlNormalizer;

$layoutNav = is_array($layoutNav ?? null) ? $layoutNav : [];
$bookmarkNav = is_array($layoutNav['bookmarks.nav'] ?? null) ? $layoutNav['bookmarks.nav'] : [];
$bookmarkData = is_array($bookmarkNav['grouped'] ?? null) ? $bookmarkNav['grouped'] : ['groups' => [], 'ungrouped' => []];
$bookmarkGroups = is_array($bookmarkData['groups'] ?? null) ? $bookmarkData['groups'] : [];
$bookmarkUngrouped = is_array($bookmarkData['ungrouped'] ?? null) ? $bookmarkData['ungrouped'] : [];
$currentPath = trim((string) ($bookmarkNav['current_path'] ?? ''));
?>
<div data-app-bookmark-panel data-app-component="bookmark-panel"
    data-bookmark-reorder-url="<?php e(lurl('bookmarks/reorder-data')); ?>"
    data-bookmark-group-delete-url="<?php e(lurl('bookmarks/group-delete-data')); ?>"
    data-bookmark-delete-url="<?php e(lurl('bookmarks/delete-data')); ?>"
    data-bookmark-msg-group-delete-confirm="<?php e(t('Delete group and keep bookmarks?')); ?>"
    data-bookmark-msg-group-deleted="<?php e(t('Group deleted')); ?>"
    data-bookmark-msg-group-updated="<?php e(t('Group updated')); ?>"
    data-bookmark-msg-bookmark-delete-confirm="<?php e(t('Delete this bookmark?')); ?>"
    data-bookmark-msg-bookmark-deleted="<?php e(t('Bookmark deleted')); ?>"
    data-bookmark-msg-group-action-failed="<?php e(t('Group action failed')); ?>"
    data-bookmark-msg-bookmark-action-failed="<?php e(t('Bookmark action failed')); ?>"
    data-bookmark-msg-group-delete-failed="<?php e(t('Group delete failed')); ?>"
    data-bookmark-msg-bookmark-delete-failed="<?php e(t('Bookmark action failed')); ?>"
    data-bookmark-msg-reorder-saved="<?php e(t('Reorder saved')); ?>"
    data-bookmark-msg-reorder-failed="<?php e(t('Reorder failed')); ?>"
    data-bookmark-label-delete="<?php e(t('Delete')); ?>">
    <?php if (!$bookmarkGroups && !$bookmarkUngrouped): ?>
        <?php
        $emptyState = [
            'message' => t('No bookmarks yet'),
            'size' => 'compact',
            'align' => 'center',
        ];
        require templatePath('partials/app-empty-state.phtml');
        ?>
    <?php else: ?>
        <?php
        $bookmarkRootItems = [];
        foreach ($bookmarkUngrouped as $bookmark) {
            $bookmarkId = (int) ($bookmark['id'] ?? 0);
            if ($bookmarkId <= 0) {
                continue;
            }
            $bookmarkRootItems[] = [
                'type' => 'bookmark',
                'id' => $bookmarkId,
                'sort_order' => (int) ($bookmark['sort_order'] ?? 0),
                'bookmark' => $bookmark,
            ];
        }

        foreach ($bookmarkGroups as $group) {
            $groupId = (int) ($group['id'] ?? 0);
            if ($groupId <= 0) {
                continue;
            }
            $bookmarkRootItems[] = [
                'type' => 'group',
                'id' => $groupId,
                'sort_order' => (int) ($group['sort_order'] ?? 0),
                'group' => $group,
            ];
        }

        usort($bookmarkRootItems, static function (array $a, array $b): int {
            $sortCmp = ((int) ($a['sort_order'] ?? 0)) <=> ((int) ($b['sort_order'] ?? 0));
            if ($sortCmp !== 0) {
                return $sortCmp;
            }
            $typeCmp = strcmp((string) ($a['type'] ?? ''), (string) ($b['type'] ?? ''));
            if ($typeCmp !== 0) {
                return $typeCmp;
            }
            return ((int) ($a['id'] ?? 0)) <=> ((int) ($b['id'] ?? 0));
        });

        $rootCount = count($bookmarkRootItems);
        ?>
        <ul data-bookmark-root-list>
            <?php foreach ($bookmarkRootItems as $rootIndex => $rootItem): ?>
                <?php
                $rootType = (string) ($rootItem['type'] ?? '');
                $rootFirst = $rootIndex === 0;
                $rootLast = $rootIndex === ($rootCount - 1);
                ?>
                <?php if ($rootType === 'bookmark'): ?>
                    <?php
                    $bookmark = is_array($rootItem['bookmark'] ?? null) ? $rootItem['bookmark'] : [];
                    $bookmarkId = (int) ($bookmark['id'] ?? 0);
                    $bookmarkName = trim((string) ($bookmark['name'] ?? ''));
                    $bookmarkUrl = trim((string) ($bookmark['url'] ?? ''));
                    $canonicalUrl = BookmarkUrlNormalizer::canonicalizeRelative($bookmarkUrl);
                    $bookmarkActive = $canonicalUrl !== '' && $currentPath === $canonicalUrl;
                    ?>
                    <li class="app-sidebar-bookmark-root-item app-sidebar-bookmark-item"
                        data-bookmark-root-kind="bookmark"
                        data-bookmark-root-id="<?php e($bookmarkId); ?>"
                        data-bookmark-id="<?php e($bookmarkId); ?>"
                        data-bookmark-name="<?php e($bookmarkName); ?>"
                        data-bookmark-group-id="">
                        <a href="<?php e(lurl($bookmarkUrl)); ?>"
                            class="<?php e($bookmarkActive ? 'active' : ''); ?>"
                            <?php echo $bookmarkActive ? 'aria-current="page"' : ''; // raw-html-ok: hardcoded ARIA attribute ?>>
                            <?php e($bookmarkName); ?>
                        </a>
                        <details class="app-sidebar-bookmark-action-menu" data-bookmark-action-menu>
                            <summary class="icon-button" title="<?php e(t('Actions')); ?>" aria-label="<?php e(t('Actions')); ?>">
                                <i class="bi bi-three-dots"></i>
                            </summary>
                            <ul class="app-sidebar-bookmark-action-menu-list" role="menu" aria-label="<?php e(t('Bookmark actions')); ?>">
                                <li>
                                    <button type="button" role="menuitem" data-bookmark-item-action="edit">
                                        <?php e(t('Edit')); ?>
                                    </button>
                                </li>
                                <li>
                                    <button type="button" role="menuitem" data-bookmark-item-action="move-up" data-bookmark-item-action-scope="root" <?php if ($rootFirst): ?>disabled<?php endif; ?>>
                                        <?php e(t('Move bookmark up')); ?>
                                    </button>
                                </li>
                                <li>
                                    <button type="button" role="menuitem" data-bookmark-item-action="move-down" data-bookmark-item-action-scope="root" <?php if ($rootLast): ?>disabled<?php endif; ?>>
                                        <?php e(t('Move bookmark down')); ?>
                                    </button>
                                </li>
                                <li>
                                    <button type="button" role="menuitem" class="app-sidebar-bookmark-action-danger" data-bookmark-item-action="delete">
                                        <?php e(t('Delete bookmark')); ?>
                                    </button>
                                </li>
                            </ul>
                        </details>
                    </li>
                <?php else: ?>
                    <?php
                    $group = is_array($rootItem['group'] ?? null) ? $rootItem['group'] : [];
                    $groupId = (int) ($group['id'] ?? 0);
                    $groupName = trim((string) ($group['name'] ?? ''));
                    $groupIcon = trim((string) ($group['icon'] ?? 'bi-folder'));
                    $groupBookmarks = is_array($group['bookmarks'] ?? null) ? $group['bookmarks'] : [];
                    ?>
                    <li class="app-sidebar-group app-sidebar-bookmark-group app-sidebar-bookmark-root-item"
                        data-bookmark-root-kind="group"
                        data-bookmark-root-id="<?php e($groupId); ?>"
                        data-bookmark-group-id="<?php e($groupId); ?>"
                        data-bookmark-group-name="<?php e($groupName); ?>"
                        data-bookmark-group-icon="<?php e($groupIcon); ?>">
                        <div class="app-sidebar-bookmark-group-shell">
                            <div class="app-sidebar-bookmark-group-header">
                                <small class="app-sidebar-bookmark-group-label">
                                    <i class="bi <?php e($groupIcon); ?>" aria-hidden="true"></i>
                                    <span><?php e($groupName); ?></span>
                                </small>
                                <details class="app-sidebar-bookmark-action-menu app-sidebar-bookmark-group-menu" data-bookmark-action-menu>
                                    <summary class="icon-button" title="<?php e(t('Actions')); ?>" aria-label="<?php e(t('Actions')); ?>">
                                        <i class="bi bi-three-dots"></i>
                                    </summary>
                                    <ul class="app-sidebar-bookmark-action-menu-list" role="menu" aria-label="<?php e(t('Group actions')); ?>">
                                        <li>
                                            <button type="button" role="menuitem" data-bookmark-group-action="edit">
                                                <?php e(t('Edit')); ?>
                                            </button>
                                        </li>
                                        <li>
                                            <button type="button" role="menuitem" data-bookmark-group-action="move-up" <?php if ($rootFirst): ?>disabled<?php endif; ?>>
                                                <?php e(t('Move group up')); ?>
                                            </button>
                                        </li>
                                        <li>
                                            <button type="button" role="menuitem" data-bookmark-group-action="move-down" <?php if ($rootLast): ?>disabled<?php endif; ?>>
                                                <?php e(t('Move group down')); ?>
                                            </button>
                                        </li>
                                        <li>
                                            <button type="button" role="menuitem" class="app-sidebar-bookmark-action-danger" data-bookmark-group-action="delete">
                                                <?php e(t('Delete group')); ?>
                                            </button>
                                        </li>
                                    </ul>
                                </details>
                            </div>
                            <?php if ($groupBookmarks): ?>
                                <ul data-bookmark-group-bookmark-list>
                                    <?php $groupBookmarkCount = count($groupBookmarks); ?>
                                    <?php foreach ($groupBookmarks as $bookmarkIndex => $bookmark): ?>
                                        <?php
                                        $bookmarkId = (int) ($bookmark['id'] ?? 0);
                                        $bookmarkName = trim((string) ($bookmark['name'] ?? ''));
                                        $bookmarkUrl = trim((string) ($bookmark['url'] ?? ''));
                                        $canonicalUrl = BookmarkUrlNormalizer::canonicalizeRelative($bookmarkUrl);
                                        $bookmarkActive = $canonicalUrl !== '' && $currentPath === $canonicalUrl;
                                        $bookmarkFirst = $bookmarkIndex === 0;
                                        $bookmarkLast = $bookmarkIndex === ($groupBookmarkCount - 1);
                                        ?>
                                        <li class="app-sidebar-bookmark-item"
                                            data-bookmark-id="<?php e($bookmarkId); ?>"
                                            data-bookmark-name="<?php e($bookmarkName); ?>"
                                            data-bookmark-group-id="<?php e($groupId); ?>">
                                            <a href="<?php e(lurl($bookmarkUrl)); ?>"
                                                class="<?php e($bookmarkActive ? 'active' : ''); ?>"
                                                <?php echo $bookmarkActive ? 'aria-current="page"' : ''; // raw-html-ok: hardcoded ARIA attribute ?>>
                                                <?php e($bookmarkName); ?>
                                            </a>
                                            <details class="app-sidebar-bookmark-action-menu" data-bookmark-action-menu>
                                                <summary class="icon-button" title="<?php e(t('Actions')); ?>" aria-label="<?php e(t('Actions')); ?>">
                                                    <i class="bi bi-three-dots"></i>
                                                </summary>
                                                <ul class="app-sidebar-bookmark-action-menu-list" role="menu" aria-label="<?php e(t('Bookmark actions')); ?>">
                                                    <li>
                                                        <button type="button" role="menuitem" data-bookmark-item-action="edit">
                                                            <?php e(t('Edit')); ?>
                                                        </button>
                                                    </li>
                                                    <li>
                                                        <button type="button" role="menuitem" data-bookmark-item-action="move-up" <?php if ($bookmarkFirst): ?>disabled<?php endif; ?>>
                                                            <?php e(t('Move bookmark up')); ?>
                                                        </button>
                                                    </li>
                                                    <li>
                                                        <button type="button" role="menuitem" data-bookmark-item-action="move-down" <?php if ($bookmarkLast): ?>disabled<?php endif; ?>>
                                                            <?php e(t('Move bookmark down')); ?>
                                                        </button>
                                                    </li>
                                                    <li>
                                                        <button type="button" role="menuitem" class="app-sidebar-bookmark-action-danger" data-bookmark-item-action="delete">
                                                            <?php e(t('Delete bookmark')); ?>
                                                        </button>
                                                    </li>
                                                </ul>
                                            </details>
                                        </li>
                                    <?php endforeach; ?>
                                </ul>
                            <?php endif; ?>
                        </div>
                    </li>
                <?php endif; ?>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
</div>
