import * as tsplus_module_1 from "@tsplus/stdlib/data/Maybe/definition";
import * as tsplus_module_2 from "@effect/core/stm/TMap/operations/delete";
import * as tsplus_module_3 from "@effect/core/stm/STM/operations/as";
import * as tsplus_module_4 from "@effect/core/stm/TMap/operations/put";
import * as tsplus_module_5 from "@tsplus/stdlib/data/Maybe/fold";
import * as tsplus_module_6 from "@effect/core/stm/TMap/operations/get";
import * as tsplus_module_7 from "@effect/core/stm/STM/operations/flatMap";
/**
 * Updates the mapping for the specified key with the specified function,
 * which takes the current value of the key as an input, if it exists, and
 * either returns `Some` with a new value to indicate to update the value in
 * the map or `None` to remove the value from the map. Returns `Some` with the
 * updated value or `None` if the value was removed from the map.
 *
 * @tsplus fluent ets/TMap updateWith
 */

export function updateWith_(self, k, f) {
  return tsplus_module_7.flatMap_(tsplus_module_6.get_(self, k), _ => tsplus_module_5.fold_(f(_), () => tsplus_module_3.as_(tsplus_module_2.delete_(self, k), () => tsplus_module_1.emptyOf()), v => tsplus_module_3.as_(tsplus_module_4.put_(self, k, v), () => tsplus_module_1.some(v))));
}
/**
 * Updates the mapping for the specified key with the specified function,
 * which takes the current value of the key as an input, if it exists, and
 * either returns `Some` with a new value to indicate to update the value in
 * the map or `None` to remove the value from the map. Returns `Some` with the
 * updated value or `None` if the value was removed from the map.
 *
 * @tsplus static ets/TMap/Aspects updateWith
 */

export const updateWith = (k, f) => self => updateWith_(self, k, f);
//# sourceMappingURL=updateWith.mjs.map