<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit;

function Mirai_renderAjaxPaginationLoader($scopeType) {
    if (!Mirai_isSkeletonEnabled()) {
        return '';
    }

    if ($scopeType === 'comments') {
        $commentConfig = Mirai_getCommentConfig();
        $commentPageSize = intval($commentConfig['pageSize']);
        $commentPageSize = max(1, min(50, $commentPageSize));
        return '<div class="mirai-ajax-skeleton mirai-ajax-skeleton-comments">' . Mirai_renderSkeletonCommentThread($commentPageSize) . '</div>';
    }

    $options = Mirai_opt();
    $gridValue = intval($options->gridLayout ?: 3);
    $gridColumns = max(1, min(4, $gridValue));
    $articlePageSize = intval($options->ajaxArticleSkeletonSize ?: 5);
    $articlePageSize = max(1, min(50, $articlePageSize));

    $singleColumn = ($gridColumns === 1);
    $html = '<div class="mirai-ajax-skeleton">';
    if ($singleColumn) {
        $html .= Mirai_renderSkeletonSingleColumnArticles($articlePageSize);
    } else {
        $html .= Mirai_renderSkeletonArticleCards($articlePageSize, $gridColumns);
    }
    $html .= '</div>';

    return $html;
}

function Mirai_customPagination($current, $total, $linkGenerator, $options = []) {
    if ($total <= 1) return '';

    $linkCallback = function($page) use ($linkGenerator) {
        return is_callable($linkGenerator) ? $linkGenerator($page) : $linkGenerator . ($page > 1 ? '?page=' . $page : '');
    };

    $opts = array_merge(['endSize' => 1, 'midSize' => 1, 'mode' => 'default', 'scopeType' => '', 'triggerMode' => 'button'], $options);

    if ($opts['mode'] === 'ajax') {
        $scopeType = $opts['scopeType'] === 'comments' ? 'comments' : 'archive';
        $triggerMode = $opts['triggerMode'] === 'auto' ? 'auto' : 'button';

        $commentsPageDisplay = $opts['commentsPageDisplay'] ?? 'first';
        $direction = ($scopeType === 'comments' && $commentsPageDisplay === 'last') ? 'backward' : 'forward';

        $hasMore = ($direction === 'forward') ? ($current < $total) : ($current > 1);
        $nextPage = ($direction === 'forward') ? $current + 1 : $current - 1;

        $html = Mirai_buildAjaxPaginationHtml([
            'scopeType' => $scopeType,
            'current' => $current,
            'total' => $total,
            'nextUrl' => $hasMore ? $linkCallback($nextPage) : '',
            'triggerMode' => $triggerMode,
            'direction' => $direction,
            'hasMore' => $hasMore,
        ]);

        return $html;
    }

    $pages = [];
    $dots = false;
    for ($i = 1; $i <= $total; $i++) {
        $isCurrent = $i == $current;
        $show = $isCurrent || $i <= $opts['endSize'] || $i > $total - $opts['endSize'] || ($i >= $current - $opts['midSize'] && $i <= $current + $opts['midSize']);
        
        if ($show) {
            $pages[] = $isCurrent 
                ? '<span class="p-num cur" aria-current="page">' . $i . '</span>' 
                : '<a href="' . $linkCallback($i) . '" class="p-num" aria-label="第' . $i . '页">' . $i . '</a>';
            $dots = true;
        } elseif ($dots) {
            $pages[] = '<span class="p-num p-dots" aria-hidden="true">...</span>';
            $dots = false;
        }
    }

    $prev = $current > 1 
        ? '<a href="' . $linkCallback($current - 1) . '" class="p-num p-arr" rel="prev" aria-label="上一页"><i class="ri-arrow-left-s-line"></i></a>'
        : '<button type="button" class="p-num p-arr dis" disabled aria-label="上一页"><i class="ri-arrow-left-s-line"></i></button>';
    
    $next = $current < $total
        ? '<a href="' . $linkCallback($current + 1) . '" class="p-num p-arr" rel="next" aria-label="下一页"><i class="ri-arrow-right-s-line"></i></a>'
        : '<button type="button" class="p-num p-arr dis" disabled aria-label="下一页"><i class="ri-arrow-right-s-line"></i></button>';

    return '<nav class="p-wrap" aria-label="分页">' . $prev . implode('', $pages) . $next . '</nav>';
}
