<?php

namespace Fifa\ConnectId\Sdk\Exception;

use Fifa\ConnectId\Sdk\Api\ApiException;
use Fifa\ConnectId\Sdk\Api\Model\GetOrganisationDuplicatesResponseType;
use Fifa\ConnectId\Sdk\Api\Model\OrganisationLocalType;
use Fifa\ConnectId\Sdk\Util\DuplicateExceptionMessageFormatter;

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

    /**
     * Constructor
     *
     * @param ApiException $e
     * @param GetOrganisationDuplicatesResponseType $duplicatesResponseType
     */
    public function __construct(ApiException $e, GetOrganisationDuplicatesResponseType $duplicatesResponseType)
    {
        parent::__construct($e, DuplicateExceptionMessageFormatter::generate(array_map(
            function (OrganisationLocalType $duplicate) {
                return $duplicate->getOrganisationFifaId();
            },
            $duplicatesResponseType->getDuplicates()
        )));
        $this->duplicatesResponseType = $duplicatesResponseType;
    }

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

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

        return $duplicates ?: array();
    }
}