<?php
/**
 * Copyright since 2025 InPost S.A.
 *
 * For the full license information, please view the LICENSE file bundled with the module.
 *
 * @author InPost S.A.
 * @copyright since 2025 InPost S.A.
 * @license MIT
 */

declare(strict_types=1);

namespace InPost\International\Checkout\View\Model;

use InPost\International\Delivery\Point\PointInterface;

if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * @implements \ArrayAccess<string, string>
 */
final class SelectedPointView implements \ArrayAccess, \JsonSerializable
{
    /**
     * @var PointInterface
     */
    private $point;

    public function __construct(PointInterface $point)
    {
        $this->point = $point;
    }

    public function offsetExists($offset): bool
    {
        return in_array($offset, [
            'name',
            'formatted_address',
        ]);
    }

    public function offsetGet($offset): string
    {
        switch ($offset) {
            case 'name':
                return $this->point->getId();
            case 'formatted_address':
                return (string) $this->point->getAddress();
            default:
                throw new \OutOfRangeException(sprintf('Undefined index "%s".', $offset));
        }
    }

    public function offsetSet($offset, $value): void
    {
        throw new \BadMethodCallException('Not supported.');
    }

    public function offsetUnset($offset): void
    {
        throw new \BadMethodCallException('Not supported.');
    }

    /**
     * @return array<string, string>
     */
    public function jsonSerialize(): array
    {
        return [
            'name' => $this->point->getId(),
            'formatted_address' => (string) $this->point->getAddress(),
        ];
    }
}
