<?php

namespace IpagWoocommerce;

abstract class AbstractTransactionParser extends AbstractEndpointParser
{
    protected SerializerInterface $serializer;

    public function __construct()
    {
        $this->serializer = new XmlSerializer();
        $this->requestTransformer = new TransactionRequestTransformer();
        $this->responseTransformer = new TransactionResponseTransformer();
    }

    public function parseRequest(array $request): array
    {
        $this->injectLatestAPIHeaders($request);

        $transformed = $this->requestTransformer->transform($request);
        return $transformed;
    }

    public function parseResponse($response): array
    {
        $transformed = $this->responseTransformer->transform($response);

        $body = ArrUtils::get($transformed, 'body', []);
        $transformed['body'] = $this->serializer->serialize($body);

        $transformed['headers']['Content-Type'] = $this->serializer->getContentType();

        return $transformed;
    }
}