import { Component, Input, Output, EventEmitter, ChangeDetectionStrategy } from '@angular/core';
import { CommonModule } from '@angular/common';

export type TextButtonSize = 'small' | 'medium' | 'large';

@Component({
  selector: 'ui-text-button',
  standalone: true,
  imports: [CommonModule],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <button
      [class]="buttonClasses"
      [disabled]="disabled || loading"
      [type]="type"
      (click)="handleClick($event)"
    >
      @if (loading) {
        <div class="text-button-loader">
          <div class="text-button-spinner"></div>
        </div>
      } @else {
        <ng-content></ng-content>
      }
    </button>
  `,
  styleUrl: './text-button.component.scss'
})
export class TextButtonComponent {
  @Input() size: TextButtonSize = 'medium';
  @Input() disabled: boolean = false;
  @Input() loading: boolean = false;
  @Input() type: 'button' | 'submit' | 'reset' = 'button';
  @Input() class: string = '';

  @Output() clicked = new EventEmitter<Event>();

  get buttonClasses(): string {
    return [
      'ui-text-button',
      `ui-text-button--${this.size}`,
      this.disabled ? 'ui-text-button--disabled' : '',
      this.loading ? 'ui-text-button--loading' : '',
      this.class
    ].filter(Boolean).join(' ');
  }

  handleClick(event: Event): void {
    if (!this.disabled && !this.loading) {
      this.clicked.emit(event);
    }
  }
}