<?php

namespace Modules\ProjetAchat\Service;

class BibliothequeService
{

    public static function getObjectDataSysteme(string $OBJET_DATA) : string
    {
        $nomRetourTable = $OBJET_DATA;
        
        if ($OBJET_DATA === 'CENTRE_E' || $OBJET_DATA === 'CENTRE_R' || $OBJET_DATA === 'CENTRE_F' || $OBJET_DATA === 'CENTRE_L') {
            $nomRetourTable = 'CENTRE';
        } elseif ($OBJET_DATA == 'ACHETEUR' || $OBJET_DATA == 'DEMANDEU' || $OBJET_DATA == 'SIGDA' || $OBJET_DATA == 'SIGCDE' || $OBJET_DATA == 'SIGBAP') {
            $nomRetourTable = 'HASTUTIL';
        }
        
        return $nomRetourTable;
    }
    
    /**
     * OLD_treeBibliotheque
     * @param string $parent
     * @param array<mixed> $array
     * @param array<mixed> $post
     * @return array<mixed>
     */
    // phpcs:ignore
    public function OLD_treeBibliotheque(string $parent, array $array, array $post) : ?array
    {
        $result = array();
        foreach ($array as $noeud) {
            if ($parent == trim($noeud['ID_PARENT'])) {
                $result[] = array(
                    'id' => $noeud['ID'],
                    'text' => (strlen($noeud['LIB'])>50? '<span title="'.$noeud['LIB'].'">'.substr($noeud['LIB'], 0, 50).'</span>':$noeud['LIB']),
                    'nodes' => $this->OLD_treeBibliotheque($noeud['ID'], $array, $post),
                    'href' => '#'.$noeud['ID'],
                );
            }
        }
        return $result;
    }

    /** @phpstan-ignore-next-line */
    public function treeBibliotheque(string $parent, array $array, array $post, ?string $urlImage = null) : ?array
    {
        $result = array();
        foreach ($array as $noeud) {
            if ($parent == trim($noeud['ID_PARENT'])) {
                $result[] = array(
                    'id' => $noeud['ID'],
                    'text' => (strlen($noeud['LIB'])>50? '<span title="'.$noeud['LIB'].'">'.substr($noeud['LIB'], 0, 50).'</span>':$noeud['LIB']) .($urlImage? '<div class="divMenuContextuel" onClick="AfficheMenu(this);roueDenteActive(this);"><img class="imagePoint" style="float: right;" src="'.$urlImage.'" width="5px" style=""/></div><ul class="ss_parametres"></ul>':''),
                    'children' => $this->treeBibliotheque($noeud['ID'], $array, $post, $urlImage),
                    'li_attr' => [ "data-drop" => $noeud['COUNT_STRUCT']=='0'? 'true':'false' ],
                );
            }
        }
        return $result;
    }
    
    /** @phpstan-ignore-next-line */
    public function treeMaquette(string $parent, array $array, array $post) : ?array
    {
        
        $result = array();
        foreach ($array as $noeud) {
            if ($parent == trim($noeud['ID_PARENT'])) {
                $result[] = array(
                    'id' => $noeud['ID'],
                    'text' => $noeud['LIB'],
                    'nodes' => $this->treeMaquette($noeud['ID'], $array, $post),
                    'href' => '#'.$noeud['ID'],
                    'color' => '#'.$noeud['COLOR'],
                );
            }
        }
        return $result;
    }
}
