import { AfterViewInit, Component, Input, OnInit, Renderer2 } from '@angular/core';
import { environment } from '@environment';

@Component({
  selector: 'chat-bot',
  templateUrl: './chat-bot.component.html',
  styleUrls: ['./chat-bot.component.scss']
})
export class ChatBotComponent implements OnInit, AfterViewInit {

  constructor(private renderer: Renderer2) { }

  urlForChatBot : string;

  ngOnInit(): void { 
    this.urlForChatBot = environment.urlForChatBot;
  }

  ngAfterViewInit(): void {
    const script = this.renderer.createElement('script');

    const scriptId = 'pit-chat-widget';
    this.renderer.setAttribute(script, 'id', scriptId);
    
    script.type = 'text/javascript';
    script.src = this.urlForChatBot;

    script.onload = () => {
      console.debug('Чат-бот успешно загружен.');
    };

    const containerName = '#app-chat-bot-container'

    const container = this.renderer.selectRootElement(containerName, true);
    this.renderer.appendChild(container, script);
  }
}
