"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.effectAsyncM = void 0
const Function_1 = require("../Function")
const P = __importStar(require("../Promise"))
const catchAllCause = __importStar(require("./catchAllCause"))
const core_1 = require("./core")
const Do = __importStar(require("./do"))
const interruption_1 = require("./interruption")
const map = __importStar(require("./map"))
const runtime_1 = require("./runtime")
const to = __importStar(require("./to"))
const zips = __importStar(require("./zips"))
/**
 * Imports an asynchronous effect into a pure `Effect` value. This formulation is
 * necessary when the effect is itself expressed in terms of `Effect`.
 */
function effectAsyncM(register, __trace) {
  return map.map_(
    Do.bind_(
      Do.bind_(
        Do.bind_(Do.do, "p", () => P.make()),
        "r",
        () => (0, runtime_1.runtime)()
      ),
      "a",
      ({ p, r }) =>
        (0, interruption_1.uninterruptibleMask)(({ restore }) =>
          zips.zipRight_(
            (0, core_1.fork)(
              restore(
                catchAllCause.catchAllCause_(
                  register((k) => {
                    r.run(to.to_(k, p))
                  }),
                  (c) => P.halt(c)(p)
                )
              ),
              __trace
            ),
            restore(P.await(p))
          )
        )
    ),
    ({ a }) => a
  )
}
exports.effectAsyncM = effectAsyncM
//# sourceMappingURL=effectAsyncM.js.map
