<?php 

/**
 * Registers block patterns and categories.
 *
 * @since 1.0
 */
function the_construction_register_block_patterns() {
    $default_categories = array(
        'the-construction' => array(
            'label' => esc_html__('the-construction', 'the-construction'),
            'description' => esc_html__('Patterns designed specifically for The Construction theme', 'the-construction'),
        ),
        'the-construction-pages' => array(
            'label' => esc_html__('Pages', 'the-construction'),
            'description' => esc_html__('Full page layouts', 'the-construction'),
        ),
        'the-construction-sections' => array(
            'label' => esc_html__('Sections', 'the-construction'),
            'description' => esc_html__('Content section patterns', 'the-construction'),
        ),
    );

    $block_pattern_categories = apply_filters('the_construction_block_pattern_categories', $default_categories);

    foreach ($block_pattern_categories as $name => $properties) {
        if (!WP_Block_Pattern_Categories_Registry::get_instance()->is_registered($name)) {
            register_block_pattern_category($name, $properties);
        }
    }

    $patterns_directory = get_template_directory() . '/patterns/';

    if (!file_exists($patterns_directory)) {
        return;
    }

    $pattern_files = array_diff(scandir($patterns_directory), array('..', '.'));

    foreach ($pattern_files as $file) {
        $file_path = $patterns_directory . $file;

        if (!is_file($file_path) || pathinfo($file_path, PATHINFO_EXTENSION) !== 'php') {
            continue;
        }

        $pattern_data = get_file_data($file_path, array(
            'title'       => 'Title',
            'slug'        => 'Slug',
            'description' => 'Description',
            'categories'  => 'Categories',
            'keywords'    => 'Keywords',
            'viewport'    => 'ViewportWidth',
        ));

        if (empty($pattern_data['title'])) {
            continue;
        }

        ob_start();
        include $file_path;
        $pattern_content = ob_get_clean();

        if (empty($pattern_content)) {
            continue;
        }

        $categories = array('the-construction');
        if (!empty($pattern_data['categories'])) {
            $additional_cats = array_map('trim', explode(',', $pattern_data['categories']));
            $categories = array_merge($categories, $additional_cats);
        }

        register_block_pattern(
            'the-construction/' . (!empty($pattern_data['slug']) ? sanitize_title($pattern_data['slug']) : sanitize_title($pattern_data['title'])),
            array(
                'title'       => $pattern_data['title'],
                'description' => !empty($pattern_data['description']) ? $pattern_data['description'] : '',
                'content'     => $pattern_content,
                'categories'  => $categories,
                'keywords'    => !empty($pattern_data['keywords']) ? explode(',', $pattern_data['keywords']) : array(),
                'viewport'    => !empty($pattern_data['viewport']) ? (int) $pattern_data['viewport'] : null,
            )
        );
    }
}
add_action('init', 'the_construction_register_block_patterns', 9);


if ( ! function_exists( 'the_construction_block_styles' ) ) :
	/**
	 * Registers custom block styles.
	 *
	 * @since The Construction 1.0
	 *
	 * @return void
	 */
	function the_construction_block_styles() {
		register_block_style(
			'core/list',
			array(
				'name'         => 'checkmark-list',
				'label'        => __( 'Checkmark', 'the-construction' ),
				'inline_style' => '
				ul.is-style-checkmark-list {
					list-style-type: "\2713";
				}
				ul.is-style-checkmark-list li {
					padding-inline-start: 1ch;
				}',
			)
		);
	}
endif;
add_action( 'init', 'the_construction_block_styles' );


function the_construction_enqueue_block_styles() {
	wp_enqueue_style(
		'the-construction-block-styles',
		get_template_directory_uri() . '/assets/css/block-styles.css',
		array(),
		wp_get_theme()->get( 'Version' )
	);
}
add_action( 'enqueue_block_assets', 'the_construction_enqueue_block_styles' );


function the_construction_enqueue_theme_assets() {
 
    wp_enqueue_style(
        'the-construction-main-css',
        get_template_directory_uri() . '/assets/css/the-construction.css',
        array(),
        '1.0.0'
    );
   
    wp_enqueue_script(
        'the-construction-scrolltotop-js',
        get_template_directory_uri() . '/assets/js/the-construction-scrolltotop.js',
        array(),
        wp_get_theme()->get('Version'),
        true 
    );
   

   }
   add_action('wp_enqueue_scripts', 'the_construction_enqueue_theme_assets');


function the_construction_enqueue_swiper_assets() {
 
    wp_enqueue_style(
        'the-construction-swiper-css',
        get_template_directory_uri() . '/assets/css/swiper-bundle.min.css',
        array(),
        '11.0.0'
    );
    wp_enqueue_style(
        'the-construction-custom-swiper-css',
        get_template_directory_uri() . '/assets/css/custom-swiper.css',
        array(),
        '1.0.0'
    );

    wp_enqueue_script(
        'the-construction-swiper-js',
        get_template_directory_uri() . '/assets/js/swiper-bundle.min.js',
        array(),
        '11.0.0',
        true 
    );
   
    wp_enqueue_script(
        'the-construction-custom-swiper',
        get_template_directory_uri() . '/assets/js/the-construction-custom-swiper.js',
        array('the-construction-swiper-js'), 
        '1.0.0',
        true 
    );
   }
   add_action('wp_enqueue_scripts', 'the_construction_enqueue_swiper_assets');


   
/**
 * Info
 */
require get_theme_file_path( '/inc/admin/info.php' );
require_once get_template_directory() . '/inc/pro-themes.php';

/**
 * Load TGM.
 */
require get_template_directory() . '/inc/tgm/tgm.php';
