<?php
declare(strict_types=1);

namespace WP_AUX\Entities\Embeds\VideoSource;

use WP_AUX\Entities\Embeds\VideoSourceEntity;

class YoutubeVideoSourceEntity extends VideoSourceEntity
{
  public static string $urlPattern = '/.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\\&\\?]*).*/';
  public static string $embedPattern = 'https://youtube.com/embed/{id}?&rel=0';
  public static string $linkPattern = 'https://www.youtube.com/watch?v={id}';

  public static string $allow = 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share';
  public static string $allowFullscreen = 'allowfullscreen';
  public static string $referrerPolicy = 'strict-origin-when-cross-origin';

  protected function embedUrlParams(): string
  {
    $params = [];

    if ($this->autoplay) {
      $params[] = 'autoplay=1';
      $params[] = 'mute=1';
    } else {
      if ($this->muted) {
        $params[] = 'mute=1';
      }
    }

    if ($this->disableControls) {
      $params[] = 'controls=0';
    }

    if ($this->loop) {
      $params[] = 'loop=1&playlist=' . $this->id;
    }

    return join('&', $params);
  }
}
