<?php
/**
 * Generates a page URL based on the given route, arguments, and namespace.
 *
 * @param string $route The route to generate the URL for.
 * @param array $args Optional. Additional arguments to append to the URL.
 * @param bool $namespace Optional. Whether to include the namespace in the URL.
 * @return string The generated page URL.
 */
if (!function_exists('neurogenesis_theme_admin_page_file')) {
    /**
     * Parent admin file for the Neurogenesis screen.
     *
     * Appearance submenu when Neurogenesis Styler is inactive; top-level
     * admin.php when the plugin owns the menu.
     */
    function neurogenesis_theme_admin_page_file(): string
    {
        if (
            class_exists(\NeurogenesisTheme\Helpers\RecommendedPlugin::class)
            && 'active' === \NeurogenesisTheme\Helpers\RecommendedPlugin::get_status()
        ) {
            return 'admin.php';
        }

        return 'themes.php';
    }
}

if (!function_exists('neurogenesis_theme_generate_page_url')) {        
    function neurogenesis_theme_generate_page_url($route, $args = [], $namespace = false)
    {
        if ($namespace == false) {
            $namespace = NEUROGENESIS_THEME_PAGE_ID;
        }
        $urlParams = [
            'page'  => $namespace,
            'route' => $route
        ];

        if (!empty($args)) {
            $urlParams = array_merge($urlParams, $args);
        }

        return add_query_arg(
            $urlParams,
            admin_url(neurogenesis_theme_admin_page_file())
        );
    }
}

if (!function_exists('neurogenesis_theme_active_page')) {  
    /**
     * Checks if the current page matches the given route.
     *
     * @param string $route The route to match against.
     * @param bool $namespace Optional. Whether to include the namespace in the route. Default is false.
     * @return bool True if the current page matches the route, false otherwise.
     */
    function neurogenesis_theme_active_page($route, $namespace = false) {
        if ($namespace === false) {
            $namespace = NEUROGENESIS_THEME_PAGE_ID;
        }

        $page = neurogenesis_theme_array_get($_GET, 'page', '');
        $current_route = neurogenesis_theme_array_get($_GET, 'route', '');

        if ($page !== $namespace) {
            return false;
        }

        if ($route === 'dashboard') {
            return $current_route === '' || $current_route === 'dashboard';
        }

        return $current_route === $route;
    }
}

/**
 * Requires a file in the plugin.
 *
 * @param string $file_path The path to the file to be required.
 * @return void
 */
if(!function_exists('neurogenesis_theme_plugin_require')) {
    
    function neurogenesis_theme_plugin_require($file_path)
    {
        require_once(NEUROGENESIS_THEME_PATH . '/' . $file_path);
    }
}

/**
 * Includes a file from the plugin.
 *
 * @param string $file_path The path to the file to be included.
 * @return void
 */
if(!function_exists('neurogenesis_theme_require')) {
    
    function neurogenesis_theme_require($file_path)
    {
        require_once(NEUROGENESIS_THEME_PATH . '/' . $file_path);
    }
}

if(!function_exists('neurogenesis_theme_url')) {
    /**
     * Generates a URL for the plugin.
     *
     * @param string $path The path to append to the base URL.
     * @return string The generated URL.
     */
    function neurogenesis_theme_url($path = '')
    {
        return NEUROGENESIS_THEME_URL . '/' . $path;
    }
}

if(!function_exists('neurogenesis_theme_theme_url')) {
    /**
     * Generates the URL for the NeurogenesisTheme theme.
     *
     * @param string $path The path to append to the base URL.
     * @return string The generated URL.
     */
    function neurogenesis_theme_theme_url($path = '')
    {
        return get_template_directory_uri() . '/' . $path;
    }
}

if(!function_exists('neurogenesis_theme_plugin_url')) {
    /**
     * Generates the URL for the NeurogenesisTheme Architect plugin.
     *
     * @param string $path The path to append to the base URL.
     * @return string The generated URL.
     */
    function neurogenesis_theme_plugin_url($path = '')
    {
        return NEUROGENESIS_THEME_URL . '/' . $path;
    }
}

if(!function_exists('neurogenesis_theme_plugin_path')) {
    /**
     * Generates the Path for the NeurogenesisTheme Architect plugin.
     *
     * @param string $path The path to append to the base URL.
     * @return string The generated path.
     */
    function neurogenesis_theme_plugin_path($path = '')
    {        
        return NEUROGENESIS_THEME_PATH . '/' . $path;
    }
}