{{/* =============================================================================
   Common helpers
   ============================================================================= */}}

{{- define "gitdata.fullname" -}}
{{- .Release.Name -}}
{{- end -}}

{{- define "gitdata.namespace" -}}
{{- .Values.namespace | default .Release.Namespace -}}
{{- end -}}

{{- define "gitdata.image" -}}
{{- $registry := .Values.image.registry -}}
{{- $pullPolicy := .Values.image.pullPolicy -}}
{{- printf "%s/%s:%s" $registry .image.repository .image.tag -}}
{{- end -}}

{{/* Inject image pull policy into sub-chart image dict */}}
{{- define "gitdata.mergeImage" -}}
{{- $merged := dict "pullPolicy" $.Values.image.pullPolicy -}}
{{- $merged = merge $merged .image -}}
{{- printf "%s/%s:%s" $.Values.image.registry $merged.repository $merged.tag -}}
{{- end -}}

{{/* Build a key-value env var list, optionally reading from a Secret */}}
{{- define "gitdata.envFromSecret" -}}
{{- $secretName := .existingSecret -}}
{{- $keys := .secretKeys -}}
{{- $result := list -}}
{{- range $envName, $secretKey := $keys -}}
{{- $item := dict "name" $envName "valueFrom" (dict "secretKeyRef" (dict "name" $secretName "key" $secretKey)) -}}
{{- $result = append $result $item -}}
{{- end -}}
{{- $result | toJson | fromJson -}}
{{- end -}}

{{/* Merge two env lists (extra env over auto-injected) */}}
{{- define "gitdata.mergeEnv" -}}
{{- $auto := .auto -}}
{{- $extra := .extra | default list -}}
{{- $merged := append $auto $extra | toJson | fromJson -}}
{{- $merged | toYaml -}}
{{- end -}}
