<?php

namespace WPDesk\FakturyKSEF\Blocks;

use WPDesk\FakturyKSEF\Blocks\Replacers\KSeFQRCodeReplacer;
use WPDeskKSEFVendor\WPDesk\PluginBuilder\Plugin\Hookable;

class RegisterEditorBlocks implements Hookable {

	private $plugin_info;

	public function __construct( $plugin_info ) {
		$this->plugin_info = $plugin_info;
	}

	public function hooks() {
		add_filter( 'fi/core/blocks/block_list', [ $this, 'register_block_types' ] );
		add_filter( 'fi/core/blocks/replacers', [ $this, 'add_block_replacers' ], 10, 1 );
		add_filter( 'fi/core/blocks/allowed_block_types', [ $this, 'allow_block_as_fi_block' ], 10, 1 );
		$this->register_blocks();
	}

	public function allow_block_as_fi_block( $fi_blocks ) {
		$fi_blocks[] = 'flexible-invoices/document-qr-code';
		return $fi_blocks;
	}

	public function register_block_types( $registered_blocks ) {
		$registered_blocks[] = 'document-qr-code';
		return $registered_blocks;
	}

	public function add_block_replacers( $registered_block_replacers ) {
		$registered_block_replacers['document-qr-code'] = new KSeFQRCodeReplacer();
		return $registered_block_replacers;
	}

	public function register_blocks() {
		register_block_type(
			$this->plugin_info->get_plugin_dir() . '/assets/js/blocks/document-qr-code',
		);
	}
}
