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

#if CompletionService
  import Foundation
  import GoogleCloudWkt

  /// Autocomplete suggestions that are imported from Customer.
  public struct CompletionSuggestion: Codable, Equatable, GoogleCloudWkt._AnyPackable,
    Sendable
  {
    /// Required. The suggestion text.
    public var suggestion: Swift.String = Swift.String()

    /// BCP-47 language code of this suggestion.
    public var languageCode: Swift.String = Swift.String()

    /// If two suggestions have the same groupId, they will not be
    /// returned together. Instead the one ranked higher will be returned. This can
    /// be used to deduplicate semantically identical suggestions.
    public var groupId: Swift.String = Swift.String()

    /// The score of this suggestion within its group.
    public var groupScore: Swift.Double = Swift.Double()

    /// Alternative matching phrases for this suggestion.
    public var alternativePhrases: [Swift.String] = []

    /// Ranking metrics of this suggestion.
    public var rankingInfo: OneOf_RankingInfo? = nil

    /// Initialize a new instance of `CompletionSuggestion`.
    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 = CompletionSuggestion().with { $0.globalScore = ... }
    /// ```
    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 globalScore = "globalScore"
      case frequency = "frequency"
      case suggestion = "suggestion"
      case languageCode = "languageCode"
      case groupId = "groupId"
      case groupScore = "groupScore"
      case alternativePhrases = "alternativePhrases"
    }

    public init(from decoder: Decoder) throws {
      let container = try decoder.container(keyedBy: CodingKeys.self)
      self.suggestion = try container.decode(Swift.String.self, forKey: .suggestion)
      self.languageCode = try container.decode(Swift.String.self, forKey: .languageCode)
      self.groupId = try container.decode(Swift.String.self, forKey: .groupId)
      self.groupScore = try container.decode(Swift.Double.self, forKey: .groupScore)
      self.alternativePhrases = try container.decode(
        [Swift.String].self, forKey: .alternativePhrases)

      var rankingInfo: OneOf_RankingInfo? = nil
      let rankingInfoCheckAndSet = {
        if rankingInfo != nil {
          throw DecodingError.dataCorrupted(
            DecodingError.Context(
              codingPath: decoder.codingPath,
              debugDescription: "Multiple values set for oneof 'rankingInfo'"))
        }
        rankingInfo = $0
      }
      if let globalScore = try container.decodeIfPresent(Swift.Double.self, forKey: .globalScore) {
        try rankingInfoCheckAndSet(.globalScore(globalScore))
      }
      if let frequency = try container.decodeIfPresent(Swift.Int64.self, forKey: .frequency) {
        try rankingInfoCheckAndSet(.frequency(frequency))
      }
      self.rankingInfo = rankingInfo
    }

    public func encode(to encoder: Encoder) throws {
      var container = encoder.container(keyedBy: CodingKeys.self)
      try container.encode(self.suggestion, forKey: .suggestion)
      try container.encode(self.languageCode, forKey: .languageCode)
      try container.encode(self.groupId, forKey: .groupId)
      try container.encode(self.groupScore, forKey: .groupScore)
      try container.encode(self.alternativePhrases, forKey: .alternativePhrases)

      if let choice = self.rankingInfo {
        switch choice {
        case .globalScore(let value):
          try container.encode(value, forKey: .globalScore)
        case .frequency(let value):
          try container.encode(value, forKey: .frequency)
        }
      }
    }

    /// Ranking metrics of this suggestion.
    public enum OneOf_RankingInfo: Codable, Equatable, Sendable {
      /// Global score of this suggestion. Control how this suggestion would be
      /// scored / ranked.
      case globalScore(Swift.Double)
      /// Frequency of this suggestion. Will be used to rank suggestions when score
      /// is not available.
      case frequency(Swift.Int64)
    }

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