<?php

namespace Src\Modules\JsonObjects\Application;

use Src\Modules\JsonObjects\Interfaces\Application\IDataBuilder;
use Src\Modules\JsonObjects\Interfaces\Infrastructure\IDirsStorage;

class DataBuilder implements IDataBuilder
{

    protected IDirsStorage $dirStorage;

    public function setDirStorage(IDirsStorage $dirStorage): void
    {
        $this->dirStorage = $dirStorage;
    }

    public function build(array $data): array
    {
        $dirData = [];
        if (key_exists('dir-id', $data) && $data['dir-id']) {
            $dirData = $this->dirStorage->getById($data['dir-id']);
            if (!$dirData) {
                $dirData = ['id' => ''];
            }
            $data['dir'] = [
                $data['dir-id'] => $dirData,
            ];
        }
        return $data;
    }

    public function buildDirData(array $requestData): array
    {
        $dsl = [
            'id',
            'matherial_path',
            'path' => ['id']
        ];
        $parentData = $this->dirStorage->getById($requestData['parent-dir'], $dsl);
        if ($parentData) {
            $requestData['parent'] = [
                $parentData['id'] => $parentData,
            ];
        }
        return $requestData;
    }
}
