<?php

namespace Lucent;

use ArrayAccess;
use JsonSerializable;

/**
 * @implements ArrayAccess<string,mixed>
 */
class ArrayContainer implements ArrayAccess, JsonSerializable
{
    public function __construct(public array $data)
    {
    }

    public function get(string $key, mixed $default = null): mixed
    {
        return $this->data[$key] ?? $default;
    }

    public function offsetSet($offset, $value): void
    {
        if (is_null($offset)) {
            $this->data[] = $value;
        } else {
            $this->data[$offset] = $value;
        }
    }

    public function offsetExists($offset): bool
    {
        return isset($this->data[$offset]);
    }

    public function offsetUnset($offset): void
    {
        unset($this->data[$offset]);
    }

    public function offsetGet($offset): mixed
    {
        return $this->data[$offset] ?? null;
    }

    public function jsonSerialize(): array
    {
        return $this->data;
    }

    public function toArray(): array
    {
        return $this->data;
    }
}
