import * as tsplus_module_1 from "@tsplus/stdlib/data/Maybe/fromNullable";
import * as tsplus_module_2 from "@tsplus/stdlib/data/Tuple/make";
import * as tsplus_module_3 from "@effect/core/io/Effect/operations/intoDeferred";
import * as tsplus_module_4 from "@effect/core/io/Effect/operations/fork";
import * as tsplus_module_5 from "@effect/core/io/Deferred/operations/make";
import * as tsplus_module_6 from "@effect/core/io/Effect/operations/tap";
import * as tsplus_module_7 from "@effect/core/io/Effect/operations/map";
import * as tsplus_module_8 from "@effect/core/io/Effect/operations/succeedNow";
import * as tsplus_module_9 from "@tsplus/stdlib/data/Maybe/fold";
import * as tsplus_module_10 from "@effect/core/io/Effect/operations/flatMap";
import * as tsplus_module_11 from "@effect/core/io/Ref/operations/make";
/**
 * Returns a memoized version of the specified effectual function.
 *
 * @tsplus static ets/Effect/Ops memoize
 */

export function memoizeF(f, __tsplusTrace) {
  return tsplus_module_7.map_(tsplus_module_11.makeSynchronized(() => new Map(), __tsplusTrace), ref => a => tsplus_module_10.flatMap_(ref.modifyEffect(map => {
    const result = tsplus_module_1.fromNullable(map.get(a));
    return tsplus_module_9.fold_(result, () => tsplus_module_7.map_(tsplus_module_6.tap_(tsplus_module_5.make(__tsplusTrace), deferred => tsplus_module_4.fork(tsplus_module_3.intoDeferred_(f(a), () => deferred, __tsplusTrace), __tsplusTrace), __tsplusTrace), deferred => tsplus_module_2.make(deferred, map.set(a, deferred)), __tsplusTrace), deferred => tsplus_module_8.succeedNow(tsplus_module_2.make(deferred, map), __tsplusTrace));
  }, __tsplusTrace), deferred => deferred.await(__tsplusTrace), __tsplusTrace), __tsplusTrace);
}
//# sourceMappingURL=memoizeF.mjs.map