<?php
/**
 * Form List Class
 *
 * This class manages and stores all forms supported by the plugin.
 * It maintains a list of all forms and a separate list of enabled forms.
 *
 * @package miniorange-sms-order-notification-otp-verification/helper
 * @since   1.0.0
 */

namespace WCSMSOTP\Helper;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use WCSMSOTP\Objects\FormHandler;
use WCSMSOTP\Traits\Instance;

/**
 * Form List Class
 *
 * This class manages and stores all forms supported by the plugin.
 * It maintains a list of all forms and a separate list of enabled forms.
 */
if ( ! class_exists( 'MoFormList' ) ) {
	/**
	 * Form List Class
	 *
	 * This class manages and stores all forms supported by the plugin.
	 * It maintains a list of all forms and a separate list of enabled forms.
	 */
	final class MoFormList {

		use Instance;

		/**
		 * Key-value pair associative array. This holds all the
		 * form objects which are initialized.
		 *
		 * @var array<string, FormHandler>
		 */
		private $forms;

		/**
		 * Key-value pair associative array. This holds all the form
		 * objects which are active/enabled.
		 *
		 * @var array<string, FormHandler>
		 */
		private $enabled_forms;

		/**
		 * Constructor.
		 *
		 * Initializes the forms and enabled_forms arrays.
		 */
		private function __construct() {
			$this->forms         = array();
			$this->enabled_forms = array();
		}

		/**
		 * Add form to the form list.
		 *
		 * Adds a form handler to the forms list. If the form is enabled,
		 * it is also added to the enabled forms list.
		 *
		 * @param string      $key  The form key.
		 * @param FormHandler $form The form handler object of the form.
		 * @return void
		 */
		public function add( $key, $form ) {
			if ( ! is_string( $key ) || empty( $key ) ) {
				return;
			}
			if ( ! is_object( $form ) ) {
				return;
			}
			if ( ! ( $form instanceof FormHandler ) ) {
				return;
			}
			$key                 = sanitize_key( $key );
			$this->forms[ $key ] = $form;
			if ( method_exists( $form, 'is_form_enabled' ) && $form->is_form_enabled() ) {
				$this->enabled_forms[ $key ] = $form;
			}
		}

		/*
		|---------------------------------------------------------------------------
		| Getters
		|---------------------------------------------------------------------------
		 */

		/**
		 * Get list of all forms.
		 *
		 * Returns the complete list of all forms (enabled and disabled).
		 *
		 * @return array<string, FormHandler> Array of all form handlers.
		 */
		public function get_list() {
			return $this->forms;
		}

		/**
		 * Get list of enabled forms.
		 *
		 * Returns only the forms that are currently enabled.
		 *
		 * @return array<string, FormHandler> Array of enabled form handlers.
		 */
		public function get_enabled_forms() {
			return $this->enabled_forms;
		}
	}
}
