<?php
declare(strict_types=1);

namespace Help4BuilderSuite\Modules\Calendar;

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

final class EventSitemapProvider extends \WP_Sitemaps_Provider
{
    public function __construct()
    {
        $this->name = 'events';
        $this->object_type = 'event';
    }

    /** @return array<int, array<string, string>> */
    public function get_url_list($page_num, $object_subtype = ''): array
    {
        if ((int) $page_num !== 1) return [];
        $urls = [];
        foreach (Workspace::effectiveEvents() as $event) {
            if (! Workspace::searchReadiness($event)['eligible']) continue;
            $row = ['loc' => Workspace::eventUrl($event)];
            if ((string) ($event['updated_at'] ?? '') !== '') $row['lastmod'] = (string) $event['updated_at'];
            $urls[] = $row;
        }
        return $urls;
    }

    public function get_max_num_pages($object_subtype = ''): int
    {
        foreach (Workspace::effectiveEvents() as $event) if (Workspace::searchReadiness($event)['eligible']) return 1;
        return 0;
    }
}
