<?php

namespace Modules\Stock\Service;

use Helpers\Trad\ITrad;
use SimpleXMLElement;

class WebserviceListErrorDataTransformer
{
    /**
     * @param SimpleXMLElement $webserviceOutput
     * @param ITrad $trad
     * @return array<int,array<string,mixed>>
     */
    public static function output(SimpleXMLElement $webserviceOutput, ITrad $trad): array
    {
        $output = [];
        $erreurs = $webserviceOutput->ListeErreur->Erreur;

        if (count($erreurs) > 1) {
            foreach ($erreurs as $erreur) {
                $output[] = self::transformError($erreur, $trad);
            }
        } else {
            $output[] = self::transformError($erreurs, $trad);
        }

        return $output;
    }

    /**
     * @param SimpleXMLElement $erreur
     * @param ITrad $trad
     * @return array<string,string>
     */
    private static function transformError(SimpleXMLElement $erreur, ITrad $trad): array
    {
        return [
            'id' => $erreur->Num,
            'message' => $erreur->Texte,
            'type' => $erreur->Bloquante == 'true' ? $trad->t('Oui'): $trad->t('Non')
        ];
    }
}
