import * as tracing_1 from "../../../../Tracing";
const fileName_1 = "(@effect-ts/system): _src/Collections/Immutable/Chunk/api/collectWhileM.ts";
import * as core from "../../../../Effect/core";
import * as map from "../../../../Effect/map";
import * as zipWith from "../../../../Effect/zipWith";
import * as O from "../../../../Option";
import * as Chunk from "../core";
import * as ChunkDef from "../definition";
/**
 * Transforms all elements of the chunk for as long as the specified partial function is defined.
 */

export function collectWhileM_(self, f) {
  ;

  switch (self._typeId) {
    case ChunkDef.SingletonTypeId:
      {
        return O.fold_(f(self.a), () => core.succeed(Chunk.empty(), fileName_1 + ":22:27"), b => map.map_(b, Chunk.single, fileName_1 + ":23:24"));
      }

    case ChunkDef.ArrTypeId:
      {
        const array = self.arrayLike();
        let dest = core.succeed(Chunk.empty(), fileName_1 + ":28:60");

        for (let i = 0; i < array.length; i++) {
          const rhs = f(array[i]);

          if (O.isSome(rhs)) {
            dest = zipWith.zipWith_(dest, rhs.value, Chunk.append_, fileName_1 + ":32:34");
          } else {
            return dest;
          }
        }

        return dest;
      }

    default:
      {
        return collectWhileM_(self.materialize(), f);
      }
  }
}
/**
 * Transforms all elements of the chunk for as long as the specified partial function is defined.
 *
 * @ets_data_first collectWhileM_
 */

export function collectWhileM(f) {
  return self => collectWhileM_(self, f);
}
//# sourceMappingURL=collectWhileM.js.map