import * as tsplus_module_1 from "@effect/core/stm/STM/operations/succeedNow";
import * as tsplus_module_2 from "@effect/core/stm/STM/operations/flatMap";
import * as tsplus_module_3 from "@effect/core/stm/STM/operations/suspend";
/**
 * inspired by https://github.com/tusharmath/qio/pull/22 (revised)
 */

import { _A, _E, _R } from "@effect/core/stm/STM/definition/base";
export class GenSTM {
  constructor(stm) {
    this.stm = stm;
  }

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

}

const adapter = (_, __) => {
  return new GenSTM(_);
};
/**
 * Do simulation using Generators
 *
 * @tsplus static ets/STM/Ops gen
 */


export function gen(f) {
  return tsplus_module_3.suspend(() => {
    const iterator = f(adapter);
    const state = iterator.next();

    function run(state) {
      if (state.done) {
        return tsplus_module_1.succeedNow(state.value);
      }

      return tsplus_module_2.flatMap_(state.value["stm"], val => {
        const next = iterator.next(val);
        return run(next);
      });
    }

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