<Project>
  <Target Name="AddGlobalAnalyzerConfigForPackage_MicrosoftCodeAnalysisCSharpCodeStyle" BeforeTargets="GenerateMSBuildEditorConfigFileCore;CoreCompile" Condition="'$(SkipGlobalAnalyzerConfigForPackage)' != 'true'">
    <!-- PropertyGroup to compute global analyzer config file to be used -->
    <PropertyGroup>
      <!-- Default 'AnalysisLevelStyle' to the core 'AnalysisLevel' -->
      <AnalysisLevelStyle Condition="'$(AnalysisLevelStyle)' == ''">$(AnalysisLevel)</AnalysisLevelStyle>

      <!-- AnalysisLevelStyle can also contain compound values with a prefix and suffix separated by a '-' character.
           The prefix indicates the core AnalysisLevel for 'Style' rules and the suffix indicates the bucket of
           rules to enable for 'Style' rules by default. For example, some valid compound values for AnalysisLevelStyle are:
             1. '5-all' - Indicates core AnalysisLevelStyle = '5' with 'all' the 'Style' rules enabled by default.
             2. 'latest-none' - Indicates core AnalysisLevelStyle = 'latest' with 'none' of the 'Style' rules enabled by default.
           AnalysisLevelPrefixStyle is used to set the EffectiveAnalysisLevelStyle below.
           AnalysisLevelSuffixStyle is used to map to the correct global config.
      -->
      <AnalysisLevelPrefixStyle Condition="$(AnalysisLevelStyle.Contains('-'))">$([System.Text.RegularExpressions.Regex]::Replace($(AnalysisLevelStyle), '-(.)*', ''))</AnalysisLevelPrefixStyle>
      <AnalysisLevelSuffixStyle Condition="'$(AnalysisLevelPrefixStyle)' != ''">$([System.Text.RegularExpressions.Regex]::Replace($(AnalysisLevelStyle), '$(AnalysisLevelPrefixStyle)-', ''))</AnalysisLevelSuffixStyle>

      <!-- Default 'AnalysisLevelSuffixStyle' to the core 'AnalysisLevelSuffix' -->
      <AnalysisLevelSuffixStyle Condition="'$(AnalysisLevelSuffixStyle)' == ''">$(AnalysisLevelSuffix)</AnalysisLevelSuffixStyle>
      <!-- Default 'AnalysisModeStyle' to the core 'AnalysisMode' -->
      <AnalysisModeStyle Condition="'$(AnalysisModeStyle)' == ''">$(AnalysisMode)</AnalysisModeStyle>

      <!-- EffectiveAnalysisLevelStyle is used to differentiate from user specified strings (such as 'none')
           and an implied numerical option (such as '4') -->
      <EffectiveAnalysisLevelStyle Condition="'$(AnalysisLevelStyle)' == 'none' or '$(AnalysisLevelPrefixStyle)' == 'none'">$(_NoneAnalysisLevel)</EffectiveAnalysisLevelStyle>
      <EffectiveAnalysisLevelStyle Condition="'$(AnalysisLevelStyle)' == 'latest' or '$(AnalysisLevelPrefixStyle)' == 'latest'">$(_LatestAnalysisLevel)</EffectiveAnalysisLevelStyle>
      <EffectiveAnalysisLevelStyle Condition="'$(AnalysisLevelStyle)' == 'preview' or '$(AnalysisLevelPrefixStyle)' == 'preview'">$(_PreviewAnalysisLevel)</EffectiveAnalysisLevelStyle>

      <!-- Set EffectiveAnalysisLevelStyle to the value of AnalysisLevelStyle if it is a version number -->
      <EffectiveAnalysisLevelStyle Condition="'$(EffectiveAnalysisLevelStyle)' == '' And
                                          '$(AnalysisLevelPrefixStyle)' != ''">$(AnalysisLevelPrefixStyle)</EffectiveAnalysisLevelStyle>
      <EffectiveAnalysisLevelStyle Condition="'$(EffectiveAnalysisLevelStyle)' == '' And
                                          '$(AnalysisLevelStyle)' != ''">$(AnalysisLevelStyle)</EffectiveAnalysisLevelStyle>

      <!-- Set the default analysis mode, if not set by the user -->
      <_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle>$(AnalysisModeStyle)</_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle>
      <_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle Condition="'$(_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle)' == ''">$(AnalysisLevelSuffixStyle)</_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle>
      <_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle Condition="'$(_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle)' == 'AllEnabledByDefault'">All</_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle>
      <_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle Condition="'$(_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle)' == 'AllDisabledByDefault'">None</_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle>
      <_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle Condition="'$(_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle)' == ''">Default</_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle>

      <_GlobalAnalyzerConfigFileName_MicrosoftCodeAnalysisCSharpCodeStyle>AnalysisLevelStyle_$(_GlobalAnalyzerConfigAnalysisMode_MicrosoftCodeAnalysisCSharpCodeStyle).globalconfig</_GlobalAnalyzerConfigFileName_MicrosoftCodeAnalysisCSharpCodeStyle>
      <_GlobalAnalyzerConfigFileName_MicrosoftCodeAnalysisCSharpCodeStyle>$(_GlobalAnalyzerConfigFileName_MicrosoftCodeAnalysisCSharpCodeStyle.ToLowerInvariant())</_GlobalAnalyzerConfigFileName_MicrosoftCodeAnalysisCSharpCodeStyle>

      <_GlobalAnalyzerConfigDir_MicrosoftCodeAnalysisCSharpCodeStyle Condition="'$(_GlobalAnalyzerConfigDir_MicrosoftCodeAnalysisCSharpCodeStyle)' == ''">$(MSBuildThisFileDirectory)config</_GlobalAnalyzerConfigDir_MicrosoftCodeAnalysisCSharpCodeStyle>
      <_GlobalAnalyzerConfigFile_MicrosoftCodeAnalysisCSharpCodeStyle Condition="'$(_GlobalAnalyzerConfigFileName_MicrosoftCodeAnalysisCSharpCodeStyle)' != ''">$(_GlobalAnalyzerConfigDir_MicrosoftCodeAnalysisCSharpCodeStyle)\$(_GlobalAnalyzerConfigFileName_MicrosoftCodeAnalysisCSharpCodeStyle)</_GlobalAnalyzerConfigFile_MicrosoftCodeAnalysisCSharpCodeStyle>
    </PropertyGroup>

    <!-- From .NET 10, the global config is systematically added if the file exists. Please check https://github.com/dotnet/roslyn/pull/71173 for more info. -->
    <ItemGroup Condition="Exists('$(_GlobalAnalyzerConfigFile_MicrosoftCodeAnalysisCSharpCodeStyle)') and
                           ('$(AnalysisLevelStyle)' != '$(AnalysisLevel)' or '$(AnalysisModeStyle)' != '$(AnalysisMode)' or ('$(EffectiveAnalysisLevelStyle)' != '' and $([MSBuild]::VersionGreaterThanOrEquals('$(EffectiveAnalysisLevelStyle)', '10.0'))))">
      <EditorConfigFiles Include="$(_GlobalAnalyzerConfigFile_MicrosoftCodeAnalysisCSharpCodeStyle)" />
    </ItemGroup>

    <!-- Pass the MSBuild property values for 'EffectiveAnalysisLevelStyle' and 'EnableCodeStyleSeverity' to the analyzers via analyzer config options. -->
    <ItemGroup>
      <CompilerVisibleProperty Include="EffectiveAnalysisLevelStyle" />
      <CompilerVisibleProperty Include="EnableCodeStyleSeverity" />
    </ItemGroup>
  </Target>

</Project>