<?php

namespace Solspace\Freeform\Bundles\Settings;

use Solspace\Freeform\Library\DataObjects\Form\Defaults\ConfigItems\DefaultConfigInterface;
use Solspace\Freeform\Library\DataObjects\Form\Defaults\Defaults;
use Solspace\Freeform\Services\SettingsService;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

class DefaultsProvider
{
    private Defaults $defaults;
    private PropertyAccessor $propertyAccess;

    public function __construct(SettingsService $settingsService)
    {
        $this->defaults = $settingsService->getSettingsModel()->defaults;
        $this->propertyAccess = PropertyAccess::createPropertyAccessor();
    }

    public function getValue(string $path, mixed $default = null): mixed
    {
        try {
            $object = $this->propertyAccess->getValue($this->defaults, $path);
        } catch (NoSuchPropertyException) {
            return $default;
        }

        if ($object instanceof DefaultConfigInterface) {
            return $object->getValue();
        }

        return $object ?? $default;
    }

    public function isLocked(string $path): bool
    {
        try {
            $object = $this->propertyAccess->getValue($this->defaults, $path);
        } catch (NoSuchPropertyException) {
            return false;
        }

        if ($object instanceof DefaultConfigInterface) {
            return $object->isLocked();
        }

        if (\is_bool($object)) {
            return $object;
        }

        return false;
    }
}
