import { Component, Input } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatCardModule } from '@angular/material/card';

import { Errors } from 'src/app/core';
@Component({
    selector: 'huapp-list-errors',
    templateUrl: './list-errors.component.html',
    standalone: true,
    imports: [CommonModule, MatCardModule]
})
export class ListErrorsComponent {
    formattedErrors: Array<string | undefined> = [];
    stacktrace?: string;

    @Input()
    set errors(errorList: Errors) {
        if (errorList.errors['stack'] !== undefined) {
            this.stacktrace = errorList.errors['stack'];
            delete errorList.errors['stack'];
        }
        this.formattedErrors = Object.keys(errorList.errors || {})
            .map((key: string) => {
                if (key !== 'stack') {
                    return `${key}: ${errorList.errors[key]}`;
                }
                return undefined;
            });
    }

    get errorList() {
        return this.formattedErrors;
    }


}
