<?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  Modal
 * @since		6.1.5
 * @version    	6.6.8
 */

defined( 'ABSPATH' ) || exit;

use function WeCodeArt\Functions\{ toJSON, array_extract };

\wecodeart( 'styles' )->Components->load( [ 'modal' ] );

\wp_enqueue_script( 'wecodeart-support-assets-backdrop' );
\wp_enqueue_script( 'wecodeart-support-assets-focustrap' );

\wp_enqueue_script_module( '@wecodeart/modal' );

\wp_interactivity_state( 'wecodeart/modal', apply_filters( 'wecodeart/filter/interactive/state/modal', [
	'backdrop'	=> true,
	'focus'	    => true,
	'keyboard'	=> true,
] ) );
	
\wp_interactivity_config( 'wecodeart/modal', [
	'backdrop'	=> '(boolean|string)',
	'focus'	    => 'boolean',
	'keyboard'	=> 'boolean',
] );

/**
 * @param   mixed  	$id			Modal ID
 * @param   string	$title		Modal Title
 * @param   boolean	$close		Modal Close
 * @param   string	$content	Modal Content
 * @param   string	$content	Modal Footer
 */

$classnames = [ 'wp-modal' ]; 
$classnames = array_merge( $classnames, $classes ?? [] );

$dialog_cls = [ 'wp-modal__dialog' ];
$dialog_cls = array_merge( $dialog_cls, array_extract( $classnames, '__dialog--' ) );

$title      = $title ?? false;
$close 		= $close ?? true;
$content    = $content ?? false;
$footer     = $footer ?? false;

// Allways goes to footer
\add_action( 'wp_footer', static function() use ( $classnames, $dialog_cls, $close, $id, $title, $content, $footer ) {
?>
<div class="<?php echo esc_attr( join( ' ', $classnames) ); ?>" id="<?php echo esc_attr( $id ); ?>-modal" aria-hidden="true" tabindex="-1"
    <?php if( ! empty( $title ) ) : ?>
	aria-labelledby="<?php echo esc_attr( $id ); ?>-modal-title"
	<?php endif; ?>
    >
    <div class="<?php echo esc_attr( join( ' ', $dialog_cls ) ); ?>">
        <div class="wp-modal__content">
            <?php if( ! empty( $title ) || $close ) : ?>
            <div class="wp-modal__header">
                <?php if( ! empty( $title ) ) : ?>
                <h5 class="wp-modal__title" id="<?php echo esc_attr( $id ); ?>-modal-title"><?php echo esc_html( $title ); ?></h5>
                <?php endif; ?>
                <?php if( $close ) wecodeart_template( 'general/close', [ 'close' => 'modal' ] ); ?>
            </div>
            <?php endif; ?>
            <div class="wp-modal__body"><?php
            
                // Content markup should be escaped before passing to this view.
                echo $content;
                
            ?></div>
            <?php if( $footer ) : ?>
            <div class="wp-modal__footer"><?php echo wp_kses_post( $footer ); ?></div>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php
}, 0 );