<?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  Gutenberg\Blocks
 * @copyright   Copyright (c) 2025, WeCodeArt Framework
 * @since		5.0.0
 * @version		6.4.5
 */

namespace WeCodeArt\Gutenberg\Blocks\Query\Pagination;

defined( 'ABSPATH' ) || exit;

use WeCodeArt\Singleton;
use WeCodeArt\Gutenberg\Blocks\Dynamic;
use function WeCodeArt\Functions\get_prop;

/**
 * Gutenberg Query Pagination block.
 */
class Numbers extends Dynamic {

	use Singleton;

    /**
	 * Block namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'core';

	/**
	 * Block name.
	 *
	 * @var string
	 */
	protected $block_name = 'query-pagination-numbers';

	/**
	 * Block args.
	 *
	 * @return 	array
	 */
	public function init() {
		\add_filter( 'render_block_' . $this->get_block_type(), [ $this, 'render' ], 20, 1 );
	}

	/**
	 * Dynamically renders the `core/query-pagination-numbers` block.
	 *
	 * @param 	string 	$content 	The block markup.
	 *
	 * @return 	string 	The block markup.
	 */
	public function render( string $content = '' ): string {
        return $this->format_pagination( $content );
	}

    /**
	 * Formats paginate_links 
	 *
	 * @return 	void
	 */
    public function format_pagination( string $content = '' ): string {
        if( ! $content ) {
            return $content;
        }

        $dom 	= wecodeart( 'dom' )::create( $content );
		$div 	= wecodeart( 'dom' )::get_element( 'div', $dom );

        if( ! $div ) {
            return $content;
        }

        // Change to ul.
        $div    = wecodeart( 'dom' )::change_tag( $div, 'ul' );

        // Create a list wrapper
        $wrap	= wecodeart( 'dom' )::create_element( 'li', $dom );
		$wrap->setAttribute( 'class', 'wp-block-query-pagination-numbers__item' );

        $items = [];
        $count 	= $div->childNodes->count();
        
        for ( $i = 0; $i < $count; $i++ ) {
            $item = $div->childNodes->item( $i );
            if ( $item ) {
                $items[] = $item;
            }
        }

        $items = array_reverse( $items );

        for ( $i = 0; $i < $count; $i++ ) {
            $item = $items[$i];

            if ( ! method_exists( $item, 'setAttribute' ) ) {
                continue;
            }

            wecodeart( 'dom' )::add_classes( $item, [ 'wp-block-query-pagination-numbers__link' ] );

            $clone = wecodeart( 'dom' )::element( $wrap->cloneNode( true ) );
            $clone->appendChild( $item );
            $div->insertBefore( $clone, $div->firstChild );
        }

		$content = $dom->saveHTML();

        return $content;
    }

    /**
	 * Block styles
	 *
	 * @return 	string 	The block styles.
	 */
	public function styles() {
        $button_css = wecodeart_json( [ 'styles', 'elements', 'button' ], [] );

        $width      = get_prop( $button_css, [ 'border', 'width' ], '1px' );
        $style      = get_prop( $button_css, [ 'border', 'style' ], 'solid' );
        $color      = get_prop( $button_css, [ 'border', 'color' ], 'currentColor' );
        $color      = $color === 'transparent' ? 'currentColor' : $color;
        $radius     = get_prop( $button_css, [ 'border', 'radius' ], '.25rem' );
        
		return "
            .wp-block-query-pagination-numbers {
                display: flex;
                list-style-type: none;
                padding-left: 0;
                margin-bottom: 0;
            }
            .wp-block-query-pagination-numbers .page-numbers {
                position: relative;
                display: block;
                padding: .35rem .75rem;
                margin: 0;
                color: var(--wp--preset--color--primary);
                text-decoration: none;
                line-height: 1.5;
                background-color: var(--wp--preset--color--accent);
                border: $width $style $color;
                border-radius: inherit;
            }
            .wp-block-query-pagination-numbers .page-numbers:hover {
                z-index: 2;
                color: var(--wp--preset--color--white);
                background-color: var(--wp--preset--color--primary);
                border-color: var(--wp--preset--color--primary);
            }
            .wp-block-query-pagination-numbers .page-numbers:focus {
                z-index: 3;
                color: var(--wp--preset--color--white);
                background-color: var(--wp--preset--color--primary);
                outline: none;
                box-shadow: 0 0 0 1px var(--wp--preset--color--primary);
            }
            .wp-block-query-pagination-numbers > *:first-child {
                border-top-left-radius: $radius;
                border-bottom-left-radius: $radius;
            }
            .wp-block-query-pagination-numbers > *:not(:first-child) {
                margin-left: -1px;
            }
            .wp-block-query-pagination-numbers > *:last-child {
                border-top-right-radius: $radius;
                border-bottom-right-radius: $radius;
            }
            .wp-block-query-pagination-numbers .page-numbers.current {
                z-index: 3;
                color: white;
                background-color: var(--wp--preset--color--primary);
                border-color: var(--wp--preset--color--primary);
            } 
            .wp-block-query-pagination-numbers .page-numbers.disabled {
                color: var(--wp--gray);
                pointer-events: none;
                background-color: var(--wp--gray-100);
                border-color: currentColor;
            }
            .wp-block-query-pagination-numbers .page-numbers.dots {
                pointer-events: none;
            } 
		";
	}
}