import * as tsplus_module_1 from "@effect/core/io/Effect/operations/unit";
import * as tsplus_module_2 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_3 from "@tsplus/stdlib/data/Maybe/fromNullable";
import * as tsplus_module_4 from "@tsplus/stdlib/data/Maybe/fold";
import * as tsplus_module_5 from "@effect/core/io/Effect/operations/flatten";
import { Running } from "@effect/core/io/Scope/ReleaseMap/_internal/State";
/**
 * Runs the specified finalizer and removes it from the finalizers associated
 * with this scope.
 *
 * @tsplus fluent ets/ReleaseMap release
 */

export 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 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
 */

export const release = (key, exit, __tsplusTrace) => self => release_(self, key, exit, __tsplusTrace);
//# sourceMappingURL=release.mjs.map