<?php

declare(strict_types=1);

namespace CliqThemes\StoreLocator\Integrations;

defined('ABSPATH') || exit;

/**
 * Registers every third-party integration whose target plugin is active. New
 * integrations (SEO plugins, analytics, consent, …) are added to the list in
 * {@see self::all()} — each self-detects and self-wires.
 */
final class IntegrationManager
{
    private StoreDetailSeo $seo;

    /** @var Integration[]|null */
    private ?array $integrations = null;

    public function __construct(StoreDetailSeo $seo)
    {
        $this->seo = $seo;
    }

    /** Boot every active integration. */
    public function boot(): void
    {
        foreach ($this->all() as $integration) {
            if ($integration->isActive()) {
                $integration->register();
            }
        }
    }

    /** Whether any SEO plugin is active (so we should not emit our own meta tags). */
    public function anySeoActive(): bool
    {
        foreach ($this->seoIntegrations() as $integration) {
            if ($integration->isActive()) {
                return true;
            }
        }

        return false;
    }

    /** @return Integration[] */
    private function all(): array
    {
        if ($this->integrations === null) {
            $this->integrations = array_merge($this->seoIntegrations(), $this->pageBuilderIntegrations());
        }

        return $this->integrations;
    }

    /** @return Integration[] */
    private function seoIntegrations(): array
    {
        return [
            new Yoast($this->seo),
            new RankMath($this->seo),
            new SeoPress($this->seo),
        ];
    }

    /** Page-builder embed widgets (not SEO — excluded from anySeoActive). @return Integration[] */
    private function pageBuilderIntegrations(): array
    {
        return [
            new Elementor(),
            new WPBakery(),
        ];
    }
}
