<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>

    <!-- Allow WEBP  -->
    <security>
      <requestFiltering>
        <fileExtensions>
          <remove fileExtension=".webp" />
          <add fileExtension=".webp" allowed="true" />
        </fileExtensions>
      </requestFiltering>
    </security>

    <!-- MIME types -->
    <staticContent>
      <remove fileExtension=".webp" />
      <mimeMap fileExtension=".webp" mimeType="image/webp" />
    </staticContent>

    <!-- Prevent index.html from being cached -->
    <httpProtocol>
      <customHeaders>
        <clear />
      </customHeaders>
    </httpProtocol>

    <rewrite>
      <rules>

        <!-- 1. Skip API -->
        <rule name="Skip API" stopProcessing="true">
          <match url="^api/.*" />
          <action type="None" />
        </rule>

        <!-- 2. Locale redirect -->
        <rule name="LocaleRedirect" stopProcessing="true">
          <match url="^((?!nl/|fr/|es/|pt/|de/|ja/|ru/|ar|zh/).*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTP_COOKIE}" pattern="(?:^|;\s*)locale=(nl|fr|es|pt|de|ja|ru|ar|zh)(?:;|$)" />
          </conditions>
          <action type="Redirect" url="/{C:1}/{R:1}" redirectType="Temporary" />
        </rule>

        <!-- 3. Locale fallback (single generic rule) -->
        <rule name="Locale Fallback" stopProcessing="true">
          <match url="^(nl|fr|es|pt|de|ja|ru|ar|zh)(/.*)?$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_URI}" pattern="\.(js|mjs|css|png|jpg|jpeg|webp|svg|ico|woff|woff2|ttf|json|map)(\?.*)?$" negate="true" />
          </conditions>
          <action type="Rewrite" url="{R:1}/index.html" />
        </rule>

        <!-- 4. Angular fallback -->
        <rule name="Angular Fallback" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.html" />
        </rule>

      </rules>

      <outboundRules>
        <!-- Set no-cache headers on any response that is text/html (i.e. index.html) -->
        <rule name="No Cache HTML" preCondition="IsHTML">
          <match serverVariable="RESPONSE_Cache-Control" pattern=".*" />
          <action type="Rewrite" value="no-store, no-cache, must-revalidate" />
        </rule>
        <preConditions>
          <preCondition name="IsHTML">
            <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
          </preCondition>
        </preConditions>
      </outboundRules>

    </rewrite>

  </system.webServer>
</configuration>