<?php

namespace Livewire\Blaze;

use Closure;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\File;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\ComponentSlot;
use Livewire\Blaze\Exceptions\InvalidBlazeFoldUsageException;
use Livewire\Blaze\Parser\Attribute;
use Livewire\Blaze\Parser\Nodes\ComponentNode;
use Livewire\Blaze\Parser\Nodes\SlotNode;
use Livewire\Blaze\Runtime\BlazeRuntime;
use Livewire\Blaze\Support\Utils;
use ReflectionClass;
use ReflectionFunction;

/**
 * Handles isolated Blade rendering used during compile-time folding.
 */
class BladeRenderer
{
    public function __construct(
        protected BladeCompiler $blade,
        protected Factory $factory,
        protected BlazeRuntime $runtime,
        protected BlazeManager $manager,
    ) {}

    /**
     * Get the temporary cache directory path used during isolated rendering.
     */
    public function getTemporaryCachePath(): string
    {
        return config('view.compiled').'/blaze';
    }

    /**
     * Render a Blade template string in isolation by freezing and restoring compiler state.
     */
    public function render(ComponentNode $component, string $path): string
    {
        $temporaryCachePath = $this->getTemporaryCachePath();

        File::ensureDirectoryExists($temporaryCachePath);

        $restoreFactory = $this->freezeObjectProperties($this->factory, [
            'renderCount' => 0,
            'renderedOnce' => [],
            'sections' => [],
            'sectionStack' => [],
            'pushes' => [],
            'prepends' => [],
            'pushStack' => [],
            'componentStack' => [],
            'componentData' => [],
            'currentComponentData' => [],
            'slots' => [],
            'slotStack' => [],
            'fragments' => [],
            'fragmentStack' => [],
            'loopsStack' => [],
            'translationReplacements' => [],
        ]);

        $restoreCompiler = $this->freezeObjectProperties($this->blade, [
            'cachePath' => $temporaryCachePath,
            'rawBlocks' => [],
            'footer' => [],
            'prepareStringsForCompilationUsing' => [
                function ($input) {
                    if (Unblaze::hasUnblaze($input)) {
                        $input = Unblaze::processUnblazeDirectives($input);
                    };

                    $input = $this->manager->compileForFolding($input, $this->blade->getPath());

                    return $input;
                },
            ],
            'precompilers' => fn (array $precompilers) => [
                ...$this->withoutLivewirePrecompilers($precompilers),
                function (string $input) use ($path) {
                    if (preg_match('~<\s*livewire[-:]|(?<![@\w])@livewire\b(?=\s*\()~', $input)) {
                        throw InvalidBlazeFoldUsageException::forLivewire($path);
                    }

                    return $input;
                }
             ],
            'path' => null,
            'forElseCounter' => 0,
            'firstCaseInSwitch' => true,
            'lastSection' => null,
            'lastFragment' => null,
        ]);

        $restoreRuntime = $this->freezeObjectProperties($this->runtime, [
            'compiledPath' => $temporaryCachePath,
            'dataStack' => [],
            'slotsStack' => [],
            'folding' => true,
        ]);

        $obLevel = ob_get_level();
        $hash = Utils::hash($path);
        $compiled = $temporaryCachePath . '/' . $hash . '.php';
        $fn = '__' . $hash;

        $this->manager->startFolding();

        try {
            if (! file_exists($compiled) || filemtime($path) >= filemtime($compiled)) {
                $this->blade->compile($path);
            }

            $awareData = Arr::mapWithKeys($component->parentsAttributes, function (Attribute $attribute) {
                return [$attribute->name => $attribute->getStaticValue()];
            });

            $attributes = Arr::mapWithKeys($component->attributes, function (Attribute $attribute) {
                return [$attribute->name => $attribute->getStaticValue()];
            });
            
            $slots = Arr::mapWithKeys($component->children, function (SlotNode $slot) {
                return [$slot->name => new ComponentSlot($slot->content())];
            });

            $this->runtime->pushData($awareData);
            $this->runtime->pushData($attributes);
            $this->runtime->pushSlots($slots);

            ob_start();

            require_once $compiled;

            $fn(
                __blaze: $this->runtime,
                __data: $attributes,
                __slots: $slots,
            );

            $result = ltrim(ob_get_clean());
        } finally {
            while (ob_get_level() > $obLevel) {
                ob_end_clean();
            }

            $this->runtime->popData();
            $this->runtime->popData();
            $this->manager->stopFolding();

            $restoreCompiler();
            $restoreFactory();
            $restoreRuntime();
        }

        $result = Unblaze::replaceUnblazePrecompiledDirectives($result);

        return $result;
    }

    /**
     * Delete the temporary cache directory created during isolated rendering.
     */
    public function deleteTemporaryCacheDirectory(): void
    {
        File::deleteDirectory($this->getTemporaryCachePath());
    }

    /**
     * Snapshot object properties and return a restore closure to revert them.
     */
    protected function freezeObjectProperties(object|string $object, array $properties)
    {
        $reflection = new ReflectionClass($object);

        $frozen = [];

        foreach ($properties as $key => $value) {
            $name = is_numeric($key) ? $value : $key;

            $property = $reflection->getProperty($name);

            $currentValue = $property->getValue(is_object($object) ? $object : null);

            $frozen[$name] = $currentValue;

            if (! is_numeric($key)) {
                $property->setValue($object, $value instanceof Closure ? $value($currentValue) : $value);
            }
        }

        return function () use ($reflection, $object, $frozen) {
            foreach ($frozen as $name => $value) {
                $property = $reflection->getProperty($name);
                $property->setValue($object, $value);
            }
        };
    }

    /**
     * Filter out Livewire-specific precompilers.
     */
    protected function withoutLivewirePrecompilers(array $precompilers): array
    {
        if (! class_exists(\Livewire\Livewire::class)) {
            return $precompilers;
        }

        $livewireOnlyPrecompilers = class_exists(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::class)
            ? \Livewire\invade(app(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::class))->precompilers
            : [];

        return Arr::where($precompilers, function ($precompiler) use ($livewireOnlyPrecompilers) {
            if ($precompiler instanceof \Livewire\Mechanisms\CompileLivewireTags\LivewireTagPrecompiler) {
                return false;
            }

            if (in_array($precompiler, $livewireOnlyPrecompilers, true)) {
                return false;
            }

            if ($precompiler instanceof Closure && in_array((new ReflectionFunction($precompiler))->getClosureScopeClass()?->getName(), [
                \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::class,
                \Livewire\Features\SupportMorphAwareBladeCompilation\SupportMorphAwareBladeCompilation::class,
                \Livewire\Mechanisms\ExtendBlade\ExtendBlade::class,
                \Livewire\LivewireServiceProvider::class,
            ], true)) {
                return false;
            }

            return true;
        });
    }
}
