import { pipe } from "../Function"
import * as P from "../Promise"
import * as catchAllCause from "./catchAllCause"
import { fork } from "./core"
import * as Do from "./do"
import { uninterruptibleMask } from "./interruption"
import * as map from "./map"
import { runtime } from "./runtime"
import * as to from "./to"
import * as zips from "./zips"
/**
 * Imports an asynchronous effect into a pure `Effect` value. This formulation is
 * necessary when the effect is itself expressed in terms of `Effect`.
 */

export function effectAsyncM(register, __trace) {
  return map.map_(
    Do.bind_(
      Do.bind_(
        Do.bind_(Do.do, "p", () => P.make()),
        "r",
        () => runtime()
      ),
      "a",
      ({ p, r }) =>
        uninterruptibleMask(({ restore }) =>
          zips.zipRight_(
            fork(
              restore(
                catchAllCause.catchAllCause_(
                  register((k) => {
                    r.run(to.to_(k, p))
                  }),
                  (c) => P.halt(c)(p)
                )
              ),
              __trace
            ),
            restore(P.await(p))
          )
        )
    ),
    ({ a }) => a
  )
}
//# sourceMappingURL=effectAsyncM.js.map
