<?php

namespace Fifa\ConnectId\Sdk\Exception;

use Fifa\ConnectId\Sdk\Api\ApiException;
use Fifa\ConnectId\Sdk\Api\Model\GetDuplicatesOfRegisteredPersonResponseType;
use Fifa\ConnectId\Sdk\Api\Model\PersonDuplicateType;
use Fifa\ConnectId\Sdk\Api\Model\PersonRegistrationContextType;
use Fifa\ConnectId\Sdk\Util\DuplicateExceptionMessageFormatter;

/**
 * Class FifaDuplicateOfRegisteredPersonFoundException
 * @package Fifa\ConnectId\Sdk\Exception
 */
class FifaDuplicateOfRegisteredPersonFoundException extends FifaConnectIdException
{
    /**
     * @var GetDuplicatesOfRegisteredPersonResponseType
     */
    private $duplicatesResponseType;

    /**
     * Constructor
     *
     * @param ApiException $e
     * @param GetDuplicatesOfRegisteredPersonResponseType $duplicatesResponseType
     */
    public function __construct(ApiException $e, GetDuplicatesOfRegisteredPersonResponseType $duplicatesResponseType)
    {
        parent::__construct($e, DuplicateExceptionMessageFormatter::generate(array_map(
            function (PersonDuplicateType $personDuplicateType) {
                return $personDuplicateType->getPersonFifaId();
            },
            $duplicatesResponseType->getDuplicates()
        )));
        $this->duplicatesResponseType = $duplicatesResponseType;
    }

    /**
     * Gets the duplicates as an array
     *
     * @return PersonDuplicateType[]
     */
    public function getDuplicates(): array
    {
        $duplicates = $this->duplicatesResponseType->getDuplicates();

        return $duplicates ?: array();
    }

    /**
     * Gets person's original registration context
     *
     * @return PersonRegistrationContextType
     */
    public function getPersonRegistrationContext() : PersonRegistrationContextType {
        return $this->duplicatesResponseType->getRequestedPersonRegistrationContext();
    }

    /**
     * Gets original response
     *
     * @return GetDuplicatesOfRegisteredPersonResponseType
     */
    public function getResponse(): GetDuplicatesOfRegisteredPersonResponseType {
        return $this->duplicatesResponseType;
    }
}