//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftDiagnostics
import SwiftIfConfig
import SwiftSyntax

extension SyntaxProtocol {
  /// Get the warning emission behavior control for the specified diagnostic group
  /// by determining its containing `WarningControlRegion`, if one is present.
  /// Returns the syntactic control for the given diagnostic group, or `nil` if
  /// there is not one.
  /// - Parameters:
  ///   - for diagnosticGroupIdentifier: The identifier of the diagnostic group.
  ///   - configuredRegions: configured regions of the current syntax tree.
  ///   - globalControls: The global controls to consider, specified by the client (compiler)
  ///     representing module-wide diagnostic group emission configuration, for example
  ///     with `-Wwarning` and `-Werror` flags. These controls can be overriden at
  ///     finer-grained scopes with the `@diagnose` attribute.
  public func warningGroupControl(
    for diagnosticGroupIdentifier: DiagnosticGroupIdentifier,
    configuredRegions: ConfiguredRegions,
    globalControls: [(DiagnosticGroupIdentifier, WarningGroupControl)] = [],
    groupInheritanceTree: DiagnosticGroupInheritanceTree? = nil
  ) -> WarningGroupControl? {
    let warningControlRegions = root.warningGroupControlRegionTreeImpl(
      configuredRegions: configuredRegions,
      globalControls: globalControls,
      groupInheritanceTree: groupInheritanceTree,
      containing: self.position
    )
    return warningControlRegions.warningGroupControl(at: self.position, for: diagnosticGroupIdentifier)
  }

  @_spi(ExperimentalLanguageFeatures)
  @available(
    *,
    deprecated,
    message: "Use warningGroupControl(for:configuredRegions:globalControls:groupInheritanceTree:) instead"
  )
  public func warningGroupControl(
    for diagnosticGroupIdentifier: DiagnosticGroupIdentifier,
    globalControls: [(DiagnosticGroupIdentifier, WarningGroupControl)] = [],
    groupInheritanceTree: DiagnosticGroupInheritanceTree? = nil
  ) -> WarningGroupControl? {
    return warningGroupControl(
      for: diagnosticGroupIdentifier,
      configuredRegions: ConfiguredRegions.empty,
      globalControls: globalControls,
      groupInheritanceTree: groupInheritanceTree
    )
  }
}
