<?php

namespace Modules\API\APIInterne\Magic\Service;

class MatchArrayProperty
{
    /** @var string $document */
    protected string $document;

    /**
     * @param string $document
     */
    public function __construct(string $document)
    {
        $this->document = $document;
    }

    /**
     * @return array<mixed>
     */
    public function getStructure()
    {
        $method = $this->document;

        if (method_exists($this, $this->document)) {
            return $this->$method();
        }

        return [];
    }

    /**
     * @param mixed $data
     * @param string $name
     * @param string $nodeName
     * @return array<mixed>
     */
    public function checkMissingArray($data, string $name, string $nodeName): array
    {
        $correspondance = $this->getStructure();

        $data = json_encode($data);
        $data = json_decode((string)$data, true);

        foreach ($data[$name][$nodeName] as $iRecord => $record) {
            $data[$name][$nodeName][$iRecord] = $this->processRecord($record, $correspondance);
        }

        return $data;
    }

    /**
     * @param array<mixed> $record
     * @param array<mixed> $correspondance
     * @return mixed
     */
    private function processRecord($record, $correspondance)
    {
        foreach ($record as $key => $val) {
            $match = $correspondance[$key] ?? null;

            // Case : empty value which should be a string
            if (empty($val) && $match == null) {
                $record[$key] = '';
                continue;
            }

            if (is_string($val) && $match) {
                $record[$key] = [$val];
            }

            if (is_array($val) && !empty($val)) {
                $record[$key] = $this->processRecord($val, $correspondance);
                // Case : add array depth removed by xml for an single entity list
                // @phpstan-ignore-next-line
                if ($match === 'array' && !array_is_list($val)) {
                    $record[$key] = [$record[$key]];
                }
                continue;
            }
        }
        return $record;
    }

    /**
     * @return array<mixed>
     */
    private function articles()
    {
        return [
            'Langue'                => 'array',
            'CarCompRef'            => 'array',
            'UnitAlternative'       => 'array',
            'SectionImputation'     => 'array',
            'CodeComplementaire'    => 'array',
            'SousCompteAnalyse'     => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    // @phpcs:ignore
    private function br_recep()
    {
        return [
            'Ligne'                 => 'array',
            'Doc_Rattache'          => 'array',
            'Champ_supplementaire'  => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    // @phpcs:ignore
    private function ca_entet()
    {
        return [
            'Champ_supplementaire'  => 'array',
            'Ligne'                 => 'array',
            'Date_planning'         => 'array',
            'CarCompDoc'            => 'array',
            'Clause'                => 'array',
            'Piece'                 => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function cca()
    {
        return [
            'Champ_supplementaire'  => 'array',
            'Ligne' => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function centrana()
    {
        return [
            'Champ_supplementaire'  => 'array'
        ];
    }

    /**
     * @return array<mixed>
     */
    private function codecomp()
    {
        return [
            'Champ_supplementaire'  => 'array'
        ];
    }

    /**
     * @return array<mixed>
     */
    private function codgest()
    {
        return [
            'Champ_supplementaire'  => 'array'
        ];
    }

    /**
     * @return array<mixed>
     */
    private function devises()
    {
        return [
            'Valeur'    => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function directio()
    {
        return [
            'Champ_supplementaire'  => 'array'
        ];
    }

    /**
     * @return array<mixed>
     */
    // @phpcs:ignore
    private function doc_att()
    {
        return [];
    }

    /**
     * @return array<mixed>
     */
    private function engage()
    {
        return [
            'Ligne' => 'array'
        ];
    }

    /**
     * @return array<mixed>
     */
    private function fabrican()
    {
        return [];
    }

    /**
     * @return array<mixed>
     */
    // @phpcs:ignore
    private function fac_ent()
    {
        return [
            'Champ_supplementaire'  => 'array',
            'Piece'                 => 'array',
            'Ligne'                 => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function famille()
    {
        return [
            'Langue'                => 'array',
            'Champ_supplementaire'  => 'array',
            'SectionImputation'     => 'array',
            'CodeComplementaire'    => 'array',
            'SousCompteAnalyse'     => 'array',
            'ListeAcheteurs'        => 'array',
            'CarCompRef'            => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function far()
    {
        return [
            'Champ_supplementaire'  => 'array',
            'Ligne'                 => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function fourninc()
    {
        return [];
    }

    /**
     * @return array<mixed>
     */
    private function fourniss()
    {
        return [
            'Champ_supplementaire'  => 'array',
            'Contact'               => 'array',
            'ZoneGeographique'      =>  'array',
            'CODESFA'               => 'array',
            'MOTCLEF'               => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function fourniss1031af()
    {
        return [
        'Champ_supplementaire'  => 'array',
            'Contact'               => 'array',
            'ZoneGeographique'      =>  'array',
            'CODESFA'               => 'array',
            'MOTCLEF'               => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function gammes()
    {
        return [];
    }

    /**
     * @return array<mixed>
     */
    private function hastutil()
    {
        return [
            'Champ_supplementaire'  => 'array',
            'SocietesSecondaires'   => 'array',
            'Groupe'                => 'array',
            'DomaineFonctionnel'    => 'array',
            'Droit'                 => 'array',
            'Marche'                => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function natana()
    {
        return [
            'Champ_supplementaire'  => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function remgamm()
    {
        return [];
    }

    /**
     * @return array<mixed>
     */
    // @phpcs:ignore
    private function sbe_ente()
    {
        return [
            'Ligne' => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    // @phpcs:ignore
    private function sbs_ente()
    {
        return [
            'Ligne' => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function sectimpu()
    {
        return [
            'Champ_supplementaire'  => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    // @phpcs:ignore
    private function si_uo()
    {
        return [
            'UO'    => 'array'
        ];
    }

    /**
     * @return array<mixed>
     */
    private function smouveme()
    {
        return [];
    }

    /**
     * @return array<mixed>
     */
    private function sscptana()
    {
        return [
            'Champ_supplementaire'  => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function ssfamill()
    {
        return [
            'Langue'                => 'array',
            'Champ_supplementaire'  => 'array',
            'SectionImputation'     =>  'array',
            'CodeComplementaire'    => 'array',
            'SousCompteAnalyse'     =>  'array',
            'ListeAcheteurs'        => 'array',
            'CarCompRef'            => 'array'
        ];
    }

    /**
     * @return array<mixed>
     */
    // @phpcs:ignore
    private function table_cs()
    {
        return [
            'Champ_supplementaire'  => 'array',
        ];
    }

    /**
     * @return array<mixed>
     */
    private function uo()
    {
        return [
            'Champ_supplementaire'  => 'array',
        ];
    }
}
