import {
  Component,
  computed,
  inject,
  input
} from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { extractSoundcloudResource } from '../../../../../domain/rules/link-embed.rules';

@Component({
  selector: 'app-chat-soundcloud-embed',
  standalone: true,
  templateUrl: './chat-soundcloud-embed.component.html'
})
export class ChatSoundcloudEmbedComponent {
  readonly url = input.required<string>();

  readonly resource = computed(() => extractSoundcloudResource(this.url()));

  readonly embedHeight = computed(() => this.resource()?.type === 'playlist' ? 352 : 166);

  readonly embedUrl = computed(() => {
    const resource = this.resource();

    if (!resource) {
      return '';
    }

    const embedUrl = new URL('https://w.soundcloud.com/player/');

    embedUrl.searchParams.set('url', resource.canonicalUrl);
    embedUrl.searchParams.set('auto_play', 'false');
    embedUrl.searchParams.set('hide_related', 'false');
    embedUrl.searchParams.set('show_comments', 'false');
    embedUrl.searchParams.set('show_user', 'true');
    embedUrl.searchParams.set('show_reposts', 'false');
    embedUrl.searchParams.set('show_teaser', 'true');
    embedUrl.searchParams.set('visual', resource.type === 'playlist' ? 'true' : 'false');

    return this.sanitizer.bypassSecurityTrustResourceUrl(embedUrl.toString());
  });

  private readonly sanitizer = inject(DomSanitizer);
}
