<?php

namespace Fifa\ConnectId\Sdk\Exception;

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

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

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

    /**
     * Gets response with duplicates
     *
     * @return GetDuplicatesResponseType
     */
    public function getGetDuplicatesResponse(): GetDuplicatesResponseType
    {
        return $this->duplicatesResponseType;
    }

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

        return $duplicates ?: array();
    }
}