<?php

use BookStack\Facades\Theme;
use BookStack\Theming\ThemeEvents;
use BookStack\View\BaseViewBlock;
use BookStack\View\ViewBlockManager;

/**
 * Define our custom block to display developer-focused information.
 */
class DeveloperInfoBlock extends BaseViewBlock
{
    // We provide a unique ID for this block to distinguish it from other blocks.
    // This is used as the key when the block is configured as part of a layout.
    public static function getId(): string
    {
        return 'com.bookstackapp.hacks.developer-info-block';
    }

    // We return the label which will show for this block in the configuration UI.
    public static function getLabel(): string
    {
        return 'Developer Info';
    }

    // We return the view name which will be used to render the block.
    public function getView(array $viewData): string
    {
        return 'custom-block-developer-info.block';
    }

    // We return all the developer-focused information based upon the item being viewed.
    public function getViewData(array $viewData): array
    {
        $shownEntity = $viewData['current'] ?? $viewData['shelf'] ?? null;
        return [
            'entity' => $shownEntity,
            'location' => $viewData['location'] ?? null,
        ];
    }
}

// We listen for the VIEW_BLOCKS_REGISTER event using the theme system which, when called,
// provides us a ViewBlockManager instance that we can use to register our custom block.
// We register the block on each item's display/list views, unused by default.
Theme::listen(ThemeEvents::VIEW_BLOCKS_REGISTER, function (ViewBlockManager $manager) {
    $targetLocations = ['shelves-show', 'shelves-index', 'books-show', 'books-index', 'chapters-show', 'pages-show'];
    foreach ($targetLocations as $location) {
        $manager->register($location, 'unused', DeveloperInfoBlock::class);
    }
});