/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * license agreements; and to You under the Apache License, version 2.0:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * This file is part of the Apache Pekko project, which was derived from Akka.
 */

/*
 * Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
 */

package org.apache.pekko.stream.javadsl

import org.apache.pekko.stream._
import org.apache.pekko.stream.scaladsl
import org.apache.pekko.japi.function
import org.apache.pekko.japi.Pair
import org.apache.pekko.japi.tuple._
import org.apache.pekko.NotUsed

/**
 * Split one stream into several streams using a splitting function.
 *
 * '''Emits when''' all of the outputs stops backpressuring and there is an input element available
 *
 * '''Backpressures when''' any of the outputs backpressures
 *
 * '''Completes when''' upstream completes
 *
 * '''Cancels when''' any downstream cancels
 */
object UnzipWith {

  /**
   * Create a new `UnzipWith` vertex with the specified input type and unzipping-function `f`.
   *
   * @param f unzipping-function from the input value to the pair of output values
   */
  def create[In, A, B](f: function.Function[In, Pair[A, B]]):  Graph[FanOutShape2[In, A, B], NotUsed] =
    scaladsl.UnzipWith[In, A, B]((in: In) => f.apply(in) match { case Pair(a, b) => (a, b) })


  [3..22#/** Create a new `UnzipWith` specialized for 1 outputs.
   *
   * @param f unzipping-function from the input value to the output values
   */
  def create1[In, [#T1#]](f: function.Function[In, Tuple1[[#T1#]]]): Graph[FanOutShape1[In, [#T1#]], NotUsed] =
    scaladsl.UnzipWith[In, [#T1#]]((in: In) => f.apply(in).toScala)#

  ]

}
