package glob

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"github.com/bmatcuk/doublestar/v3"
)

func GlobFiles(pattern string) ([]string, error) {
	cwd, err := os.Getwd()
	if err != nil {
		return nil, fmt.Errorf("failed to get current working directory: %w", err)
	}

	matches, err := doublestar.Glob(filepath.Join(cwd, pattern))
	if err != nil {
		return nil, fmt.Errorf("failed to match pattern: %w", err)
	}

	for i, match := range matches {
		matches[i] = strings.TrimPrefix(match, cwd)
	}

	for i, match := range matches {
		matches[i] = strings.TrimPrefix(match, "/")
	}

	return matches, nil
}
