<?php

namespace Blazemedia\Affiliation\Rendering;

class RenderResponse
{
    protected array $response;
    public array $request;
    
    public function __construct(protected string $responseBody = '{}', array $request = [])
    {        
        $this->request = $request;
        $this->responseBody = $responseBody;

        $response = empty($responseBody) ? [] : json_decode($responseBody, true);
        
        $this->response = !is_array($response) ? [] : $response;
    }

    public function isEmpty(): bool
    {
        /// perché? la parte con count sembra inutile
        return empty($this->response) && empty(json_decode($this->responseBody, true ));
    }

    public function getResponseBody(): string
    {
        return $this->responseBody;
    }

    public function getResponse(): array
    {
        return $this->response;
    }

    public function toArray(): array
    {
        return $this->response;
    }

    public function getStyles(): string
    {
        return '';
    }

    public function getReplacements(): array
    {
        return isset($this->response['replacements']) ? $this->response['replacements'] : [];
    }
    
    public function getBMPostMarkup(): string
    {
        return isset($this->response['bm_post']) ? $this->response['bm_post'] : '';
    }

    public function getRequest(): array
    {
        if (empty($this->request)) {
            return [];
        }

        if (!isset($this->request['options'])) {
            $this->request['options'] = [
                'X-API-Key' => '',
                'Source'    => '',
            ];
        }

        if (!isset($this->request['body'])) {
            $this->request['body'] = [];
        }

        if (!isset($this->request['parameters'])) {
            $this->request['parameters'] = [];
        }

        return $this->request;
    }

}
