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

import { AfterViewInit, Component, ContentChild, ElementRef, forwardRef, Input } from '@angular/core';
import { MatFormField } from '@angular/material/form-field';
import { CommandbarItemComponent } from '../item/commandbar-item.component';

@Component({
  selector: 'bci-commandbar-inlinefilter',
  templateUrl: './commandbar-inlinefilter.component.html',
  styleUrls: ['./commandbar-inlinefilter.component.scss'],
  providers: [{ provide: CommandbarItemComponent, useExisting: forwardRef(() => CommandbarInlinefilterComponent) }],
})
export class CommandbarInlinefilterComponent extends CommandbarItemComponent implements AfterViewInit {
  @ContentChild(MatFormField, { read: ElementRef, static: true }) matFormFieldRef: ElementRef;

  @Input() disableDefaultStyle = false;

  @Input() label: string;

  /**
   * Whether the button is styled as active or not
   */
  @Input() active = false;

  ngAfterViewInit() {
    if (!this.label && this.matFormFieldRef) {
      const matFormFieldLabel = this.matFormFieldRef.nativeElement.querySelector('.mat-mdc-floating-label');
      if (matFormFieldLabel) {
        this.label = matFormFieldLabel.innerText;
      }
    }
  }
}
