// Code generated by sidekick. DO NOT EDIT.
//
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import GoogleCloudWkt

/// Message describing a result of an aggregation.
public struct AggregationResult: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  public var field: Swift.String = Swift.String()

  public var result: OneOf_Result? = nil

  /// Initialize a new instance of `AggregationResult`.
  public init() {}

  /// Use `config` to return a new instance of this object, with some fields updated.
  ///
  /// Commonly used to initialize the value, for example:
  ///
  /// ```
  /// let value = AggregationResult().with { $0.field = ... }
  /// ```
  public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
    var copy = self
    try config(&copy)
    return copy
  }

  private enum CodingKeys: Swift.String, CodingKey {
    case field = "field"
    case count = "count"
    case sum = "sum"
    case histogram = "histogram"
    case frequency = "frequency"
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.field = try container.decode(Swift.String.self, forKey: .field)

    var result: OneOf_Result? = nil
    let resultCheckAndSet = {
      if result != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'result'"))
      }
      result = $0
    }
    if let count = try container.decodeIfPresent(AggregationResult.Count?.self, forKey: .count) {
      try resultCheckAndSet(.count(count))
    }
    if let sum = try container.decodeIfPresent(AggregationResult.Sum?.self, forKey: .sum) {
      try resultCheckAndSet(.sum(sum))
    }
    if let histogram = try container.decodeIfPresent(
      AggregationResult.Histogram?.self, forKey: .histogram)
    {
      try resultCheckAndSet(.histogram(histogram))
    }
    if let frequency = try container.decodeIfPresent(
      AggregationResult.Frequency?.self, forKey: .frequency)
    {
      try resultCheckAndSet(.frequency(frequency))
    }
    self.result = result
  }

  public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(self.field, forKey: .field)

    if let choice = self.result {
      switch choice {
      case .count(let value):
        try container.encode(value, forKey: .count)
      case .sum(let value):
        try container.encode(value, forKey: .sum)
      case .histogram(let value):
        try container.encode(value, forKey: .histogram)
      case .frequency(let value):
        try container.encode(value, forKey: .frequency)
      }
    }
  }

  /// The result of a count aggregation.
  public struct Count: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    public var value: Swift.Int64 = Swift.Int64()

    /// Initialize a new instance of `Count`.
    public init() {}

    /// Use `config` to return a new instance of this object, with some fields updated.
    ///
    /// Commonly used to initialize the value, for example:
    ///
    /// ```
    /// let value = Count().with { $0.value = ... }
    /// ```
    public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
      var copy = self
      try config(&copy)
      return copy
    }

    public static var _anyTypeUrl: Swift.String {
      return "type.googleapis.com/google.cloud.migrationcenter.v1.AggregationResult.Count"
    }
    public init(fromAny any: GoogleCloudWkt.`Any`) throws {
      self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
    }
    public func _pack() throws -> GoogleCloudWkt.Struct {
      return try GoogleCloudWkt._slowAnySerialize(message: self)
    }
  }

  /// The result of a sum aggregation.
  public struct Sum: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    public var value: Swift.Double = Swift.Double()

    /// Initialize a new instance of `Sum`.
    public init() {}

    /// Use `config` to return a new instance of this object, with some fields updated.
    ///
    /// Commonly used to initialize the value, for example:
    ///
    /// ```
    /// let value = Sum().with { $0.value = ... }
    /// ```
    public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
      var copy = self
      try config(&copy)
      return copy
    }

    public static var _anyTypeUrl: Swift.String {
      return "type.googleapis.com/google.cloud.migrationcenter.v1.AggregationResult.Sum"
    }
    public init(fromAny any: GoogleCloudWkt.`Any`) throws {
      self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
    }
    public func _pack() throws -> GoogleCloudWkt.Struct {
      return try GoogleCloudWkt._slowAnySerialize(message: self)
    }
  }

  /// The result of a bucketed histogram aggregation.
  public struct Histogram: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// Buckets in the histogram.
    /// There will be `n+1` buckets matching `n` lower bounds in the request.
    /// The first bucket will be from -infinity to the first bound.
    /// Subsequent buckets will be between one bound and the next.
    /// The final bucket will be from the final bound to infinity.
    public var buckets: [AggregationResult.Histogram.Bucket] = []

    /// Initialize a new instance of `Histogram`.
    public init() {}

    /// Use `config` to return a new instance of this object, with some fields updated.
    ///
    /// Commonly used to initialize the value, for example:
    ///
    /// ```
    /// let value = Histogram().with { $0.buckets = ... }
    /// ```
    public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
      var copy = self
      try config(&copy)
      return copy
    }

    /// A histogram bucket with a lower and upper bound, and a count of items
    /// with a field value between those bounds.
    /// The lower bound is inclusive and the upper bound is exclusive.
    /// Lower bound may be -infinity and upper bound may be infinity.
    public struct Bucket: Codable, Equatable, GoogleCloudWkt._AnyPackable,
      Sendable
    {
      /// Lower bound - inclusive.
      public var lowerBound: Swift.Double = Swift.Double()

      /// Upper bound - exclusive.
      public var upperBound: Swift.Double = Swift.Double()

      /// Count of items in the bucket.
      public var count: Swift.Int64 = Swift.Int64()

      /// Initialize a new instance of `Bucket`.
      public init() {}

      /// Use `config` to return a new instance of this object, with some fields updated.
      ///
      /// Commonly used to initialize the value, for example:
      ///
      /// ```
      /// let value = Bucket().with { $0.lowerBound = ... }
      /// ```
      public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
        var copy = self
        try config(&copy)
        return copy
      }

      public static var _anyTypeUrl: Swift.String {
        return
          "type.googleapis.com/google.cloud.migrationcenter.v1.AggregationResult.Histogram.Bucket"
      }
      public init(fromAny any: GoogleCloudWkt.`Any`) throws {
        self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
      }
      public func _pack() throws -> GoogleCloudWkt.Struct {
        return try GoogleCloudWkt._slowAnySerialize(message: self)
      }
    }

    public static var _anyTypeUrl: Swift.String {
      return "type.googleapis.com/google.cloud.migrationcenter.v1.AggregationResult.Histogram"
    }
    public init(fromAny any: GoogleCloudWkt.`Any`) throws {
      self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
    }
    public func _pack() throws -> GoogleCloudWkt.Struct {
      return try GoogleCloudWkt._slowAnySerialize(message: self)
    }
  }

  /// The result of a frequency distribution aggregation.
  public struct Frequency: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    public var values: [Swift.String: Swift.Int64] = [:]

    /// Initialize a new instance of `Frequency`.
    public init() {}

    /// Use `config` to return a new instance of this object, with some fields updated.
    ///
    /// Commonly used to initialize the value, for example:
    ///
    /// ```
    /// let value = Frequency().with { $0.values = ... }
    /// ```
    public func with(_ config: (inout Self) throws -> Swift.Void) rethrows -> Self {
      var copy = self
      try config(&copy)
      return copy
    }

    public static var _anyTypeUrl: Swift.String {
      return "type.googleapis.com/google.cloud.migrationcenter.v1.AggregationResult.Frequency"
    }
    public init(fromAny any: GoogleCloudWkt.`Any`) throws {
      self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
    }
    public func _pack() throws -> GoogleCloudWkt.Struct {
      return try GoogleCloudWkt._slowAnySerialize(message: self)
    }
  }

  public enum OneOf_Result: Codable, Equatable, Sendable {
    indirect case count(AggregationResult.Count?)
    indirect case sum(AggregationResult.Sum?)
    indirect case histogram(AggregationResult.Histogram?)
    indirect case frequency(AggregationResult.Frequency?)
  }

  public static var _anyTypeUrl: Swift.String {
    return "type.googleapis.com/google.cloud.migrationcenter.v1.AggregationResult"
  }
  public init(fromAny any: GoogleCloudWkt.`Any`) throws {
    self = try GoogleCloudWkt._slowAnyDeserialize(Self.self, from: any)
  }
  public func _pack() throws -> GoogleCloudWkt.Struct {
    return try GoogleCloudWkt._slowAnySerialize(message: self)
  }
}
