// tracing: off
import { insert, lookup } from "../../Collections/Immutable/Map/core"
import * as Tp from "../../Collections/Immutable/Tuple"
import { absurd, pipe } from "../../Function"
import * as O from "../../Option"
import * as T from "../deps"
import * as R from "./deps-ref"
import { Exited } from "./Exited"
import { Running } from "./Running"
export function replace(key, finalizer) {
  return (_) =>
    T.flatten(
      R.modify_(_.ref, (s) => {
        switch (s._tag) {
          case "Exited":
            return Tp.tuple(
              T.map_(finalizer(s.exit), () => O.none),
              new Exited(s.nextKey, s.exit)
            )

          case "Running":
            return Tp.tuple(
              T.succeed(lookup(key)(s.finalizers())),
              new Running(s.nextKey, insert(key, finalizer)(s.finalizers()))
            )

          default:
            return absurd(s)
        }
      })
    )
}
//# sourceMappingURL=replace.js.map
