<?php
/**
 * Block Registry Class
 *
 * @package NemtlyBooking\Blocks
 */

namespace NemtlyBooking\Blocks;

use NemtlyBooking\Core\Container;

/**
 * Block Registry class
 */
class Block_Registry {

    /**
     * Container instance
     *
     * @var Container
     */
    private $container;

    /**
     * Registered blocks
     *
     * @var array
     */
    private $blocks = [];

    /**
     * Constructor
     *
     * @param Container $container Dependency injection container.
     */
    public function __construct( Container $container ) {
        $this->container = $container;
    }

    /**
     * Register a block class
     *
     * @param string $block_class Class name of the block to register.
     */
    public function register_block( $block_class ) {
        if ( ! class_exists( $block_class ) ) {
            return;
        }

        $block = new $block_class();
        if ( $block instanceof Abstract_Block ) {
            $this->blocks[ $block->get_name() ] = $block;
            $block->register();
        }
    }

    /**
     * Initialize all registered blocks
     */
    public function init() {
        // Register Block Categories
        add_filter( 'block_categories_all', [ $this, 'register_block_category' ], 10, 2 );

        // Register Core Blocks
        $this->register_block( Event_Date::class );
        $this->register_block( Event_Location::class );
        $this->register_block( Event_Price::class );
        $this->register_block( Booking_Trigger::class );
        $this->register_block( Query_Loop_Variation::class );
        $this->register_block( Event_Calendar::class );
        $this->register_block( Booking_Calendar::class );
    }

    /**
     * Register Custom Block Category
     *
     * @param array                   $categories Array of categories.
     * @param \WP_Block_Editor_Context $context    Editor context.
     * @return array
     */
    public function register_block_category( $categories, $context ) {
        return array_merge(
            $categories,
            [
                [
                    'slug'  => 'nemtly-blocks',
                    'title' => __( 'Nemtly Booking', 'nemtly-booking' ),
                    'icon'  => 'calendar-alt',
                ],
            ]
        );
    }
}
