"use strict";

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

var tsplus_module_1 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/io/Effect/definition"));

var tsplus_module_2 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/STM/TxnId"));

var tsplus_module_3 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@tsplus/stdlib/data/AtomicReference"));

var tsplus_module_4 = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("@effect/core/stm/STM/State"));

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

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

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

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

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

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

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

var _Journal = /*#__PURE__*/require("@effect/core/stm/STM/Journal");

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

/**
 * @tsplus static ets/STM/Ops atomically
 */
function atomically(self, __tsplusTrace) {
  return tsplus_module_11.environmentWithEffect(env => tsplus_module_10.suspendSucceedWith((_, fiberId) => {
    const v = (0, _Journal.tryCommitSync)(fiberId, self, env);

    switch (v._tag) {
      case "Done":
        {
          throw new tsplus_module_1.Effect.Error(v.exit, __tsplusTrace);
        }

      case "Suspend":
        {
          const txnId = tsplus_module_2.makeTxnId();
          const state = new tsplus_module_3.AtomicReference(tsplus_module_4.running);

          const io = tsplus_module_5._async((0, _Journal.tryCommitAsync)(v.journal, fiberId, self, txnId, state, env), __tsplusTrace);

          return tsplus_module_9.uninterruptibleMask(({
            restore
          }) => tsplus_module_8.catchAllCause_(restore(() => io, __tsplusTrace), cause => {
            state.compareAndSet(tsplus_module_4.running, tsplus_module_4.interrupted);
            const currentState = state.get;
            return currentState._tag === "Done" ? tsplus_module_6.done(() => currentState.exit, __tsplusTrace) : tsplus_module_7.failCause(() => cause, __tsplusTrace);
          }, __tsplusTrace), __tsplusTrace);
        }
    }
  }, __tsplusTrace), __tsplusTrace);
}
//# sourceMappingURL=atomically.js.map