<?php
/**
 * Active plugin paths + prefix matching for editor detection (keeps {@see EditorSettingsService} smaller).
 *
 * @package FleekDash\Services
 */

namespace FleekDash\Services;

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

final class EditorPluginActivationHelper
{
    /**
     * Per-site active_plugins plus network-active plugin keys on multisite.
     *
     * @return list<string>
     */
    public static function listActivePluginFilePaths(): array
    {
        $site = \get_option('active_plugins', []);
        if (!\is_array($site)) {
            $site = [];
        }
        $merged = $site;
        if (\function_exists('is_multisite') && \is_multisite() && \function_exists('get_site_option')) {
            $network = \get_site_option('active_sitewide_plugins', []);
            if (\is_array($network)) {
                foreach (\array_keys($network) as $path) {
                    if (\is_string($path) && $path !== '') {
                        $merged[] = $path;
                    }
                }
            }
        }

        return \array_values(\array_unique($merged));
    }

    /**
     * True if any pattern matches the start of an active plugin file path (case-insensitive).
     *
     * @param list<string> $patterns
     * @param list<string> $pluginFiles
     */
    public static function matchesAnyConfiguredPrefix(array $patterns, array $pluginFiles): bool
    {
        foreach ($patterns as $pattern) {
            if (!\is_string($pattern) || $pattern === '') {
                continue;
            }
            foreach ($pluginFiles as $pluginFile) {
                if (!\is_string($pluginFile) || $pluginFile === '') {
                    continue;
                }
                if (\stripos($pluginFile, $pattern) === 0) {
                    return true;
                }
            }
        }

        return false;
    }
}
