<?php

namespace kernel;

class Assets
{
    protected array $jsHeader = [];
    protected array $jsBody = [];

    protected array $css = [];

    protected string $resourceURI = "/resource";

    public function __construct(string $resourceURI)
    {
        $this->setResourceURI($resourceURI);
        $this->createCSS();
        $this->createJS();
    }

    protected function createCSS(){}
    protected function createJS(){}

    public function setResourceURI(string $resourceURI): void
    {
        $this->resourceURI = $resourceURI;
    }

    public function registerJS(string $slug, string $resource, bool $body = true, bool $addResourceURI = true): void
    {
        $resource = $addResourceURI ? $this->resourceURI . $resource : $resource;
        if ($body) {
            $this->jsBody[$slug] = $resource;
        } else {
            $this->jsHeader[$slug] = $resource;
        }
    }

    public function registerCSS(string $slug, string $resource, bool $addResourceURI = true): void
    {
        $resource = $addResourceURI ? $this->resourceURI . $resource : $resource;
        $this->css[$slug] = $resource;
    }

    public function getJSAsStr(bool $body = true): void
    {
        if ($body) {
            foreach ($this->jsBody as $key => $item){
                echo "<script src='$item'></script>";
            }
        }
        else {
            foreach ($this->jsHeader as $key => $item){
                echo "<script src='$item'></script>";
            }
        }
    }

    public function getCSSAsSTR(): void
    {
        foreach ($this->css as $key => $item){
            echo "<link rel='stylesheet' href='$item'>";
        }
    }

}