{{- range $key,$val := .Values.applications }}
{{- if (hasKey $val "enabled" | ternary $val.enabled $.Values.default.app.enabled) }}
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: {{ $key }}
{{- if or $val.labels $.Values.default.app.labels }}
{{- $appLabels := $val.labels }}
{{- $defaultLabels := $.Values.default.app.labels }}
{{- $labels := merge $appLabels $defaultLabels }}
  labels:
{{- with $labels }}
{{ toYaml . | indent 4 }}
{{- end }}
{{- end }}

{{- if or $val.annotations $.Values.default.app.annotations }}
{{- $appAnnotations := $val.annotations }}
{{- $defaultAnnotations := $.Values.default.app.annotations }}
{{- $annotations := merge $appAnnotations $defaultAnnotations }}
  annotations:
{{- with $annotations }}
{{ toYaml . | indent 4 }}
{{- end }}
{{- end }}
  finalizers:
    - resources-finalizer.argocd.argoproj.io
spec:
  destination:
    namespace: {{ (($val.destination).namespace) | default $.Values.default.app.destination.namespace | required (printf "No destination.namespace specified for %s" $key ) }}
    {{- $name := (($val.destination).name) | default $.Values.default.app.destination.name }}
    {{- $server := ($val.destination).server | default $.Values.default.app.destination.server }}
    {{- if $name }}
    name: {{ $name }}
    {{- else if $server }}
    server: {{ $server }}
    {{- else}}
    {{- required (printf "No destination.server or destination.name specified for %s" $key ) }}
    {{- end}}
{{- if $.Values.projectOverride }}
  project: {{ $.Values.projectOverride }}
{{- else }}
  project: {{ $val.project | default $.Values.default.app.project | required (printf "No project specified for %s" $key ) }}
{{- end }}
{{- if (hasKey $val "enableAutoSync" | ternary $val.enableAutoSync $.Values.default.app.enableAutoSync) }}
  syncPolicy:
    automated:
      selfHeal: true
      {{- if (hasKey $val "autoSyncPrune" | ternary $val.autoSyncPrune $.Values.default.app.autoSyncPrune) }}
      prune: true
      {{- end }}
    {{- if hasKey $val "syncPolicyRetry" }}
    retry: {{- toYaml $val.syncPolicyRetry | nindent 6 }}
    {{- end }}
    {{- if hasKey $val "syncOptions" }}
    syncOptions:
      {{- with $val.syncOptions }}
      {{ toYaml . }}
      {{- end }}
    {{- end}}
{{- end }}
{{- if (hasKey $val "sources") }}
  sources:
{{- range $source := $val.sources }}
    - {{- if $source.chart }}
      repoURL: {{ $source.repoURL | default $.Values.default.app.source.repoURL | required (printf "No source.repoURL specified for %s" $key ) }}
      chart: {{ $source.chart | required (printf "No chart specified for %s" $key ) }}
      targetRevision: {{ $source.targetRevision | default $.Values.default.app.source.targetRevision | required (printf "No targetRevision specified for %s" $key ) }}
      {{- if $source.helmValues }}
      helm:
        {{- if $source.helmValues }}
        valueFiles:
        {{- range $source.helmValues }}
          - {{ . }}
        {{- end }}
        {{- end }}
        {{- if $source.values }}
        values: |
{{ $source.values | indent 10 }}
        {{- end }}
      {{- end }}
      {{- else }}
      path: {{ tpl $source.path $ }}
      repoURL: {{ $source.repoURL | default $.Values.default.app.source.repoURL | required (printf "No source.repoURL specified for %s" $key ) }}
      targetRevision: {{ $source.targetRevision | default $.Values.default.app.source.targetRevision | required (printf "No targetRevision specified for %s" $key ) }}
      {{- end }}
      {{- if $source.extraSourceFields }}
      {{ tpl $source.extraSourceFields $ | nindent 6 }}
      {{- end }}
{{- end }}
{{- else }}
  source:
    {{- if $val.source.chart }}
    repoURL: {{ $val.source.repoURL | default $.Values.default.app.source.repoURL | required (printf "No source.repoURL specified for %s" $key ) }}
    chart: {{ $val.source.chart | required (printf "No chart specified for %s" $key ) }}
    targetRevision: {{ $val.source.targetRevision | default $.Values.default.app.source.targetRevision | required (printf "No targetRevision specified for %s" $key ) }}
    {{- if $val.source.helmValues }}
    helm:
      {{- if $val.source.helmValues }}
      valueFiles:
      {{- range $val.source.helmValues }}
        - {{ . }}
      {{- end }}
      {{- end }}
      {{- if $val.source.values }}
      values: |
{{ $val.source.values | indent 8 }}
      {{- end }}
    {{- end }}
    {{- else }}
    path: {{ tpl $val.source.path $ }}
    repoURL: {{ $val.source.repoURL | default $.Values.default.app.source.repoURL | required (printf "No source.repoURL specified for %s" $key ) }}
    targetRevision: {{ $val.source.targetRevision | default $.Values.default.app.source.targetRevision | required (printf "No targetRevision specified for %s" $key ) }}
    {{- end }}
{{- if ($val.source | default dict ).extraSourceFields }}
{{ tpl $val.source.extraSourceFields $ | indent 4 }}
{{- else if $.Values.default.app.source.extraSourceFields }}
{{ tpl $.Values.default.app.source.extraSourceFields $ | indent 4 }}
{{- end }}
{{- end }}
{{- if $val.extraFields }}
{{ tpl $val.extraFields $ | indent 2 }}
{{- else if $.Values.default.app.extraFields }}
{{ tpl $.Values.default.app.extraFields $ | indent 2 }}
{{- end }}
{{- end }}
{{- end }}
