<?php
namespace EasyRestaurantManager\Modules\Blocks;
use EasyRestaurantManager\Resources\MenuResource;
use EasyRestaurantManager\Resources\CategoryResource;

class BlockManager {
    public function __construct() {
        $this->registerBlocks();
    }

    public function registerBlocks() {
        self::registerScript();

        (new MenuBlocks())->init();
        (new CategoryBlocks())->init();
    }

    public static function registerScript() {
        wp_register_script(
            'easy_restaurant_manager-menu-block',
            EASY_RESTAURANT_MANAGER_PLUGIN_URL.'assets/js/menu-block.js',
            ['wp-blocks', 'wp-editor', 'wp-components', 'wp-element']
        );
    
        wp_enqueue_script('easy_restaurant_manager-menu-block');
        wp_enqueue_style('easy_restaurant_manager-menu-block', EASY_RESTAURANT_MANAGER_PLUGIN_URL.'assets/css/menu-block.css', [], EASY_RESTAURANT_MANAGER_VERSION);

        wp_localize_script( 'easy_restaurant_manager-menu-block', 'RestaurantMangerBlockData', [
            'rest_api' => rest_url('easy-restaurant-manage/v1'),
            'nonce' => wp_create_nonce('wp_rest'),
            'branch_id' => 1,
            'menus' => self::getMenus(),
            'categories' => self::getCategories(),
            'add_menu_url' => admin_url('admin.php?page=easy-restaurant-manager.php#/menus'),
            'add_category_url' => admin_url('admin.php?page=easy-restaurant-manager.php#/category'),
            'currency_symbol' => get_option('erm_currency_symbol', '$')
        ] );

    }

    public static function getMenus() {
        $menus = MenuResource::getMenu();
        return $menus;
    }
    public static function getCategories() {
        $categories = CategoryResource::getCategory();
        return $categories;
    }
}