"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.memoizeF = memoizeF;

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

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

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

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

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

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

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

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

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

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

var tsplus_module_11 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Ref/operations/make"));

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

/**
 * Returns a memoized version of the specified effectual function.
 *
 * @tsplus static ets/Effect/Ops memoize
 */
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.js.map