// 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

/// Rule for determining which cells to delete during garbage collection.
public struct GcRule: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// Garbage collection rules.
  public var rule: OneOf_Rule? = nil

  /// Initialize a new instance of `GcRule`.
  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 = GcRule().with { $0.maxNumVersions = ... }
  /// ```
  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 maxNumVersions = "maxNumVersions"
    case maxAge = "maxAge"
    case intersection = "intersection"
    case union = "union"
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)

    var rule: OneOf_Rule? = nil
    let ruleCheckAndSet = {
      if rule != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath, debugDescription: "Multiple values set for oneof 'rule'"
          ))
      }
      rule = $0
    }
    if let maxNumVersions = try container.decodeIfPresent(Swift.Int32.self, forKey: .maxNumVersions)
    {
      try ruleCheckAndSet(.maxNumVersions(maxNumVersions))
    }
    if let maxAge = try container.decodeIfPresent(GoogleCloudWkt.Duration?.self, forKey: .maxAge) {
      try ruleCheckAndSet(.maxAge(maxAge))
    }
    if let intersection = try container.decodeIfPresent(
      GcRule.Intersection?.self, forKey: .intersection)
    {
      try ruleCheckAndSet(.intersection(intersection))
    }
    if let union = try container.decodeIfPresent(GcRule.Union?.self, forKey: .union) {
      try ruleCheckAndSet(.union(union))
    }
    self.rule = rule
  }

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

    if let choice = self.rule {
      switch choice {
      case .maxNumVersions(let value):
        try container.encode(value, forKey: .maxNumVersions)
      case .maxAge(let value):
        try container.encode(value, forKey: .maxAge)
      case .intersection(let value):
        try container.encode(value, forKey: .intersection)
      case .union(let value):
        try container.encode(value, forKey: .union)
      }
    }
  }

  /// A GcRule which deletes cells matching all of the given rules.
  public struct Intersection: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// Only delete cells which would be deleted by every element of `rules`.
    public var rules: [GcRule] = []

    /// Initialize a new instance of `Intersection`.
    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 = Intersection().with { $0.rules = ... }
    /// ```
    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.bigtable.admin.v2.GcRule.Intersection"
    }
    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)
    }
  }

  /// A GcRule which deletes cells matching any of the given rules.
  public struct Union: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// Delete cells which would be deleted by any element of `rules`.
    public var rules: [GcRule] = []

    /// Initialize a new instance of `Union`.
    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 = Union().with { $0.rules = ... }
    /// ```
    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.bigtable.admin.v2.GcRule.Union"
    }
    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)
    }
  }

  /// Garbage collection rules.
  public enum OneOf_Rule: Codable, Equatable, Sendable {
    /// Delete all cells in a column except the most recent N.
    case maxNumVersions(Swift.Int32)
    /// Delete cells in a column older than the given age.
    /// Values must be at least one millisecond, and will be truncated to
    /// microsecond granularity.
    indirect case maxAge(GoogleCloudWkt.Duration?)
    /// Delete cells that would be deleted by every nested rule.
    indirect case intersection(GcRule.Intersection?)
    /// Delete cells that would be deleted by any nested rule.
    indirect case union(GcRule.Union?)
  }

  public static var _anyTypeUrl: Swift.String {
    return "type.googleapis.com/google.bigtable.admin.v2.GcRule"
  }
  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)
  }
}
