<?php
declare(strict_types=1);

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

if (!function_exists('ekesto_ca_admin_notice_screen_is_allowed')) {
    /**
     * Return whether Cache Autopilot admin notices may render on the current screen.
     *
     * @return bool
     */
    function ekesto_ca_admin_notice_screen_is_allowed(): bool
    {
        $screen = function_exists('get_current_screen') ? get_current_screen() : null;
        if (!($screen instanceof WP_Screen)) {
            return false;
        }

        $screenId = (string) $screen->id;
        $screenBase = (string) $screen->base;

        if ($screenId === 'plugins' || $screenBase === 'plugins' || ekesto_ca_admin_notice_screen_is_plugins_php()) {
            return true;
        }

        return in_array($screenId, ekesto_ca_admin_notice_settings_screen_ids(), true);
    }
}

if (!function_exists('ekesto_ca_admin_notice_screen_is_plugins_php')) {
    /**
     * Return whether the current admin request is the WordPress Plugins screen.
     *
     * @return bool
     */
    function ekesto_ca_admin_notice_screen_is_plugins_php(): bool
    {
        global $pagenow;

        return is_string($pagenow) && $pagenow === 'plugins.php';
    }
}

if (!function_exists('ekesto_ca_admin_notice_settings_screen_ids')) {
    /**
     * Resolve the registered ECI and ECW settings screen ids.
     *
     * @return string[]
     */
    function ekesto_ca_admin_notice_settings_screen_ids(): array
    {
        $allowed = [];

        if (function_exists('get_plugin_page_hookname')) {
            $registeredPages = isset($GLOBALS['_registered_pages']) && is_array($GLOBALS['_registered_pages'])
                ? $GLOBALS['_registered_pages']
                : [];

            foreach (ekesto_ca_admin_notice_settings_page_candidates() as $candidate) {
                if ($candidate !== '' && isset($registeredPages[$candidate])) {
                    $allowed[] = $candidate;
                }
            }
        }

        foreach (ekesto_ca_admin_notice_settings_screen_fallbacks() as $fallback) {
            $allowed[] = $fallback;
        }

        return array_values(array_filter(array_unique($allowed)));
    }
}

if (!function_exists('ekesto_ca_admin_notice_settings_page_candidates')) {
    /**
     * Return registered-page hook candidates for both engine settings pages.
     *
     * @return string[]
     */
    function ekesto_ca_admin_notice_settings_page_candidates(): array
    {
        if (!function_exists('get_plugin_page_hookname')) {
            return [];
        }

        [$invalidatorSlug, $warmupSlug] = ekesto_ca_admin_notice_settings_page_slugs();

        return [
            (string) get_plugin_page_hookname($invalidatorSlug, 'options-general.php'),
            (string) get_plugin_page_hookname($invalidatorSlug, ''),
            (string) get_plugin_page_hookname($warmupSlug, 'options-general.php'),
            (string) get_plugin_page_hookname($warmupSlug, ''),
        ];
    }
}

if (!function_exists('ekesto_ca_admin_notice_settings_page_slugs')) {
    /**
     * Return the ECI and ECW settings page slugs.
     *
     * @return array{0:string,1:string}
     */
    function ekesto_ca_admin_notice_settings_page_slugs(): array
    {
        $invalidatorSlug = class_exists(\Ekesto\CacheInvalidator\Settings::class)
            ? \Ekesto\CacheInvalidator\Settings::SETTINGS_PAGE_SLUG
            : 'cache-autopilot-invalidator';
        $warmupSlug = class_exists(\Ekesto\CacheWarmup\Settings::class)
            ? \Ekesto\CacheWarmup\Settings::PAGE_SLUG
            : 'cache-autopilot-warmup';

        return [$invalidatorSlug, $warmupSlug];
    }
}

if (!function_exists('ekesto_ca_admin_notice_settings_screen_fallbacks')) {
    /**
     * Return codebase-defined fallback screen ids used before WordPress exposes the registered page map.
     *
     * @return string[]
     */
    function ekesto_ca_admin_notice_settings_screen_fallbacks(): array
    {
        [$invalidatorSlug, $warmupSlug] = ekesto_ca_admin_notice_settings_page_slugs();
        $invalidatorSettingsHook = class_exists(\Ekesto\CacheInvalidator\Settings::class)
            ? \Ekesto\CacheInvalidator\Settings::SETTINGS_PAGE_HOOK
            : 'settings_page_' . $invalidatorSlug;

        return [
            $invalidatorSettingsHook,
            'toplevel_page_' . $invalidatorSlug,
            'settings_page_' . $warmupSlug,
            'toplevel_page_' . $warmupSlug,
        ];
    }
}
