<?php

namespace Blazemedia\DriveKIntegrator\Render;

class DriveKwidget
{
    /**
     * Restituisce il markup fornito per lo shortcode, usando gli attributi se presenti.
     *
     * @param array $atts
     * @return string
     */
    public static function render($atts = [])
    {

        $defaults = [
            'brand_id' => '',
            'model_id' => '',
        ];
        $atts = \shortcode_atts($defaults, $atts);
        $makeUrlCode = htmlspecialchars($atts['brand_id'], ENT_QUOTES, 'UTF-8');
        $selectedModelId = htmlspecialchars($atts['model_id'], ENT_QUOTES, 'UTF-8');
        
        // Extract domain from site URL (e.g., hdmotori.it)
        $domain = preg_replace('/^.*?\.(.*?\..*?)$/', '$1', \home_url());
        $modalTemplate = "blazemedia_" . $domain;

        return WidgetMarkup::getMarkup($makeUrlCode, $selectedModelId, $modalTemplate);
    }

    public static function init()
    {
        \add_action('init', function () {
            \add_shortcode('drivek_widget', [self::class, 'render']);
        });
    }
}
