<?php
/**
 *
 * @since 6.4.0
 */

namespace WPTravelEngine\Builders\FormFields;

use WPTravelEngine\Abstracts\BookingEditFormFields;
use WTE_Default_Form_Fields;
/**
 * Form field class to render billing form fields.
 *
 * @since 6.4.0
 */
class EmergencyEditFormFields extends BookingEditFormFields {

	/**
	 * Emergency contact count
	 *
	 * @var int
	 */
	protected $count;

	public function __construct( array $defaults = array(), string $mode = 'edit', $booking = null ) {
		parent::__construct( $defaults, $mode );
		$this->count = $defaults['index'] ?? ( $defaults['total_count'] ?? 0 ) + 1;
		$this->init( $this->map_fields( static::structure( $mode, $booking ) ) );
	}

	public static function create( ...$args ): EmergencyEditFormFields {
		return new static( ...$args );
	}

	protected function map_field( $field ) {
		$name = null;

		$field = parent::map_field( $field );

		// Extract the name using regex patterns.
		if ( preg_match( '#\[([^\[]+)]$#', $field['name'], $matches ) ) {
			$name = $matches[1];
		} elseif ( preg_match( '/^[^\s]+$/', $field['name'], $matches ) ) {
			$name = $matches[0];
		}

		// If a name was found, set field attributes.
		if ( $name ) {
			$field['name']                    = sprintf( 'emergency_contacts[%s][]', $name );
			$field['id']                      = sprintf( 'emergency_contacts_%s', $name );
			$field['field_label']             = isset( $field['placeholder'] ) && $field['placeholder'] !== '' ? $field['placeholder'] : ( $field['field_label'] ?? '' );
			$field['default']                 = $this->defaults[ $name ] ?? $field['default'] ?? '';
			$field['validations']['required'] = false;
			if ( $field['type'] == 'country' ) {
				$field = $this->resolve_country_field_default( $field );
			}
		}

		// Convert datepicker to text input to fix styling issue and to enable time selection.
		if ( $field['type'] === 'datepicker' ) {
			$field = $this->apply_datepicker_field( $field );
		}

		if ( static::$mode !== 'edit' && ! ( $field['skip_disabled'] ?? false ) ) {
			$field['option_attributes'] = array(
				'disabled' => 'disabled',
			);
			$field['attributes']        = array(
				'disabled' => 'disabled',
			);
		}

		if ( $field['type'] == 'checkbox' || $field['type'] == 'radio' && isset( $this->count ) ) {
			$field['name'] = sprintf( 'emergency_contacts[%s][%s]', $name, $this->count );
		}
		return $field;
	}

	/**
	 * Structure the form fields.
	 *
	 * @param string $mode
	 * @param object $booking
	 * @return array
	 */
	public static function structure( string $mode = 'edit', $booking = null ): array {
		if ( $booking && $booking->get_meta( 'traveller_page_type' ) == 'old' ) {
			return WTE_Default_Form_Fields::emergency_contact();
		} else {
			return DefaultFormFields::emergency( $mode );
		}
	}
}
