<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class TLEvents_Block
{
    private $name;
    private $events;

    function __construct($name, $events)
    {
        $this->name = $name;
        $this->events = $events;
        $this->registerBlock();
    }

    function ourRenderCallback($attributes, $content)
    {
        ob_start();
        $events = $this->events;
        require TLEVENTS_PLUGIN_DIR . "blocks/{$this->name}/{$this->name}.php";
        return ob_get_clean();
    }

    function registerBlock()
    {
        wp_register_script(
            $this->name,
            TLEVENTS_PLUGIN_URL . "build/{$this->name}.js",
            ['wp-blocks', 'wp-editor', 'wp-i18n', 'wp-element'],
            filemtime( TLEVENTS_PLUGIN_DIR . "build/{$this->name}.js" ),
            true
        );

        wp_set_script_translations($this->name, 'ticketleo-events', TLEVENTS_PLUGIN_DIR . 'languages');

        // Register the Ticketleo reservation embed script
        wp_register_script(
            'ticketleo-reservation-embed',
            'https://reservation.ticketleo.com/embed.js',
            array(),
            null,
            true // Load in footer
        );

        register_block_type(
            TLEVENTS_PLUGIN_DIR . "blocks/{$this->name}",
            [
                'render_callback' => [$this, 'ourRenderCallback'],
            ]
        );
    }    
}