/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 *
 *     http://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.
 */
package org.apache.beam.runners.fnexecution.wire;

import java.util.function.Predicate;
import org.apache.beam.model.pipeline.v1.RunnerApi.Coder;
import org.apache.beam.model.pipeline.v1.RunnerApi.Components;
import org.apache.beam.model.pipeline.v1.RunnerApi.MessageWithComponents;
import org.apache.beam.runners.core.construction.ModelCoders;
import org.apache.beam.runners.core.construction.SyntheticComponents;
import org.apache.beam.runners.core.construction.graph.PipelineNode.PCollectionNode;

/** Helpers to construct coders for gRPC port reads and writes. */
public class WireCoders {
  /** Creates an SDK-side wire coder for a port read/write for the given PCollection. */
  public static MessageWithComponents createSdkWireCoder(
      PCollectionNode pCollectionNode, Components components, Predicate<String> idUsed) {
    return createWireCoder(pCollectionNode, components, idUsed, false);
  }

  /**
   * Creates a runner-side wire coder for a port read/write for the given PCollection. Returns a
   * windowed value coder. The element coder itself
   */
  public static MessageWithComponents createRunnerWireCoder(
      PCollectionNode pCollectionNode, Components components, Predicate<String> idUsed) {
    return createWireCoder(pCollectionNode, components, idUsed, true);
  }

  private static MessageWithComponents createWireCoder(
      PCollectionNode pCollectionNode,
      Components components,
      Predicate<String> idUsed,
      boolean useByteArrayCoder) {
    String elementCoderId = pCollectionNode.getPCollection().getCoderId();
    String windowingStrategyId = pCollectionNode.getPCollection().getWindowingStrategyId();
    String windowCoderId =
        components.getWindowingStrategiesOrThrow(windowingStrategyId).getWindowCoderId();
    Coder windowedValueCoder = ModelCoders.windowedValueCoder(elementCoderId, windowCoderId);
    // Add the original WindowedValue<T, W> coder to the components;
    String windowedValueId =
        SyntheticComponents.uniqueId(String.format("fn/wire/%s", pCollectionNode.getId()), idUsed);
    return LengthPrefixUnknownCoders.forCoder(
        windowedValueId,
        components.toBuilder().putCoders(windowedValueId, windowedValueCoder).build(),
        useByteArrayCoder);
  }

  // Not instantiable.
  private WireCoders() {}
}

@@ -941,151 +941,8 @@
 rApi
-.Coder;%0Aimport org.apache.beam.model.pipeline.v1.RunnerApi.Components;%0Aimport org.apache.beam.model.pipeline.v1.RunnerApi.MessageWithComponents
 ;%0Aim
@@ -1347,32 +1347,42 @@
   public static 
+RunnerApi.
 MessageWithCompo
@@ -1438,32 +1438,42 @@
 CollectionNode, 
+RunnerApi.
 Components compo
@@ -1753,32 +1753,42 @@
   public static 
+RunnerApi.
 MessageWithCompo
@@ -1855,16 +1855,26 @@
 onNode, 
+RunnerApi.
 Componen
@@ -2009,16 +2009,26 @@
  static 
+RunnerApi.
 MessageW
@@ -2103,16 +2103,26 @@
 ,%0A      
+RunnerApi.
 Componen
@@ -2491,16 +2491,26 @@
 ();%0A    
+RunnerApi.
 Coder wi
@@ -2527,16 +2527,24 @@
 eCoder =
+%0A       
  ModelCo

Use RunnerApi in WireCoders to prepare for coder instantiation