<?php
/**
 * Premium Form List Class
 *
 * This is the constant class which lists all the texts
 * that need to be supported for the Premium form 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\FormHandler;
use WCSMSOTP\Traits\Instance;

/**
 * Premium Form List Class
 *
 * This is the constant class which lists all the texts
 * that need to be supported for the Premium form List.
 */
if ( ! class_exists( 'PremiumFormList' ) ) {
	/**
	 * Premium Form List Class
	 *
	 * This is the constant class which lists all the texts
	 * that need to be supported for the Premium form List.
	 */
	final class PremiumFormList {

		use Instance;

		/**
		 * Premium forms list.
		 *
		 * @var array<string, string>
		 */
		private $premium_forms;

		/**
		 * Enabled forms list.
		 *
		 * @var array<string, FormHandler>
		 */
		private $enabled_forms;

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

		/**
		 * Constructor.
		 *
		 * Initializes the premium forms list.
		 *
		 * @return void
		 */
		private function __construct() {
			// Get translation function if available.
			$translation_func = function_exists( 'mowc_' ) ? 'mowc_' : '__';
			$text_domain      = 'miniorange-sms-order-notification-otp-verification';

			// Helper function to translate strings.
			$translate = function ( $text ) use ( $translation_func, $text_domain ) {
				if ( function_exists( $translation_func ) ) {
					return call_user_func( $translation_func, $text, $text_domain );
				}
				return $text;
			};

			$this->premium_forms = array(
				'WCFM'          => esc_html( $translate( 'WooCommerce Frontend Manager Form (WCFM)' ) ),
				'CHECKOUT_WC'   => esc_html( $translate( 'Checkout WC Form' ) ),
				'WC_PASS_RESET' => esc_html( $translate( 'WooCommerce Password Reset Form' ) ),
			);

			$this->enabled_forms = array();
			$this->forms         = array();
		}

		/**
		 * Add form.
		 *
		 * Function called to add the form name.
		 *
		 * @param string      $key  Unique key for the form.
		 * @param FormHandler $form Form handler object.
		 * @return void
		 */
		public function add( $key, $form ) {
			if ( ! is_string( $key ) || empty( $key ) ) {
				return;
			}
			$key = sanitize_key( $key );

			if ( ! is_object( $form ) ) {
				return;
			}

			// Check if form implements FormHandler interface or has required methods.
			if ( ! ( $form instanceof FormHandler ) && ! method_exists( $form, 'is_form_enabled' ) ) {
				return;
			}

			if ( ! is_array( $this->forms ) ) {
				$this->forms = array();
			}
			if ( ! is_array( $this->enabled_forms ) ) {
				$this->enabled_forms = array();
			}

			$this->forms[ $key ] = $form;

			if ( method_exists( $form, 'is_form_enabled' ) ) {
				$is_enabled = $form->is_form_enabled();
				if ( is_bool( $is_enabled ) && $is_enabled ) {
					$this->enabled_forms[ $key ] = $form;
				}
			}
		}

		/**
		 * Get premium form list.
		 *
		 * Function called to get the premium form list.
		 *
		 * @return array<string, string> Premium form list array.
		 */
		public function get_premium_form_list() {
			return is_array( $this->premium_forms ) ? $this->premium_forms : array();
		}

		/**
		 * Get enabled forms list.
		 *
		 * Returns the list of enabled forms.
		 *
		 * @return array<string, FormHandler> Enabled forms array.
		 */
		public function get_enabled_forms() {
			return is_array( $this->enabled_forms ) ? $this->enabled_forms : array();
		}

		/**
		 * Get all forms.
		 *
		 * Returns the list of all forms.
		 *
		 * @return array<string, FormHandler> All forms array.
		 */
		public function get_all_forms() {
			return is_array( $this->forms ) ? $this->forms : array();
		}
	}
}
