package segments

import "github.com/jandedobbeleer/oh-my-posh/src/segments/options"

type Bazel struct {
	Icon string
	Language
}

const (
	Icon options.Option = "icon"
)

func (b *Bazel) Template() string {
	return " {{ if .Error }}{{ .Icon }} {{ .Error }}{{ else }}{{ url .Icon .URL }} {{ .Full }}{{ end }} "
}

const bazelToolName = "bazel"

func (b *Bazel) Enabled() bool {
	b.extensions = []string{"*.bazel", "*.bzl", "BUILD", "WORKSPACE", ".bazelrc", ".bazelversion"}
	b.folders = []string{"bazel-bin", "bazel-out", "bazel-testlogs"}
	b.tooling = map[string]*cmd{
		bazelToolName: {
			executable: bazelToolName,
			args:       []string{versionFlagArg},
			regex:      `bazel ` + versionRegex,
		},
	}
	b.defaultTooling = []string{bazelToolName}
	// Use the correct URL for Bazel >5.4.1, since they do not have the docs subdomain.
	b.versionURLTemplate = "https://{{ if lt .Major 6 }}docs.{{ end }}bazel.build/versions/{{ .Major }}.{{ .Minor }}.{{ .Patch }}"

	b.Icon = b.options.String(Icon, "\ue63a")

	return b.Language.Enabled()
}
