<?php
namespace boru\ai\Compat\LegacyTool;

use boru\ai\Runtime\ToolExecutionContext;
use boru\dot\Dot;

class LegacyToolInvocation
{
    /** @var array */
    protected $arguments = array();

    /** @var ToolExecutionContext|null */
    protected $context;

    public function __construct(array $arguments = array(), ToolExecutionContext $context = null)
    {
        $this->arguments = $arguments;
        $this->context = $context;
    }

    public function get($key = null, $default = null)
    {
        if ($key === null) {
            return $this->arguments;
        }

        return Dot::get($this->arguments, $key, $default);
    }

    public function set($key, $value)
    {
        Dot::set($this->arguments, $key, $value);
        return $this;
    }

    public function arguments($arguments = null)
    {
        if ($arguments !== null) {
            $this->arguments = is_array($arguments) ? $arguments : array();
            return $this;
        }

        return $this->arguments;
    }

    public function context($key = null, $default = null)
    {
        if ($this->context === null) {
            return $key === null ? array() : $default;
        }

        $values = $this->context->values();
        if ($key === null) {
            return $values;
        }

        return Dot::get($values, $key, $default);
    }

    public function executionContext()
    {
        return $this->context;
    }
}
