package assets

import (
	"encoding/json"
	"os"
)

type Manifest map[string]string

func LoadManifest(path string) (Manifest, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return Manifest{
			"app.css": "/dist/app.css",
			"app.js":  "/dist/app.js",
		}, nil
	}

	var manifest Manifest
	if err := json.Unmarshal(data, &manifest); err != nil {
		return nil, err
	}

	return manifest, nil
}

func (m Manifest) CSSPath(name string) string {
	if value, ok := m[name]; ok {
		return value
	}
	return "/dist/" + name
}

func (m Manifest) JSPath(name string) string {
	if value, ok := m[name]; ok {
		return value
	}
	return "/dist/" + name
}
