{{- if .Values.dashboard.enabled -}}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "kubeapps.dashboard-config.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: dashboard
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" . ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
  vhost.conf: |-
    server {
      listen {{ .Values.dashboard.containerPorts.http }};
  {{- if .Values.frontend.largeClientHeaderBuffers }}
      large_client_header_buffers {{ .Values.frontend.largeClientHeaderBuffers }};
  {{- end }}
  {{- if .Values.enableIPv6 }}
      listen [::]:{{ .Values.dashboard.containerPorts.http }};
  {{- end }}
      server_name _;

      gzip on;
      gzip_static  on;

      location /custom_style.css {
        root /app/custom-css/;
      }

      location /custom_locale.json {
        root /app/custom-locale/;
      }

      location /custom_components.js {
        root /app/custom-components/;
      }

      location / {
        # Redirects are required to be relative otherwise the internal hostname will be exposed
        absolute_redirect off;

        # Trailing / is required in the path for the React app to be loaded correctly
        # The rewrite rule adds a trailing "/" to any path that does not contain "." neither "/".
        # i.e kubeapps => kubeapps/
        rewrite ^([^.]*[^/])$ $1/ permanent;

        # Support for ingress prefixes maintaining compatibility with the default /
        # 1 - Exactly two fragment URLs for files existing inside of the public/ dir
        # i.e /[prefix]/config.json => /config.json
        rewrite ^/[^/]+/([^/]+)$ /$1 break;

        # 2 - Any static files bundled by webpack referenced by 3 or more URL segments
        # i.e /[prefix]/static/main.js => static/main.js
        rewrite ^/[^/]+/static/(.*) /static/$1 break;

        try_files $uri /index.html;
      }
    }
  custom_style.css: |-
{{- .Values.dashboard.customStyle | nindent 4 }}
  custom_components.js: |-
{{- .Values.dashboard.customComponents | nindent 4 }}
  custom_locale.json: |-
{{- .Values.dashboard.customLocale | toJson | nindent 4 }}
  config.json: |-
    {
      "kubeappsCluster": {{ include "kubeapps.kubeappsCluster" . | quote }},
      "kubeappsNamespace": {{ .Release.Namespace | quote }},
      "helmGlobalNamespace": {{ include "kubeapps.helmGlobalPackagingNamespace" . | quote }},
      "carvelGlobalNamespace": {{ .Values.kubeappsapis.pluginConfig.kappController.packages.v1alpha1.globalPackagingNamespace | quote }},
      "appVersion": {{ printf "v%s" .Chart.AppVersion | quote }},
      "authProxyEnabled": {{ .Values.authProxy.enabled }},
      "oauthLoginURI": {{ .Values.authProxy.oauthLoginURI | quote }},
      "oauthLogoutURI": {{ .Values.authProxy.oauthLogoutURI | quote }},
      "authProxySkipLoginPage": {{ .Values.authProxy.skipKubeappsLoginPage }},
      "featureFlags": {{ .Values.featureFlags | toJson }},
      "clusters": {{ include "kubeapps.clusterNames" . }},
      "theme": {{ .Values.dashboard.defaultTheme | quote }},
      "remoteComponentsUrl": {{ .Values.dashboard.remoteComponentsUrl | quote }},
      "customAppViews": {{ .Values.dashboard.customAppViews | toJson }},
      "skipAvailablePackageDetails": {{ .Values.dashboard.skipAvailablePackageDetails }},
      "createNamespaceLabels": {{ .Values.dashboard.createNamespaceLabels | toJson }}
    }
{{- end -}}
