package preproc

import (
	"embed"
	"fmt"
	"io/fs"
	"strings"
)

//go:embed lib
var embeddedLib embed.FS

// embeddedLibPath returns the path within the embedded filesystem for a library include.
// For example, "<c64start.c65>" becomes "lib/c64start.c65"
func embeddedLibPath(spec string) (string, error) {
	if !strings.HasPrefix(spec, "<") || !strings.HasSuffix(spec, ">") {
		return "", fmt.Errorf("not an angle-bracket include: %s", spec)
	}
	
	base := strings.TrimSpace(spec[1 : len(spec)-1])
	return "lib/" + base, nil
}

// readEmbeddedFile reads a file from the embedded filesystem.
func readEmbeddedFile(path string) ([]string, error) {
	data, err := fs.ReadFile(embeddedLib, path)
	if err != nil {
		return nil, err
	}
	
	// Split into lines, preserving empty lines
	content := string(data)
	lines := strings.Split(content, "\n")
	
	// If the file ends with newline, the last element will be empty
	// This matches the behavior of strings.Split for disk files
	return lines, nil
}