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

/// Defines a certificate map entry.
public struct CertificateMapEntry: Codable, Equatable, GoogleCloudWkt._AnyPackable,
  Sendable
{
  /// A user-defined name of the Certificate Map Entry. Certificate Map Entry
  /// names must be unique globally and match pattern
  /// `projects/*/locations/*/certificateMaps/*/certificateMapEntries/*`.
  public var name: Swift.String = Swift.String()

  /// One or more paragraphs of text description of a certificate map entry.
  public var description: Swift.String = Swift.String()

  /// Output only. The creation timestamp of a Certificate Map Entry.
  public var createTime: GoogleCloudWkt.Timestamp? = nil

  /// Output only. The update timestamp of a Certificate Map Entry.
  public var updateTime: GoogleCloudWkt.Timestamp? = nil

  /// Set of labels associated with a Certificate Map Entry.
  public var labels: [Swift.String: Swift.String] = [:]

  /// A set of Certificates defines for the given `hostname`. There can be
  /// defined up to four certificates in each Certificate Map Entry. Each
  /// certificate must match pattern `projects/*/locations/*/certificates/*`.
  public var certificates: [Swift.String] = []

  /// Output only. A serving state of this Certificate Map Entry.
  public var state: ServingState = ServingState()

  public var match: OneOf_Match? = nil

  /// Initialize a new instance of `CertificateMapEntry`.
  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 = CertificateMapEntry().with { $0.name = ... }
  /// ```
  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 name = "name"
    case description = "description"
    case createTime = "createTime"
    case updateTime = "updateTime"
    case labels = "labels"
    case hostname = "hostname"
    case matcher = "matcher"
    case certificates = "certificates"
    case state = "state"
  }

  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.name = try container.decode(Swift.String.self, forKey: .name)
    self.description = try container.decode(Swift.String.self, forKey: .description)
    self.createTime = try container.decodeIfPresent(
      GoogleCloudWkt.Timestamp.self, forKey: .createTime)
    self.updateTime = try container.decodeIfPresent(
      GoogleCloudWkt.Timestamp.self, forKey: .updateTime)
    self.labels = try container.decode([Swift.String: Swift.String].self, forKey: .labels)
    self.certificates = try container.decode([Swift.String].self, forKey: .certificates)
    self.state = try container.decode(ServingState.self, forKey: .state)

    var match: OneOf_Match? = nil
    let matchCheckAndSet = {
      if match != nil {
        throw DecodingError.dataCorrupted(
          DecodingError.Context(
            codingPath: decoder.codingPath,
            debugDescription: "Multiple values set for oneof 'match'"))
      }
      match = $0
    }
    if let hostname = try container.decodeIfPresent(Swift.String.self, forKey: .hostname) {
      try matchCheckAndSet(.hostname(hostname))
    }
    if let matcher = try container.decodeIfPresent(
      CertificateMapEntry.Matcher.self, forKey: .matcher)
    {
      try matchCheckAndSet(.matcher(matcher))
    }
    self.match = match
  }

  public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(self.name, forKey: .name)
    try container.encode(self.description, forKey: .description)
    try container.encode(self.createTime, forKey: .createTime)
    try container.encode(self.updateTime, forKey: .updateTime)
    try container.encode(self.labels, forKey: .labels)
    try container.encode(self.certificates, forKey: .certificates)
    try container.encode(self.state, forKey: .state)

    if let choice = self.match {
      switch choice {
      case .hostname(let value):
        try container.encode(value, forKey: .hostname)
      case .matcher(let value):
        try container.encode(value, forKey: .matcher)
      }
    }
  }

  /// Defines predefined cases other than SNI-hostname match when this
  /// configuration should be applied.
  public enum Matcher: Codable, Equatable, Sendable {
    /// A matcher has't been recognized.
    case unspecified
    /// A primary certificate that is served when SNI wasn't specified in the
    /// request or SNI couldn't be found in the map.
    case primary
    /// Encodes an unknown integer value.
    ///
    /// The most common cause for an unknown values is for the service to send
    /// a value unknown to the library. We recommend you update your library to
    /// the latest version.
    case unknownIntValue(Int)
    /// Encodes an unknown string value.
    ///
    /// The most common cause for an unknown values is for the service to send
    /// a value unknown to the library. We recommend you update your library to
    /// the latest version.
    case unknownStringValue(String)

    public init() {
      self = .unspecified
    }

    /// Returns the integer value associated with the enumeration.
    ///
    /// If the enumeration was initialized with an unknown string value, this returns `nil`.
    public var intValue: Int? {
      switch self {
      case .unspecified: return 0
      case .primary: return 1
      case .unknownIntValue(let v): return v
      case .unknownStringValue: return nil
      }
    }

    /// Returns the string value (or name) associated with the enumeration.
    ///
    /// If the enumeration was initialized with an unknown integer value, this returns `nil`.
    public var stringValue: Swift.String? {
      switch self {
      case .unspecified: return "MATCHER_UNSPECIFIED"
      case .primary: return "PRIMARY"
      case .unknownIntValue: return nil
      case .unknownStringValue(let v): return v
      }
    }

    /// Initialize from a string value.
    ///
    /// If the value is unknown, this initializes to [`unknownStringValue`](doc:Matcher/unknownStringValue(_:)).
    public init(stringValue: Swift.String) {
      switch stringValue {
      case "MATCHER_UNSPECIFIED": self = .unspecified
      case "PRIMARY": self = .primary
      default: self = .unknownStringValue(stringValue)
      }
    }

    /// Initialize from an integer value.
    ///
    /// If the value is unknown, this initializes to [`unknownIntValue`](doc:Matcher/unknownIntValue(_:)).
    public init(intValue: Int) {
      switch intValue {
      case 0: self = .unspecified
      case 1: self = .primary
      default: self = .unknownIntValue(intValue)
      }
    }

    public init(from decoder: Decoder) throws {
      let container = try decoder.singleValueContainer()
      if let v = try? container.decode(Int.self) {
        self.init(intValue: v)
        return
      }
      if let s = try? container.decode(String.self) {
        if let v = Int(s) {
          self.init(intValue: v)
        } else {
          self.init(stringValue: s)
        }
        return
      }
      throw DecodingError.dataCorruptedError(
        in: container, debugDescription: "Expected enum value, must be integer or string.")
    }

    public func encode(to encoder: Encoder) throws {
      var container = encoder.singleValueContainer()
      switch self {
      case .unspecified: return try container.encode(0)
      case .primary: return try container.encode(1)
      case .unknownIntValue(let v): return try container.encode(v)
      case .unknownStringValue(let v): return try container.encode(v)
      }
    }
  }

  public enum OneOf_Match: Codable, Equatable, Sendable {
    /// A Hostname (FQDN, e.g. `example.com`) or a wildcard hostname expression
    /// (`*.example.com`) for a set of hostnames with common suffix. Used as
    /// Server Name Indication (SNI) for selecting a proper certificate.
    case hostname(Swift.String)
    /// A predefined matcher for particular cases, other than SNI selection.
    case matcher(CertificateMapEntry.Matcher)
  }

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