<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Build\Builders;

use PpPhp\Core\Designer\Build\Contracts\BuildArtifact;
use PpPhp\Core\Designer\Generation\Contracts\GeneratedApplication;

final class ApplicationBuilder
{
    /** @return array<int, BuildArtifact> */
    public function build(GeneratedApplication $application): array
    {
        $artifacts = [];

        foreach ($application->pages() as $page) {
            $row = $page->toArray();
            $artifacts[] = BuildArtifact::create(
                'page_' . preg_replace('/[^a-z0-9_]+/i', '_', $page->id()),
                'page',
                $page->path(),
                (string) ($row['html'] ?? ''),
                ['pageType' => $page->pageType()]
            );
        }

        $artifacts[] = BuildArtifact::create(
            'app_shell',
            'shell',
            'app-shell.php',
            (string) ($application->toArray()['shell'] ?? ''),
            ['applicationId' => $application->applicationId()]
        );

        return $artifacts;
    }
}
