<?php

declare(strict_types=1);

namespace PpPhp\Core\Platform\Catalog\Instances;

use PpPhp\Core\Platform\Catalog\Contracts\CatalogInstanceInterface;
use PpPhp\Core\Platform\Catalog\Definitions\AbstractCatalogDefinition;
use PpPhp\Core\Platform\Identity\PlatformIdentity;

abstract class AbstractCatalogInstance implements CatalogInstanceInterface
{
    protected array $properties = [];

    protected function __construct(
        protected readonly PlatformIdentity $identity,
        protected readonly AbstractCatalogDefinition $definition
    ) {
        $this->properties = $definition->defaults();
    }

    public function identity(): PlatformIdentity
    {
        return $this->identity;
    }

    public function definition(): AbstractCatalogDefinition
    {
        return $this->definition;
    }

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

    public function property(string $name, mixed $default = null): mixed
    {
        return $this->properties[$name] ?? $default;
    }

    public function setProperty(string $name, mixed $value): static
    {
        $this->properties[$name] = $value;

        return $this;
    }
}
