<?php

namespace Fifa\ConnectId\Sdk\Exception;

use Fifa\ConnectId\Sdk\Api\ApiException;
use Fifa\ConnectId\Sdk\Api\Model\FacilityLocalType;
use Fifa\ConnectId\Sdk\Api\Model\GetFacilityDuplicatesResponseType;
use Fifa\ConnectId\Sdk\Util\DuplicateExceptionMessageFormatter;

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

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

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

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

        return $duplicates ?: array();
    }
}