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

/// A [Digest][google.cloud.kms.v1.Digest] holds a cryptographic message digest.
///
/// [google.cloud.kms.v1.Digest]: <doc:Digest>
public struct Digest: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// Required. The message digest.
  public var digest: OneOf_Digest? = nil

  /// Initialize a new instance of `Digest`.
  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 = Digest().with { $0.sha256 = ... }
  /// ```
  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 sha256 = "sha256"
    case sha384 = "sha384"
    case sha512 = "sha512"
    case externalMu = "externalMu"
  }

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

    var digest: OneOf_Digest? = nil
    let digestCheckAndSet = {
      if digest != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'digest'"))
      }
      digest = $0
    }
    if let sha256 = try container.decodeIfPresent(Foundation.Data.self, forKey: .sha256) {
      try digestCheckAndSet(.sha256(sha256))
    }
    if let sha384 = try container.decodeIfPresent(Foundation.Data.self, forKey: .sha384) {
      try digestCheckAndSet(.sha384(sha384))
    }
    if let sha512 = try container.decodeIfPresent(Foundation.Data.self, forKey: .sha512) {
      try digestCheckAndSet(.sha512(sha512))
    }
    if let externalMu = try container.decodeIfPresent(Foundation.Data.self, forKey: .externalMu) {
      try digestCheckAndSet(.externalMu(externalMu))
    }
    self.digest = digest
  }

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

    if let choice = self.digest {
      switch choice {
      case .sha256(let value):
        try container.encode(value, forKey: .sha256)
      case .sha384(let value):
        try container.encode(value, forKey: .sha384)
      case .sha512(let value):
        try container.encode(value, forKey: .sha512)
      case .externalMu(let value):
        try container.encode(value, forKey: .externalMu)
      }
    }
  }

  /// Required. The message digest.
  public enum OneOf_Digest: Codable, Equatable, Sendable {
    /// A message digest produced with the SHA-256 algorithm.
    case sha256(Foundation.Data)
    /// A message digest produced with the SHA-384 algorithm.
    case sha384(Foundation.Data)
    /// A message digest produced with the SHA-512 algorithm.
    case sha512(Foundation.Data)
    /// A message digest produced with SHAKE-256, to be used with ML-DSA
    /// external-μ algorithms only. See "message representative" note in
    /// section 6.2, algorithm 7 of the FIPS-204 standard:
    /// https://doi.org/10.6028/nist.fips.204
    case externalMu(Foundation.Data)
  }

  public static var _anyTypeUrl: Swift.String {
    return "type.googleapis.com/google.cloud.kms.v1.Digest"
  }
  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)
  }
}
