const fileName_1 = "(@effect/core) _src/io/Effect/operations/gen.ts";
import * as tsplus_module_1 from "@tsplus/stdlib/data/Either/definition";
import * as tsplus_module_2 from "@effect/core/io/Effect/operations/fromEither";
import * as tsplus_module_3 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/fail";
import * as tsplus_module_5 from "@effect/core/io/Effect/operations/succeed";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/getOrFail";
import * as tsplus_module_7 from "@tsplus/stdlib/service/Tag";
import * as tsplus_module_8 from "@effect/core/io/Effect/operations/service";
import * as tsplus_module_9 from "@effect/core/io/Effect/operations/suspendSucceed";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_11 from "@effect/core/io/Effect/operations/unit";
import * as tsplus_module_12 from "@effect/core/io/Scope/operations/close";
import * as tsplus_module_13 from "@effect/core/io/Effect/operations/acquireUseReleaseExit";
import * as tsplus_module_14 from "@effect/core/io/Scope/operations/make";
/**
 * inspired by https://github.com/tusharmath/qio/pull/22 (revised)
 */

export const _GenR = /*#__PURE__*/Symbol.for("@effect/core/io/Effect/Gen/R");
export const _GenE = /*#__PURE__*/Symbol.for("@effect/core/io/Effect/Gen/E");
export const _GenA = /*#__PURE__*/Symbol.for("@effect/core/io/Effect/Gen/A");
export class GenEffect {
  constructor(effect, trace) {
    this.effect = effect;
    this.trace = trace;
  }

  *[Symbol.iterator]() {
    return yield this;
  }

}

function adapter(_, __, ___) {
  if (tsplus_module_1.isEither(_)) {
    return new GenEffect(tsplus_module_2.fromEither(() => _, fileName_1 + ":28:24"), __);
  }

  if (tsplus_module_3.isMaybe(_)) {
    if (__ && typeof __ === "function") {
      return new GenEffect(_._tag === "None" ? tsplus_module_4.fail(() => __(), fileName_1 + ":35:40") : tsplus_module_5.succeed(() => _.value, fileName_1 + ":35:69"), ___);
    }

    return new GenEffect(tsplus_module_6.getOrFail(() => _, fileName_1 + ":39:42"), __);
  }

  if (tsplus_module_7.Tag.is(_)) {
    return new GenEffect(tsplus_module_8.service(_, fileName_1 + ":42:40"), __);
  }

  return new GenEffect(_, __);
}
/**
 * @tsplus static ets/Effect/Ops genWithManaged
 */


export function genScoped(f, __tsplusTrace) {
  return tsplus_module_9.suspendSucceed(() => {
    const iterator = f(adapter);
    const state = iterator.next();

    function run(scope, state) {
      if (state.done) {
        return tsplus_module_5.succeed(() => state.value, __tsplusTrace);
      }

      return tsplus_module_10.flatMap_(tsplus_module_9.suspendSucceed(() => state.value.effect, state.value.trace), val => {
        const next = iterator.next(val);
        return run(scope, next);
      }, __tsplusTrace);
    }

    return tsplus_module_10.flatMap_(tsplus_module_14.make, scope => tsplus_module_13.acquireUseReleaseExit(() => tsplus_module_11.unit, () => run(scope, state), (_, exit) => tsplus_module_12.close_(scope, () => exit), __tsplusTrace), __tsplusTrace);
  }, __tsplusTrace);
}
/**
 * @tsplus static ets/Effect/Ops gen
 */

export function gen(f, __tsplusTrace) {
  return tsplus_module_9.suspendSucceed(() => {
    const iterator = f(adapter);
    const state = iterator.next();

    function run(state) {
      if (state.done) {
        return tsplus_module_5.succeed(() => state.value, __tsplusTrace);
      }

      return tsplus_module_10.flatMap_(tsplus_module_9.suspendSucceed(() => state.value["effect"], state.value.trace), val => run(iterator.next(val)), __tsplusTrace);
    }

    return run(state);
  }, __tsplusTrace);
}
//# sourceMappingURL=gen.mjs.map