"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.allocate = exports.Allocation = void 0
const tracing_1 = __importStar(require("../../Tracing"))
const tracing_2 = __importStar(require("../../Tracing"))
const fileName_1 = "(@effect-ts/system): _src/Managed/methods/allocate.ts"
const Tp = __importStar(require("../../Collections/Immutable/Tuple"))
const T = __importStar(require("../deps"))
const ReleaseMap_1 = require("../ReleaseMap")
class Allocation {
  constructor(value, release) {
    this.value = value
    this.release = release
  }
}
exports.Allocation = Allocation
/**
 * Allocates the managed for future usage & release.
 *
 * Note: in case of failures during acquisition resources that
 * have been acquired will be immediately released. In case the
 * managed succeeds in acquiring all the resources an Allocation
 * will be returned and it is up to the caller to ensure invokation
 * of `release`, if that is not done resources will not be released.
 */
function allocate(self) {
  return T.chain_(
    ReleaseMap_1.makeReleaseMap,
    (rm) =>
      T.foldCauseM_(
        T.provideSome_(self.effect, (r) => Tp.tuple(r, rm), fileName_1 + ":22:21"),
        (cause) =>
          T.chain_(
            (0, ReleaseMap_1.releaseAll)(
              T.exitHalt(cause),
              T.sequential,
              fileName_1 + ":24:28"
            )(rm),
            () => T.halt(cause, fileName_1 + ":24:79"),
            fileName_1 + ":24:17"
          ),
        ({ tuple: [_, a] }) =>
          T.succeed(
            new Allocation(
              a,
              T.descriptorWith(
                (d) =>
                  (0, ReleaseMap_1.releaseAll)(
                    T.exitInterrupt(d.id),
                    T.sequential,
                    fileName_1 + ":29:47"
                  )(rm),
                fileName_1 + ":29:29"
              )
            ),
            fileName_1 + ":26:18"
          ),
        fileName_1 + ":21:18"
      ),
    fileName_1 + ":20:18"
  )
}
exports.allocate = allocate
//# sourceMappingURL=allocate.js.map
