<?php

namespace Dev4Press\Plugin\BreadcrumbsPress\Blocks;

use Dev4Press\v55\Core\Blocks\Register as BaseRegister;
use WP_Block;
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
class Register extends BaseRegister {
    public static function instance() : Register {
        static $instance = false;
        if ( $instance === false ) {
            $instance = new Register();
        }
        return $instance;
    }

    public function blocks() {
        $asset_file = (include BREADCRUMBSPRESS_PATH . 'build/index.asset.php');
        wp_register_style(
            'breadcrumbspress-editor',
            BREADCRUMBSPRESS_URL . 'css/blocks.css',
            array('breadcrumbspress'),
            $asset_file['version']
        );
        wp_register_script(
            'breadcrumbspress-editor',
            BREADCRUMBSPRESS_URL . 'build/index.js',
            $asset_file['dependencies'],
            $asset_file['version']
        );
        register_block_type( BREADCRUMBSPRESS_PATH . 'build/blocks/single', array(
            'render_callback' => array($this, 'callback_single'),
        ) );
        register_block_type( BREADCRUMBSPRESS_PATH . 'build/blocks/multi', array(
            'render_callback' => array($this, 'callback_multi'),
        ) );
    }

    protected function _default_block_classes( $attributes, $block ) : array {
        $classes = array('breadcrumbspress-block-wrapper', 'breadcrumbspress-block-' . $block);
        if ( !empty( $attributes['class'] ) ) {
            $classes[] = esc_attr( $attributes['class'] );
        }
        return $classes;
    }

    public function callback_single( array $attributes, string $content, WP_Block $block ) : string {
        $classes = $this->_default_block_classes( $attributes, 'single' );
        if ( $this->is_editor() ) {
            $post_id = $block->context['postId'] ?? 0;
        } else {
            $post_id = 0;
        }
        $rendered = Render::instance()->single( $post_id, $attributes );
        if ( empty( $rendered ) && $this->is_editor() ) {
            $rendered = __( 'Breadcrumbs can\'t be rendered in this context.', 'breadcrumbspress' );
        }
        return '<div class="' . join( ' ', $classes ) . '">' . $rendered . '</div>';
    }

    public function callback_multi( array $attributes, string $content, WP_Block $block ) : string {
        return '<div class="components-placeholder is-large">
					    <div class="components-placeholder__label">
					        <svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" role="img" aria-hidden="true" focusable="false">
					            <path d="M224 184c13.25 0 24-10.74 24-24c0-13.25-10.75-24-24-24S200 146.7 200 160C200 173.3 210.7 184 224 184zM384 32H64C28.65 32 0 60.65 0 96v320c0 35.35 28.65 64 64 64h320c35.35 0 64-28.65 64-64V96C448 60.65 419.3 32 384 32zM416 416c0 17.64-14.36 32-32 32H64c-17.64 0-32-14.36-32-32V96c0-17.64 14.36-32 32-32h320c17.64 0 32 14.36 32 32V416zM272 352h-32V240C240 231.2 232.8 224 224 224H192C183.2 224 176 231.2 176 240S183.2 256 192 256h16v96h-32C167.2 352 160 359.2 160 368C160 376.8 167.2 384 176 384h96c8.836 0 16-7.164 16-16C288 359.2 280.8 352 272 352z"/>
					        </svg>
					        Breadcrumbs Multi Block
					    </div>
					    <div class="components-placeholder__fieldset">
					        This block is only available in the Breadcrumbs Pro version. More information and upgrade option is available in the Settings -> BreadcrumbsPress panel.
					    </div>
					</div>';
    }

}
