"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.release = void 0;
exports.release_ = release_;

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/unit"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Tuple/make"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/fromNullable"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/Maybe/fold"));

var tsplus_module_5 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/operations/flatten"));

var _State = /*#__PURE__*/require("@effect/core/io/Scope/ReleaseMap/_internal/State");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

/**
 * Runs the specified finalizer and removes it from the finalizers associated
 * with this scope.
 *
 * @tsplus fluent ets/ReleaseMap release
 */
function release_(self, key, exit, __tsplusTrace) {
  return tsplus_module_5.flattenNow(self.ref.modify(s => {
    switch (s._tag) {
      case "Exited":
        {
          return tsplus_module_2.make(tsplus_module_1.unit, s);
        }

      case "Running":
        {
          const finalizers = s.finalizers();
          const finalizer = tsplus_module_3.fromNullable(finalizers.get(key));
          finalizers.delete(key);
          return tsplus_module_2.make(tsplus_module_4.fold_(finalizer, () => tsplus_module_1.unit, fin => s.update(fin)(exit)), new _State.Running(s.nextKey, finalizers, s.update));
        }
    }
  }, __tsplusTrace), __tsplusTrace);
}
/**
 * Runs the specified finalizer and removes it from the finalizers associated
 * with this scope.
 *
 * @tsplus static ets/ReleaseMap/Aspects release
 */


const release = (key, exit, __tsplusTrace) => self => release_(self, key, exit, __tsplusTrace);

exports.release = release;
//# sourceMappingURL=release.js.map