<?php
namespace CSB;

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

if (!class_exists('bchCodeblock')) {
    class bchCodeblock
    {
        public function __construct()
        {
            add_action('init', [$this, 'OnInit']);
            add_action('enqueue_block_editor_assets', [$this, "csb_EnqueueEditorAssets"]);
            add_action('enqueue_block_assets', [$this, "csb_EnqueueFrontendAssets"]);
            add_filter('block_categories_all', [$this, 'csb_RegisterCategory'], 9);
        }

        /**
         * Register the "Code" block category that all of this plugin's blocks
         */
        public function csb_RegisterCategory($categories)
        {
            foreach ($categories as $category) {
                if (isset($category['slug']) && 'code' === $category['slug']) {
                    return $categories;
                }
            } 

            return array_merge(
                [[
                    'slug'  => 'code',
                    'title' => __('Code', 'code-snippets-block'),
                    'icon'  => null,
                ]],
                $categories
            );
        }
        public function OnInit()
        {
            wp_register_style('codeMirror', CSBCH_ASSETS_DIR . 'css/codeMirror.min.css', [], CSBCH_PLUGIN_VERSION);

            wp_register_script('highlight', CSBCH_ASSETS_DIR . 'js/highlight.min.js', [], '11.11.1', true);
            wp_register_script('highlightgo', CSBCH_ASSETS_DIR . 'js/highlightgo.js', ['highlight'], '11.11.1', true);
            wp_register_script('highlightlinenumber', CSBCH_ASSETS_DIR . 'js/highlightlinenumber.js', ['highlight'], CSBCH_PLUGIN_VERSION, true);
            wp_register_script('codeMirror', CSBCH_ASSETS_DIR . 'js/csb-codemirror.min.js', [], CSBCH_PLUGIN_VERSION, true);

            // Free base block — always registered.
            register_block_type(__DIR__ . '/build/blocks/code-highlight');
            register_block_type(__DIR__ . '/build/blocks/code-parent');

            // Pro-only blocks — registered only with an active premium license and
            // only when not turned off on the dashboard Blocks page. Add entries as 
            // 'build-dir' => 'bch/block-name'.
            if (CSBCH_HAS_PRO && csbIsPremium()) {

                $disabled = get_option('csbDisabledBlocks', []);
                if (!is_array($disabled)) {
                    $disabled = [];
                }

                $pro_blocks = [
                    'code-tabs'     => 'bch/code-tabs',
                    'terminal'      => 'bch/terminal',
                    'remote-embed'  => 'bch/remote-embed',
                    'code-steps'    => 'bch/code-steps',
                    'code-diff'     => 'bch/code-diff',
                    'playground'    => 'bch/playground',
                    'diagram'       => 'bch/diagram',
                    'snapshot'      => 'bch/snapshot',
                    'api-tryit'     => 'bch/api-tryit',
                    'annotations'   => 'bch/annotations',
                    'explain'       => 'bch/explain',
                    'python'        => 'bch/python',
                    'screencast'    => 'bch/screencast',
                    'json-viewer'   => 'bch/json-viewer',
                    'regex'         => 'bch/regex',
                    'spotlight'     => 'bch/spotlight',
                ];

                foreach ($pro_blocks as $dir => $block_name) {
                    if (in_array($block_name, $disabled, true)) {
                        continue;
                    }
                    register_block_type(__DIR__ . '/build/blocks/' . $dir);
                }
            }
        }
        public function csb_EnqueueEditorAssets()
        {
            wp_add_inline_script(
                'wp-blocks',
                'const csbIsPipeChecker = ' . wp_json_encode(csbIsPremium()) . ';',
                'after'
            );

        }
        public function csb_EnqueueFrontendAssets()
        {
            wp_add_inline_script(
                'bch-code-highlight-view-script',
                'const csbIsPipeChecker = ' . wp_json_encode(csbIsPremium()) . ';',
                'before'
            );
        }
    }

    new bchCodeblock();
}