<?php

declare(strict_types=1);

namespace App\Business\DataImport\Support;

use Illuminate\Validation\ValidationException;

final class DataImportErrorSanitizer
{
    public static function executionFailureMessage(\Throwable $exception): string
    {
        if ($exception instanceof ValidationException) {
            $messages = [];

            foreach ($exception->errors() as $fieldMessages) {
                foreach ($fieldMessages as $message) {
                    $messages[] = (string) $message;
                }
            }

            if ($messages !== []) {
                return implode(' ', array_values(array_unique($messages)));
            }
        }

        return (string) __('dberp.data_import.errors.execution_failed_generic');
    }
}
