<?php

if (!defined('ABSPATH')) {
    exit;
}

class AIEO_Block_Patterns {

    private static $registered = false;

    public static function bootstrap() {
        if (self::$registered) { return; }
        self::$registered = true;
        add_action('init', array(__CLASS__, 'register_category'), 25);
        add_action('init', array(__CLASS__, 'register_patterns'), 35);
    }

    public static function register_category() {
        if (!function_exists('register_block_pattern_category')) { return; }
        register_block_pattern_category('aieo', array(
            'label' => __('AIEO', 'ai-eshop-optimizer'),
        ));
    }

    public static function register_patterns() {
        if (!function_exists('register_block_pattern')) { return; }
        $base = AIEO_DIR . 'includes/dmm/blocks/patterns/';
        if (!is_dir($base)) { return; }

        foreach (scandir($base) as $entry) {
            if ($entry === '' || $entry[0] === '.') { continue; }
            if (substr($entry, -4) !== '.php') { continue; }
            $path = $base . $entry;
            if (!is_file($path)) { continue; }

            $args = include $path;
            if (!is_array($args)) { continue; }

            $slug = 'aieo/' . substr($entry, 0, -4);
            if (!isset($args['categories']) || !is_array($args['categories'])) {
                $args['categories'] = array('aieo');
            }
            register_block_pattern($slug, $args);
        }
    }
}
