<?php
declare( strict_types=1 );

namespace FiboFilters\Blocks;

use FiboFilters\Config\Env;
use FiboFilters\Filtering\Front;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Filters class
 */
class Filters {
	/** @var Front */
	protected $front;

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

	public function init(): void {
		$this->register_block();
	}

	private function register_block(): void {
		register_block_type(
			Env::$build_path . '/blocks/filters',
			[ 'render_callback' => [ $this, 'render_callback' ] ]
		);
	}

	/**
	 * Render "FiboFilters/Filters" blocks content
	 *
	 * @param array $attributes The block attributes.
	 */
	public function render_callback( array $attributes ): string {
		$attributes = wp_parse_args(
			$attributes,
			[
				'layout'     => 'vertical',
				'min_screen' => '',
				'max_screen' => '',
			]
		);

		$settings = '';

		if ( ! empty( $attributes['layout'] ) ) {
			$settings .= sprintf( ' layout="%s"', $attributes['layout'] );
		}
		if ( ! empty( $attributes['min_screen'] ) ) {
			$settings .= sprintf( ' min_screen="%d"', $this->remove_px( $attributes['min_screen'] ) );
		}
		if ( ! empty( $attributes['max_screen'] ) ) {
			$settings .= sprintf( ' max_screen="%d"', $this->remove_px( $attributes['max_screen'] ) );
		}

		return do_shortcode( '[fibofilters' . $settings . ']' );
	}

	private function remove_px( string $value ): string {
		return str_replace( 'px', '', $value );
	}
}
