<?php

use iwbvel\classes\helpers\Sanitizer;

if (!defined('ABSPATH')) exit; // Exit if accessed directly 

if (!isset($current_page) || !isset($max_num_pages) || $max_num_pages < 2) {
    return;
}

$iwbvel_current_page = intval($current_page);
$iwbvel_max_num_pages = intval($max_num_pages);

$iwbvel_prev = max(1, $iwbvel_current_page - 1);
$iwbvel_next = min($iwbvel_max_num_pages, $iwbvel_current_page + 1);
$iwbvel_max_display = 3;
$iwbvel_pagination_html = "<div data-last-page='" . esc_attr($iwbvel_max_num_pages) . "'>";
if (isset($iwbvel_max_num_pages)) {
    $iwbvel_pagination_html .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_prev) . "' class='iwbvel-variations-pagination-item'><</a>";
    if ($iwbvel_current_page < $iwbvel_max_display) {
        for ($iwbvel_i = 1; $iwbvel_i <= min($iwbvel_max_display, $iwbvel_max_num_pages); $iwbvel_i++) {
            $iwbvel_current = ($iwbvel_i == $iwbvel_current_page) ? 'current' : '';
            $iwbvel_pagination_html .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_i) . "' class='iwbvel-variations-pagination-item " . esc_attr($iwbvel_current) . "'>" . esc_html($iwbvel_i) . "</a>";
        }
        if ($iwbvel_max_num_pages > ($iwbvel_max_display + 1)) {
            $iwbvel_pagination_html .= "<span>...</span>";
        }
        if ($iwbvel_max_num_pages > $iwbvel_max_display) {
            $iwbvel_pagination_html .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_max_num_pages) . "' class='iwbvel-variations-pagination-item'>" . esc_html($iwbvel_max_num_pages) . "</a>";
        }
    } elseif ($iwbvel_current_page == $iwbvel_max_display) {
        $iwbvel_max_num = ($iwbvel_max_display < $iwbvel_max_num_pages) ? $iwbvel_max_display + 1 : $iwbvel_max_display;
        for ($iwbvel_i = 1; $iwbvel_i <= $iwbvel_max_num; $iwbvel_i++) {
            $iwbvel_current = ($iwbvel_i == $iwbvel_current_page) ? 'current' : '';
            $iwbvel_pagination_html .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_i) . "' class='iwbvel-variations-pagination-item " . esc_attr($iwbvel_current) . "'>" . esc_html($iwbvel_i) . "</a>";
        }
        if ($iwbvel_max_num_pages > ($iwbvel_current_page + 1)) {
            $iwbvel_pagination_html .= "<span>...</span>";
            $iwbvel_pagination_html .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_max_num_pages) . "' class='iwbvel-variations-pagination-item'>" . esc_html($iwbvel_max_num_pages) . "</a>";
        }
    } else {
        $iwbvel_pagination_html .= "<a href='javascript:;' data-index='1' class='iwbvel-variations-pagination-item'>1</a>";
        if ($iwbvel_max_num_pages > $iwbvel_max_display) {
            $iwbvel_pagination_html .= "<span>...</span>";
        }
        for ($iwbvel_i = $iwbvel_current_page - 1; $iwbvel_i <= min($iwbvel_current_page + 1, $iwbvel_max_num_pages); $iwbvel_i++) {
            $iwbvel_current = ($iwbvel_i == $iwbvel_current_page) ? 'current' : '';
            $iwbvel_pagination_html .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_i) . "' class='iwbvel-variations-pagination-item " . esc_attr($iwbvel_current) . "'>" . esc_html($iwbvel_i) . "</a>";
        }
        if ($iwbvel_current_page + 1 < $iwbvel_max_num_pages) {
            $iwbvel_pagination_html .= "<span>...</span>";
            $iwbvel_pagination_html .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_max_num_pages) . "' class='iwbvel-variations-pagination-item'>" . esc_html($iwbvel_max_num_pages) . "</a>";
        }
    }
    $iwbvel_pagination_html .= "<a href='javascript:;' data-index='" . esc_attr($iwbvel_next) . "' class='iwbvel-variations-pagination-item'>></a>";
}
$iwbvel_pagination_html .= "</div>";

echo wp_kses($iwbvel_pagination_html, Sanitizer::allowed_html());
