/*
 * Copyright (C) 2019 Robert Bosch GmbH Copyright (C) 2019 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { Directive, EventEmitter, HostListener, Output } from '@angular/core';

@Directive({
  selector: `[bciClickOutside]`,
})
export class ClickOutsideDirective {
  private localEvent = null;

  @Output() bciClickOutside = new EventEmitter();

  @HostListener('click', ['$event']) selfClick(event: Event) {
    this.localEvent = event;
  }

  @HostListener('document:click', ['$event']) documentClick(event: Event) {
    if (event !== this.localEvent) {
      this.bciClickOutside.emit();
    }
    this.localEvent = null;
  }
}
