// Automatically generated by xdrgen
// DO NOT EDIT or your changes may be overwritten

import Foundation

// === xdr source ============================================================

//  struct ManageInvoiceRequestOp
//  {
//      union switch (ManageInvoiceRequestAction action){
//      case CREATE:
//          InvoiceCreationRequest invoiceRequest;
//      case REMOVE:
//          uint64 requestID;
//      } details;
//  
//  	// reserved for future use
//      union switch (LedgerVersion v)
//      {
//      case EMPTY_VERSION:
//          void;
//      }
//      ext;
//  };

//  ===========================================================================
public struct ManageInvoiceRequestOp: XDRCodable {
  public var details: ManageInvoiceRequestOpDetails
  public var ext: ManageInvoiceRequestOpExt

  public init(
      details: ManageInvoiceRequestOpDetails,
      ext: ManageInvoiceRequestOpExt) {

    self.details = details
    self.ext = ext
  }

  public func toXDR() -> Data {
    var xdr = Data()

    xdr.append(self.details.toXDR())
    xdr.append(self.ext.toXDR())

    return xdr
  }

  public init(xdrData: inout Data) throws {
    self.details = try ManageInvoiceRequestOpDetails(xdrData: &xdrData)
    self.ext = try ManageInvoiceRequestOpExt(xdrData: &xdrData)
  }

  public enum ManageInvoiceRequestOpDetails: XDRDiscriminatedUnion {
    case create(InvoiceCreationRequest)
    case remove(Uint64)

    public var discriminant: Int32 {
      switch self {
      case .create: return ManageInvoiceRequestAction.create.rawValue
      case .remove: return ManageInvoiceRequestAction.remove.rawValue
      }
    }

    public func toXDR() -> Data {
      var xdr = Data()

      xdr.append(self.discriminant.toXDR())

      switch self {
      case .create(let data): xdr.append(data.toXDR())
      case .remove(let data): xdr.append(data.toXDR())
      }

      return xdr
    }

    public init(xdrData: inout Data) throws {
      let discriminant = try Int32(xdrData: &xdrData)

      switch discriminant {
      case ManageInvoiceRequestAction.create.rawValue:
        let data = try InvoiceCreationRequest(xdrData: &xdrData)
        self = .create(data)
      case ManageInvoiceRequestAction.remove.rawValue:
        let data = try Uint64(xdrData: &xdrData)
        self = .remove(data)
      default:
        throw XDRErrors.unknownEnumCase
      }
    }

  }
  public enum ManageInvoiceRequestOpExt: XDRDiscriminatedUnion {
    case emptyVersion

    public var discriminant: Int32 {
      switch self {
      case .emptyVersion: return LedgerVersion.emptyVersion.rawValue
      }
    }

    public func toXDR() -> Data {
      var xdr = Data()

      xdr.append(self.discriminant.toXDR())

      switch self {
      case .emptyVersion: xdr.append(Data())
      }

      return xdr
    }

    public init(xdrData: inout Data) throws {
      let discriminant = try Int32(xdrData: &xdrData)

      switch discriminant {
      case LedgerVersion.emptyVersion.rawValue: self = .emptyVersion
      default:
        throw XDRErrors.unknownEnumCase
      }
    }

  }
}
