<?php
declare( strict_types=1 );

namespace FiboFilters\Settings;

use FiboFilters\Config\Env;

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

/**
 * Class Onboarding
 */
class Onboarding {
	const OPTION_NAME = 'onboarding_finished';

	public function is_finished(): bool {
		return get_option( Env::$plugin_prefix . self::OPTION_NAME, '0' ) === '1';
	}

	public function finish(): void {
		update_option( Env::$plugin_prefix . self::OPTION_NAME, '1' );
	}

	public function insert_block_into_widget_area( string $widget_area ): void {
		$widget_block = get_option( 'widget_block' );
		$content      = '<!-- wp:fibofilters/filters /-->';

		if ( ! is_numeric( array_search( [ 'content' => $content ], $widget_block, true ) ) ) {
			$widget_block[] = [ 'content' => $content ];
			update_option( 'widget_block', $widget_block );

			$block_id_in_widget_area = array_search( [ 'content' => $content ], $widget_block, true );
			$sidebars_widgets        = wp_get_sidebars_widgets();
			$widget_area_id_exist    = array_search( 'block-' . $block_id_in_widget_area, $sidebars_widgets[ $widget_area ], true );

			if ( ! is_numeric( $widget_area_id_exist ) ) {
				$sidebars_widgets[ $widget_area ][] = 'block-' . $block_id_in_widget_area;
				wp_set_sidebars_widgets( $sidebars_widgets );
			}
		}
	}
}
