<?php
namespace Misuzu\Parsers\BBCode\Tags;

use Misuzu\Parsers\BBCode\BBCodeTag;

final class AudioTag extends BBCodeTag {
    public function parseText(string $text): string {
        return preg_replace_callback(
            '#\[audio\]((?:https?:)?\/\/.+?)\[/audio\]#',
            function ($matches) {
                $url = parse_url($matches[1]);

                if(!empty($url['scheme']) && !in_array(mb_strtolower($url['scheme']), ['http', 'https'], true))
                    return $matches[0];

                return sprintf(
                    '<span class="js-msz-embed-media" data-msz-embed-url="%1$s"><noscript><a href="%1$s" target="_blank" class="link" rel="noopener noreferrer">%1$s</a></noscript></span>',
                    $matches[1]
                );
            },
            $text
        );
    }
}
