import { ChangeDetectionStrategy, Component } from '@angular/core';
import { DialogContentDirective } from '../dialog-content.directive';
import { NgIcon, provideIcons } from '@ng-icons/core';
import { isaActionClose } from '@isa/icons';
import { logger } from '@isa/core/logging';

/**
 * Input data for the error message dialog
 */
export interface FeedbackErrorMessageDialogData {
  /** The Error message text to display in the dialog */
  errorMessage: string;
}

export type FeedbackErrorDialogData =
  | FeedbackErrorMessageDialogData
  | {
      /** The error to display in the dialog */
      error: unknown;
    };

/**
 * A simple feedback dialog component that displays an error message and an error icon.
 */
@Component({
  selector: 'ui-feedback-error-dialog',
  templateUrl: './feedback-error-dialog.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [NgIcon],
  providers: [provideIcons({ isaActionClose })],
  host: {
    '[class]': '["ui-feedback-error-dialog"]',
  },
})
export class FeedbackErrorDialogComponent extends DialogContentDirective<
  FeedbackErrorDialogData,
  void
> {
  #logger = logger(() => ({
    component: 'FeedbackErrorDialogComponent',
  }));

  get errorMessage() {
    if ('errorMessage' in this.data) {
      return this.data.errorMessage;
    } else if ('error' in this.data) {
      const error = this.data.error;
      if (error instanceof Error) {
        return error.message;
      }
    }
    this.#logger.warn('No valid error message found', () => ({
      data: this.data,
    }));
    return 'Ein unbekannter Fehler ist aufgetreten';
  }
}
