"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.onTermination = exports.onTermination_ = void 0
const Cause_1 = require("../Cause")
const E = __importStar(require("../Either"))
const Function_1 = require("../Function")
const bracketExit_1 = require("./bracketExit")
const core_1 = require("./core")
/**
 * Runs the specified effect if this effect is terminated, either because of
 * a defect or because of interruption.
 */
function onTermination_(self, cleanup, __trace) {
  return (0, bracketExit_1.bracketExit_)(
    core_1.unit,
    () => self,
    (_, eb) => {
      switch (eb._tag) {
        case "Success": {
          return core_1.unit
        }
        case "Failure": {
          return E.fold_(
            (0, Cause_1.failureOrCause)(eb.cause),
            () => core_1.unit,
            cleanup
          )
        }
      }
    },
    __trace
  )
}
exports.onTermination_ = onTermination_
/**
 * Runs the specified effect if this effect is terminated, either because of
 * a defect or because of interruption.
 *
 * @dataFirst onTermination_
 */
function onTermination(cleanup, __trace) {
  return (self) => onTermination_(self, cleanup, __trace)
}
exports.onTermination = onTermination
//# sourceMappingURL=onTermination.js.map
