<?php

if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Design_Switcher
{
    public const HANDLE = 'puffergo-design-switcher';

    public const VERSION = PUFFERGO_CORE_VERSION;

    public const AI_BUILDER_SCREEN = 'puffergo_page_puffergo-ai-site-builder';

    public function __construct()
    {
        add_action('init', array($this, 'maybe_register_hooks'));
    }

    public function maybe_register_hooks()
    {
        if (! is_user_logged_in() || ! current_user_can('edit_posts')) {
            return;
        } add_action('wp_enqueue_scripts', array($this, 'enqueue_assets'));
        add_action('admin_enqueue_scripts', array($this, 'enqueue_assets'));
        add_action('wp_footer', array($this, 'render_mount_node'));
        add_action('admin_footer', array($this, 'render_mount_node'));
    }

    private function should_skip()
    {
        if (! is_admin()) {
            return false;
        } $screen = get_current_screen();

        return $screen && (in_array($screen->base, array('post', 'post-new'), true) || self::AI_BUILDER_SCREEN === $screen->id);
    }

    private function is_current_view_puffergo_fullpage()
    {
        if (is_admin()) {
            return false;
        }

if (! is_singular()) {
            return false;
        } $post_id = (int) get_queried_object_id();

        if ($post_id < 1) {
            return false;
        } $template = get_page_template_slug($post_id);

        if ('' === $template) {
            $template = (string) get_post_meta($post_id, '_wp_page_template', true);
        }

return 'puffergo-fullpage.php' === $template;
    }

    public function enqueue_assets()
    {
        if ($this->should_skip()) {
            return;
        } $plugin_url = plugin_dir_url(PUFFERGO_PLUGIN_FILE);
        $plugin_dir = plugin_dir_path(PUFFERGO_PLUGIN_FILE);
        $asset_file = $plugin_dir . 'build/admin/design-switcher/index.asset.php';
        $js_file = $plugin_dir . 'build/admin/design-switcher/index.js';

        if (! file_exists($js_file)) {
            return;
        } $deps = array('wp-element', 'wp-i18n', 'wp-api-fetch');
        $version = self::VERSION;

        if (file_exists($asset_file)) {
            $asset = include $asset_file;
            $deps = $asset['dependencies'] ?? $deps;
            $version = $asset['version'] ?? $version;
        } wp_enqueue_script(self::HANDLE, esc_url($plugin_url . 'build/admin/design-switcher/index.js'), $deps, $version, true);
        wp_localize_script(self::HANDLE, 'puffergoDesignSwitcher', array('restUrl' => rest_url('puffergo/v1/'), 'nonce' => wp_create_nonce('wp_rest'), 'isPuffergoFullpage' => $this->is_current_view_puffergo_fullpage(), ));
        $css_file = $plugin_dir . 'build/admin/design-switcher/index.css';

        if (file_exists($css_file)) {
            wp_enqueue_style(self::HANDLE, esc_url($plugin_url . 'build/admin/design-switcher/index.css'), array(), $version);
        }
    }

    public function render_mount_node()
    {
        if ($this->should_skip()) {
            return;
        } echo '<div id="puffergo-design-switcher-root"></div>';
    }
}
