"use strict"
var __createBinding =
  (this && this.__createBinding) ||
  (Object.create
    ? function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        Object.defineProperty(o, k2, {
          enumerable: true,
          get: function () {
            return m[k]
          }
        })
      }
    : function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        o[k2] = m[k]
      })
var __setModuleDefault =
  (this && this.__setModuleDefault) ||
  (Object.create
    ? function (o, v) {
        Object.defineProperty(o, "default", { enumerable: true, value: v })
      }
    : function (o, v) {
        o["default"] = v
      })
var __importStar =
  (this && this.__importStar) ||
  function (mod) {
    if (mod && mod.__esModule) return mod
    var result = {}
    if (mod != null)
      for (var k in mod)
        if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
          __createBinding(result, mod, k)
    __setModuleDefault(result, mod)
    return result
  }
Object.defineProperty(exports, "__esModule", { value: true })
exports.gen = exports.GenStream = void 0
// tracing: off
const L = __importStar(require("../../Collections/Immutable/List"))
const GlobalExceptions_1 = require("../../GlobalExceptions")
const Utils_1 = require("../../Utils")
const T = __importStar(require("../_internal/effect"))
const effect_1 = require("../_internal/effect")
const chain_1 = require("./chain")
const definitions_1 = require("./definitions")
const fail_1 = require("./fail")
const fromEffect_1 = require("./fromEffect")
const succeed_1 = require("./succeed")
const suspend_1 = require("./suspend")
class GenStream {
  constructor(effect) {
    this.effect = effect
  }
  *[Symbol.iterator]() {
    return yield this
  }
}
exports.GenStream = GenStream
const adapter = (_, __) => {
  return new GenStream(() => {
    const x = _()
    if ((0, Utils_1.isOption)(x)) {
      return x._tag === "None"
        ? (0, fail_1.fail)(__ ? __() : new GlobalExceptions_1.NoSuchElementException())
        : (0, succeed_1.succeed)(x.value)
    } else if ((0, Utils_1.isEither)(x)) {
      return (0, fromEffect_1.fromEffect)(T.fromEither(() => x))
    } else if (x instanceof definitions_1.Stream) {
      return x
    } else if ((0, Utils_1.isTag)(x)) {
      return (0, fromEffect_1.fromEffect)(T.service(x))
    }
    return (0, fromEffect_1.fromEffect)(x)
  })
}
function gen(...args) {
  function gen_(f) {
    return (0, suspend_1.suspend)(() => {
      function run(replayStack) {
        const iterator = f(adapter)
        let state = iterator.next()
        for (const a of replayStack) {
          if (state.done) {
            return (0, fromEffect_1.fromEffect)(
              T.die(new GlobalExceptions_1.PrematureGeneratorExit())
            )
          }
          state = iterator.next(a)
        }
        if (state.done) {
          return (0, succeed_1.succeed)(state.value)
        }
        return (0, chain_1.chain_)(state.value["effect"](), (val) => {
          return run(L.append_(replayStack, val))
        })
      }
      return run(L.empty())
    })
  }
  if (args.length === 0) {
    return (f) => gen_(f)
  }
  return gen_(args[0])
}
exports.gen = gen
//# sourceMappingURL=gen.js.map
