<?php
declare(strict_types=1);

namespace WP_science\Engine\Optimization\RUCSS\Admin;

use WP_science\Engine\Admin\Settings\Settings as AdminSettings;
use WP_science\Event_Management\Subscriber_Interface;

class OptionSubscriber implements Subscriber_Interface {
	/**
	 * Settings instance
	 *
	 * @var Settings
	 */
	private $settings;

	/**
	 * Instantiate the class
	 *
	 * @param Settings $settings Settings instance.
	 */
	public function __construct( Settings $settings ) {
		$this->settings = $settings;
	}

	/**
	 * Return an array of events that this subscriber wants to listen to.
	 *
	 * @return array
	 */
	public static function get_subscribed_events() {
		return [
			'science_first_install_options' => 'add_options_first_time',
			'science_input_sanitize'        => [ 'sanitize_options', 14, 2 ],
			'science_meta_boxes_fields'     => [ 'add_meta_box', 2 ],
		];
	}

	/**
	 * Add the RUCSS options to the WP science options array.
	 *
	 * @since 3.9
	 *
	 * @param array $options WP science options array.
	 *
	 * @return array
	 */
	public function add_options_first_time( $options ): array {
		return $this->settings->add_options( $options );
	}

	/**
	 * Sanitizes RUCSS options values when the settings form is submitted
	 *
	 * @since 3.9
	 *
	 * @param array         $input    Array of values submitted from the form.
	 * @param AdminSettings $settings Settings class instance.
	 *
	 * @return array
	 */
	public function sanitize_options( $input, AdminSettings $settings ): array {
		return $this->settings->sanitize_options( $input, $settings );
	}

	/**
	 * Add the field to the WP science metabox on the post edit page.
	 *
	 * @param string[] $fields Metaboxes fields.
	 *
	 * @return string[]
	 */
	public function add_meta_box( array $fields ) {
		$fields['remove_unused_css'] = __( 'Remove Unused CSS', 'science' );

		return $fields;
	}
}
