<?php

/*
 * This file is part of the Fidry\Console package.
 *
 * (c) Théo FIDRY <theo.fidry@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace Fidry\Console\Internal\Generator;

use function Safe\file_get_contents;
use function Safe\file_put_contents;
use function Safe\preg_replace;
use function str_replace;

/**
 * @private
 */
final class GettersDumper
{
    private const TARGET_PATH = __DIR__.'/../../Input/TypedInput.php';

    public static function generate(): void
    {
        $autoGeneratedContent = GettersGenerator::generate(
            TypeMap::provideTypes(),
        );

        $existingContent = file_get_contents(self::TARGET_PATH);

        $newContent = preg_replace(
            '/(__AUTO_GENERATE_START__\n)([\s\S]*?)(\s+\/\/ __AUTO_GENERATE_END__)/m',
            '$1'.$autoGeneratedContent.'$3',
            $existingContent,
        );

        $cleanedNewContent = str_replace("\n\n\n", "\n\n", $newContent);

        file_put_contents(self::TARGET_PATH, $cleanedNewContent);
    }
}
