<?php

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

if ( ! class_exists( 'AIEO_Block_Header', false ) ) {

class AIEO_Block_Header {

    private static $css_done = false;

    
    public static function defaults() {
        return array(
            'secHeading'      => '',
            'secHeadingUpper' => true,
            
            
            
            
            'secHeadingFeature' => false,
            'secHeadingSize'  => '2rem',
            'secHeadingSizeM' => '1.4rem',
            'secDivider'      => true,
            'secDesc'         => '',
            'secDescSize'     => '1rem',
            'secDescSizeM'    => '0.9rem',
            'secAlign'        => 'left', 
        );
    }

    
    public static function block_attributes() {
        $a = array();
        foreach (self::defaults() as $k => $v) {
            $a[$k] = array('type' => is_bool($v) ? 'boolean' : 'string', 'default' => $v);
        }
        return $a;
    }

    private static function size($v, $def) {
        $v = trim((string) $v);
        return preg_match('/^[0-9]*\.?[0-9]+\s*(rem|em|px|%|vw|vh|pt)$/i', $v) ? $v : $def;
    }

    
    public static function render($attrs) {
        $d = self::defaults();
        $a = array();
        foreach ($d as $k => $v) { $a[$k] = array_key_exists($k, (array) $attrs) ? $attrs[$k] : $v; }

        $heading = trim((string) $a['secHeading']);
        $desc    = trim((string) $a['secDesc']);
        if ($heading === '' && $desc === '') { return ''; }

        $align = in_array($a['secAlign'], array('left', 'center', 'right'), true) ? $a['secAlign'] : 'left';
        $style = '--aieo-sh-h-d:' . self::size($a['secHeadingSize'],  '2rem')
               . ';--aieo-sh-h-m:' . self::size($a['secHeadingSizeM'], '1.4rem')
               . ';--aieo-sh-d-d:' . self::size($a['secDescSize'],     '1rem')
               . ';--aieo-sh-d-m:' . self::size($a['secDescSizeM'],    '0.9rem')
               . ';text-align:' . $align;
        
        
        
        
        $feature   = !empty($a['secHeadingFeature']);
        $title_cls = $feature
            ? 'aieo-recommendations-title'
            : 'aieo-secthead__title' . (!empty($a['secHeadingUpper']) ? ' aieo-secthead__title--upper' : '');

        $out  = self::css_tag();
        $out .= '<div class="aieo-secthead" style="' . esc_attr($style) . '">';
        if ($heading !== '') { $out .= '<h2 class="' . esc_attr($title_cls) . '">' . esc_html($heading) . '</h2>'; }
        if (!$feature && !empty($a['secDivider']) && ($heading !== '' || $desc !== '')) { $out .= '<hr class="aieo-secthead__line">'; }
        if ($desc !== '') { $out .= '<p class="aieo-secthead__desc">' . esc_html($desc) . '</p>'; }
        $out .= '</div>';
        return $out;
    }

    private static function css_tag() {
        if (self::$css_done) { return ''; }
        self::$css_done = true;
        return '<style id="aieo-secthead-css">' . self::css() . '</style>';
    }

    public static function css() {
        return '.aieo-secthead{margin:0 0 18px}'
            . '.aieo-secthead__title{margin:0 0 8px;font-weight:700;line-height:1.15;font-size:var(--aieo-sh-h-d,2rem)}'
            . '.aieo-secthead__title--upper{text-transform:uppercase;letter-spacing:.02em}'
            . '.aieo-secthead__line{border:0;border-top:1px solid currentColor;opacity:.25;margin:0 0 10px;width:100%}'
            . '.aieo-secthead__desc{margin:0;opacity:.85;line-height:1.5;font-size:var(--aieo-sh-d-d,1rem)}'
            
            
            
            . '.aieo-secthead .aieo-recommendations-title::after{left:0;width:100%}'
            . '@media(max-width:767px){.aieo-secthead__title{font-size:var(--aieo-sh-h-m,1.4rem)}.aieo-secthead__desc{font-size:var(--aieo-sh-d-m,.9rem)}}';
    }
}
}
