"use strict";

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

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("@effect/core/io/Effect/operations/succeed"));

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");

var _next = /*#__PURE__*/require("@effect/core/io/Scope/ReleaseMap/operations/_internal/next");

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; }

/**
 * Adds a finalizer to the finalizers associated with this scope. If the
 * scope is still open, a key will be returned. This is an identifier that can
 * be used to activate this finalizer and remove it from the map. If the scope
 * has been closed, the finalizer will be executed immediately (with the `Exit`
 * value with which the scope has ended) and no key will be returned.
 *
 * @tsplus fluent ets/ReleaseMap addIfOpen
 */
function addIfOpen_(self, finalizer, __tsplusTrace) {
  return tsplus_module_5.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((0, _next.next)(s.nextKey), s.exit, s.update));
        }

      case "Running":
        {
          const finalizers = s.finalizers().set(s.nextKey, finalizer);
          return tsplus_module_3.make(tsplus_module_4.succeed(() => tsplus_module_1.some(s.nextKey), __tsplusTrace), new _State.Running((0, _next.next)(s.nextKey), finalizers, s.update));
        }
    }
  }, __tsplusTrace), __tsplusTrace);
}
/**
 * Adds a finalizer to the finalizers associated with this scope. If the
 * scope is still open, a key will be returned. This is an identifier that can
 * be used to activate this finalizer and remove it from the map. If the scope
 * has been closed, the finalizer will be executed immediately (with the `Exit`
 * value with which the scope has ended) and no key will be returned.
 *
 * @tsplus static ets/ReleaseMap/Aspects addIfOpen
 */


const addIfOpen = (finalizer, __tsplusTrace) => self => addIfOpen_(self, finalizer, __tsplusTrace);

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