<?php
/**
 * WeCodeArt Framework
 *
 * WARNING: This file is part of the core WeCodeArt Framework. DO NOT edit this file under any circumstances.
 * Please do all modifications in the form of a child theme.
 *
 * @package 	WeCodeArt Framework 
 * @subpackage 	Markup\Inputs
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since		5.0.0
 * @version		6.4.1
 */

namespace WeCodeArt\Support\DOM\Inputs;

defined( 'ABSPATH' ) || exit;

use function WeCodeArt\Functions\get_prop;

/**
 * Standard Inputs Markup
 */
abstract class Base {

    /**
     * Input's Type.
     *
     * @since   5.0.0
     * @var     string
     */
    public $type = '';

    /**
	 * Unique ID for this field.
	 *
	 * @var string
	 */
	public $unique_id;

    /**
     * Input's Label Position.
     *
     * @since   5.0.0
     * @var     string
     */
    public $_label = 'before';

    /**
     * Input's Label.
     *
     * @since   5.0.0
     * @var     string
     */
    public $label = '';

    /**
     * All Attrs tied to the control.
     *
     * @since   5.0.0
     * @var     array
     */
    public $attrs = [];
    
    /**
     * All Messages tied to the control.
     *
     * @since   5.0.0
     * @var     array
     */
    public $messages = [];

    /**
     * Input's Messages.
     *
     * @since   5.0.0
     * @var     boolean
     */
    public $with_messages = true;

    /**
     * Input's Style Deps.
     *
     * @since   6.4.1
     * @var     array
     */
    static $style_deps = [];

	/**
	 * Constructor 
	 */
	public function __construct( $type = '', array $args = [] ) {
        $this->unique_id    = wp_unique_id( 'input-' );
        $this->label        = get_prop( $args, 'label', '' );
        $this->_label       = get_prop( $args, '_label', 'before' );
        $this->attrs        = get_prop( $args, 'attrs', [] );
        $this->messages     = get_prop( $args, 'messages', [] );
    }

    /**
     * Renders the control wrapper and calls $this->content() for the internals.
     *
     * @since   5.0.0
     */
    protected function render() {
        echo $this->get_content();
    }

    /**
     * Get the control's content.
     *
     * @since   5.0.0
     * @version 5.4.2
     *
     * @return  string Contents of the control.
     */
    final public function get_content() {
        $content = '';
        if( $this->_label === 'before' ) $content .= $this->get_label();
        ob_start();
        $this->content();
        $content .= ob_get_clean();
        if( $this->_label === 'after' ) $content .= $this->get_label();
        if( $this->with_messages ) $content .= $this->get_messages();

        return $content; // Is ok, we use markup wrap method which escapes all attributes.
    }

    /**
	 * Render the label HTML of the input
     *
     * @since   5.0.0
     * @version 5.4.4
     *
	 * @return	mixed|string
	 */
	public function get_label() {
        if( empty( $this->label ) ) return;

        return wecodeart( 'dom' )::wrap( $this->type . '-label', [
            [
                'tag'   => 'label',
                'attrs' => [
                    'class' => in_array( $this->type, [ 'radio', 'checkbox' ] ) ? 'form-check-label' : 'form-label',
                    'for'   => get_prop( $this->attrs, 'id', $this->unique_id )
                ]
            ]
        ], $this->label, [], false );
    }

    /**
	 * Render the messages HTML of the input
     *
     * @since   5.0.0
     * @version 5.4.7
	 * @param 	bool    $echo
     *
	 * @return	string
	 */
	public function get_messages( $echo = false ) {
		if( empty( $this->messages ) ) return;
        
		$html       = '';
        $messages   = wp_array_slice_assoc( $this->messages, [ 'valid', 'invalid' ] );
		$help       = isset( $this->messages['help'] ) ? (string) $this->messages['help'] : false;

		if( is_string( $help ) ) {
			$html .= sprintf( '<small class="help-text">%s</small>', $help );
		}

		if( $messages ) {
			foreach( $messages as $key => $msg ) {
                if( empty( $msg ) ) continue;

				$data   = is_string( $msg ) ? [ 'text' => $msg ] : $msg;
                $data   = array_map( 'trim', $data );

                if( empty( $data['text'] ) ) continue;
                
				$html .= sprintf(
                    '<span class="%s">%s</span>',
                    isset( $data['class'] ) ? $data['class'] : $key . '-tooltip',
                    $data['text']
                );
			}
		}

		if( $echo ) {
			echo wp_kses_post( $html );
			return;
		}

		return wp_kses_post( $html );
    }

    /**
     * Render the custom attributes for the control's input element.
     *
     * @since   5.0.0
     * @param 	array   $ommit Attributes to exclude
     */
    public function input_attrs( array $ommit = [] ) {
        $attributes = wp_parse_args( $this->attrs, [
            'type'  => $this->type,
            'name'  => $this->unique_id,
            'id'    => $this->unique_id,
            'class' => $this->input_class()
        ] );

        $attributes = ! empty( $ommit ) ? array_diff_key( $attributes, array_flip( $ommit ) ) : $attributes;
        
        echo wecodeart( 'dom' )::generate_attr( $this->type, $attributes );
    }

    /**
	 * Input class
	 *
	 * @since	6.4.1
     *
     * @return  string
	 */
	public function input_class(): string {
        return '';
    }
    	
	/**
	 * Create HTML Inputs
	 *
	 * @since	unknown
	 * @version	5.0.0
	 */
	abstract function content();

    /**
	 * Input styles.
	 *
	 * @return 	string
	 */
	abstract static function styles(): string;
}