<?php

declare(strict_types=1);

namespace CliqThemes\StoreLocator\Markers;

defined('ABSPATH') || exit;

/**
 * Resolves the marker source the site is using.
 *
 * Mirrors {@see \CliqThemes\StoreLocator\Layouts\LocatorLayouts}: the packaged
 * set answers unless something registers a richer source through the filter.
 */
final class Markers
{
    private static ?MarkerSource $source = null;

    public static function source(): MarkerSource
    {
        if (self::$source instanceof MarkerSource) {
            return self::$source;
        }

        /**
         * Filter the source of map markers.
         *
         * Anything not implementing {@see MarkerSource} is ignored, so a bad
         * filter falls back to the packaged marker rather than leaving every
         * store on the map without a pin.
         *
         * @param MarkerSource $source
         */
        $source = apply_filters('cliq_sl_marker_source', new BundledMarkers());

        return self::$source = $source instanceof MarkerSource ? $source : new BundledMarkers();
    }

    /** Drop the resolved source. For tests, and for a request that re-registers. */
    public static function reset(): void
    {
        self::$source = null;
    }
}
