<?php
if (! defined('ABSPATH')) {
    exit;
} class PufferGo_Template_Manager
{
    public function __construct()
    {
        add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
    }

    public static function render_page()
    { ?>
		<div class="wrap">
			<div id="puffergo-template-manager-root"></div>
		</div>
		<?php
    }

    public function enqueue_scripts($hook)
    {
        if ($hook !== 'puffergo_page_puffergo-templates') {
            return;
        } wp_enqueue_media();
        $plugin_url = plugin_dir_url(dirname(__FILE__));
        $asset_file = plugin_dir_path(dirname(__FILE__)) . 'build/admin/template-manager/index.asset.php';

        if (file_exists($asset_file)) {
            $asset = include $asset_file;
            $deps = $asset['dependencies'];
            $version = $asset['version'];
        } else {
            $deps = array('wp-element', 'wp-components', 'wp-api-fetch', 'wp-data', 'wp-i18n');
            $version = time();
        } wp_enqueue_script('puffergo-template-manager', $plugin_url . 'build/admin/template-manager/index.js', $deps, $version, true);
        $style_css = plugin_dir_path(dirname(__FILE__)) . 'build/admin/template-manager/style-index.css';

        if (file_exists($style_css)) {
            wp_enqueue_style('puffergo-template-manager-styles', $plugin_url . 'build/admin/template-manager/style-index.css', array('puffergo-admin-tailwind', 'wp-components'), $version);
        } $layouts_raw = PufferGo_Template_Loader::get_layouts();
        $product_enabled = ! class_exists('PufferGo_Base_CPT') || PufferGo_Base_CPT::is_enabled('product');
        $woo_active = class_exists('PufferGo_Business_Mode') && PufferGo_Business_Mode::is_woo_active();
        $cpt_layouts = array('product-single', 'product-archive');
        $woo_layouts = array('woo-product-single', 'woo-product-archive');
        $layouts = array();

        foreach ($layouts_raw as $layout) {
            $lid = $layout['id'];

            if (! $product_enabled && in_array($lid, $cpt_layouts, true)) {
                continue;
            }

if (! $woo_active && in_array($lid, $woo_layouts, true)) {
                continue;
            } $layouts[] = array('id' => $lid, 'label' => $layout['label'], 'hasContentTemplates' => $layout['has_content_templates'], 'content_templates' => $layout['content_templates'], 'contentTemplate' => PufferGo_Template_Loader::get_active_content_template($lid), );
        } $capabilities = class_exists('PufferGo_Template_Config') ? PufferGo_Template_Config::load_catalog() : array('schemaVersion' => 1, 'capabilities' => array());
        wp_localize_script('puffergo-template-manager', 'puffergoTemplateManager', array('layouts' => $layouts, 'nonce' => wp_create_nonce('wp_rest'), 'capabilities' => $capabilities, ));
        wp_enqueue_style('wp-components');
    }
}
