<?php

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

class AIEO_Post_Relations_Shortcodes {

    
    private static $instance = null;

    public static function instance() {
        if ( self::$instance === null ) { self::$instance = new self(); }
        return self::$instance;
    }

    private function __construct() {
        add_action( 'init', array( $this, 'register_shortcodes' ) );
    }

    public function register_shortcodes() {
        
        add_shortcode( 'aieo_related_brands',     array( $this, 'sc_related_brands' ) );
        add_shortcode( 'aieo_related_categories', array( $this, 'sc_related_categories' ) );
        add_shortcode( 'aieo_related_needs',      array( $this, 'sc_related_needs' ) );

        
        
        
        add_shortcode( 'related_brand_shortcode',   array( $this, 'sc_related_brands' ) );
        add_shortcode( 'secondary_brand_shortcode', array( $this, 'sc_related_categories' ) );
    }

    
    public function sc_related_brands( $atts = array() ) {
        $atts = shortcode_atts( array(
            'auto_rotate' => '1',
            'rotate_ms'   => '4000',
            'image_size'  => 'medium',
        ), $atts, 'aieo_related_brands' );

        return $this->render( 'aieo/post-related-brands', array(
            'autoRotate' => in_array( strtolower( (string) $atts['auto_rotate'] ), array( '1', 'true', 'yes', 'on' ), true ),
            'rotateMs'   => max( 1500, (int) $atts['rotate_ms'] ),
            'imageSize'  => (string) $atts['image_size'],
        ) );
    }

    
    public function sc_related_categories( $atts = array() ) {
        $atts = shortcode_atts( array(
            'label' => '',
        ), $atts, 'aieo_related_categories' );

        return $this->render( 'aieo/post-related-categories', array(
            'label' => (string) $atts['label'],
        ) );
    }

    
    public function sc_related_needs( $atts = array() ) {
        $atts = shortcode_atts( array(
            'heading' => '',
        ), $atts, 'aieo_related_needs' );

        return $this->render( 'aieo/post-related-needs', array(
            'heading' => (string) $atts['heading'],
        ) );
    }

    
    private function render( $name, array $attrs ) {
        if ( ! function_exists( 'render_block' ) ) { return ''; }
        return (string) render_block( array(
            'blockName'    => $name,
            'attrs'        => $attrs,
            'innerBlocks'  => array(),
            'innerHTML'    => '',
            'innerContent' => array(),
        ) );
    }
}

AIEO_Post_Relations_Shortcodes::instance();
