<?php
/**
 * Add-On List Class
 *
 * This class manages and stores all add-ons supported by the plugin.
 * It provides methods to add and retrieve add-ons from the list.
 *
 * @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\BaseAddOnHandler;
use WCSMSOTP\Traits\Instance;

/**
 * Add-On List Class
 *
 * This class manages and stores all add-ons supported by the plugin.
 * It provides methods to add and retrieve add-ons from the list.
 */
if ( ! class_exists( 'AddOnList' ) ) {
	/**
	 * Add-On List Class
	 *
	 * This class manages and stores all add-ons supported by the plugin.
	 * It provides methods to add and retrieve add-ons from the list.
	 */
	final class AddOnList {

		use Instance;

		/**
		 * Key value pair associative array. This holds all the
		 * add-on objects which are initialized.
		 *
		 * @var array<string, BaseAddOnHandler>
		 */
		private $add_ons;

		/**
		 * Constructor.
		 *
		 * Initializes the add-ons array.
		 */
		private function __construct() {
			$this->add_ons = array();
		}

		/**
		 * Add add-on to the add-on list.
		 *
		 * @param string           $key  The add-on key identifier.
		 * @param BaseAddOnHandler $form The add-on handler object.
		 * @return void
		 */
		public function add( $key, $form ) {
			if ( ! is_string( $key ) || empty( $key ) ) {
				return;
			}
			if ( ! is_object( $form ) || ! ( $form instanceof BaseAddOnHandler ) ) {
				return;
			}
			$key                   = sanitize_key( $key );
			$this->add_ons[ $key ] = $form;
		}

		/**
		 * Get the add-on list.
		 *
		 * @return array<string, BaseAddOnHandler> The array of add-ons.
		 */
		public function get_list() {
			return $this->add_ons;
		}
	}
}
