import * as tracing_1 from "../../Tracing"
const fileName_1 = "(@effect-ts/system): _src/Managed/methods/allocate.ts"
import * as Tp from "../../Collections/Immutable/Tuple"
import * as T from "../deps"
import { makeReleaseMap, releaseAll } from "../ReleaseMap"
export class Allocation {
  constructor(value, release) {
    this.value = value
    this.release = release
  }
}
/**
 * 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.
 */

export function allocate(self) {
  return T.chain_(
    makeReleaseMap,
    (rm) =>
      T.foldCauseM_(
        T.provideSome_(self.effect, (r) => Tp.tuple(r, rm), fileName_1 + ":22:21"),
        (cause) =>
          T.chain_(
            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) =>
                  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"
  )
}
//# sourceMappingURL=allocate.js.map
