"use strict";

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

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

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

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

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

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

var tsplus_module_6 = /*#__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; }

/**
 * Replaces the finalizer associated with this key and returns it. If the
 * finalizers associated with this scope have already been run this
 * finalizer will be run immediately.
 *
 * @tsplus fluent ets/ReleaseMap replace
 */
function replace_(self, key, finalizer, __tsplusTrace) {
  return tsplus_module_6.flattenNow(self.ref.modify(s => {
    switch (s._tag) {
      case "Exited":
        {
          return tsplus_module_3.make(tsplus_module_2.map_(finalizer(s.exit), () => tsplus_module_1.none, __tsplusTrace), new _State.Exited(s.nextKey, s.exit, s.update));
        }

      case "Running":
        {
          const finalizers = s.finalizers();
          const oldFinalizer = tsplus_module_4.fromNullable(finalizers.get(key));
          const newFinalizers = finalizers.set(key, finalizer);
          return tsplus_module_3.make(tsplus_module_5.succeed(() => oldFinalizer, __tsplusTrace), new _State.Running(s.nextKey, newFinalizers, s.update));
        }
    }
  }, __tsplusTrace), __tsplusTrace);
}
/**
 * Replaces the finalizer associated with this key and returns it. If the
 * finalizers associated with this scope have already been run this
 * finalizer will be run immediately.
 *
 * @tsplus static ets/ReleaseMap/Aspects replace
 */


const replace = (key, finalizer, __tsplusTrace) => self => replace_(self, key, finalizer, __tsplusTrace);

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