package tui

import (
	"os"
	"sort"
	"strings"
)

// AutocompleteCandidate represents a directory entry for path autocomplete.
type AutocompleteCandidate struct {
	Name  string
	IsDir bool
}

// ReadDirAutocomplete reads entries from searchDir whose name starts with prefix
// (case-insensitive). Returns candidates sorted by name.
func ReadDirAutocomplete(searchDir, prefix string) ([]AutocompleteCandidate, error) {
	entries, err := os.ReadDir(searchDir)
	if err != nil {
		return nil, err
	}
	prefixLower := strings.ToLower(prefix)
	var results []AutocompleteCandidate
	for _, e := range entries {
		name := e.Name()
		if prefix == "" || strings.HasPrefix(strings.ToLower(name), prefixLower) {
			results = append(results, AutocompleteCandidate{Name: name, IsDir: e.IsDir()})
		}
	}
	sort.Slice(results, func(i, j int) bool { return results[i].Name < results[j].Name })
	return results, nil
}
