import { Component, OnInit, ChangeDetectionStrategy, TemplateRef, Type } from '@angular/core';
import { IsaModalOverlayRef } from '../isa-modal-overlay-ref';
import { ModalContent } from '../defs';

@Component({
  selector: 'app-modal-dialogue',
  templateUrl: './modal.component.html',
  styleUrls: ['./modal.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ModalDialogueSharedComponent implements OnInit {
  contentType: 'data' | 'template' | 'string' | 'component' = 'component';
  content: ModalContent;
  context: any;

  constructor(private overlayRef: IsaModalOverlayRef) {}

  ngOnInit() {
    this.setContentType();
  }

  setContentType() {
    this.content = this.overlayRef.content;

    if (this.content instanceof TemplateRef) {
      this.contentType = 'template';
      this.context = {
        close: this.overlayRef.close.bind(this.overlayRef),
      };
      return;
    }

    this.contentType = 'component';
  }

  close() {
    this.overlayRef.close();
  }
}
