{{/*
Expand the name of the chart.
*/}}
{{- define "application.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "application.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Return the proper application image name.
.Values.imageRegistry overrides global.imageRegistry for this chart only.
*/}}
{{- define "application.image" -}}
{{- $global := .Values.global | deepCopy -}}
{{- if .Values.imageRegistry -}}{{- $_ := set $global "imageRegistry" .Values.imageRegistry -}}{{- end -}}
{{ include "common.images.image" (dict "imageRoot" .Values.image "global" $global "appVer" .Chart.AppVersion) }}
{{- end -}}

{{/*
Return the proper router image name
*/}}
{{- define "router.image" -}}
{{- $global := .Values.global | deepCopy -}}
{{- if .Values.imageRegistry -}}{{- $_ := set $global "imageRegistry" .Values.imageRegistry -}}{{- end -}}
{{ include "common.images.image" (dict "imageRoot" .Values.router.image "global" $global) }}
{{- end -}}

{{/*
Return the proper observability image name
*/}}
{{- define "observability.image" -}}
{{- $global := .Values.global | deepCopy -}}
{{- if .Values.imageRegistry -}}{{- $_ := set $global "imageRegistry" .Values.imageRegistry -}}{{- end -}}
{{ include "common.images.image" (dict "imageRoot" .Values.observability.image "global" $global) }}
{{- end -}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "application.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "application.ServiceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "application.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

{{/*
Resolve joinKeySecretName value
*/}}
{{- define "application.joinKeySecretName" -}}
{{- coalesce .Values.joinKeySecretName .Values.global.joinKeySecretName (include "application.fullname" .) -}}
{{- end -}}

{{/*
Resolve masterKeySecretName value
*/}}
{{- define "application.masterKeySecretName" -}}
{{- coalesce .Values.masterKeySecretName .Values.global.masterKeySecretName (include "application.fullname" .) -}}
{{- end -}}

{{/*
Scheme (http/https) based on Access TLS enabled/disabled
*/}}
{{- define "application.scheme" -}}
{{- if .Values.router.tlsEnabled -}}
{{- printf "%s" "https" -}}
{{- else -}}
{{- printf "%s" "http" -}}
{{- end -}}
{{- end -}}

{{/*
Calculate the final systemYaml, merging extraSystemYaml (root) and global.extraSystemYaml
(global takes precedence) on top of the base .Values.systemYaml.
*/}}
{{- define "application.finalSystemYaml" -}}
{{- $baseRaw := tpl .Values.systemYaml . -}}
{{- $baseParsed := fromYaml $baseRaw -}}
{{- $rootExtra := .Values.extraSystemYaml | default dict -}}
{{- $globalExtra := ((.Values.global).extraSystemYaml) | default dict -}}
{{- $extra := mergeOverwrite (deepCopy $rootExtra) $globalExtra -}}
{{- if and (kindIs "map" $baseParsed) (kindIs "map" $extra) -}}
{{ mergeOverwrite $baseParsed $extra | toYaml }}
{{- else -}}
{{ $baseRaw }}
{{- end -}}
{{- end -}}
