"use strict"
var __createBinding =
  (this && this.__createBinding) ||
  (Object.create
    ? function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        Object.defineProperty(o, k2, {
          enumerable: true,
          get: function () {
            return m[k]
          }
        })
      }
    : function (o, m, k, k2) {
        if (k2 === undefined) k2 = k
        o[k2] = m[k]
      })
var __setModuleDefault =
  (this && this.__setModuleDefault) ||
  (Object.create
    ? function (o, v) {
        Object.defineProperty(o, "default", { enumerable: true, value: v })
      }
    : function (o, v) {
        o["default"] = v
      })
var __importStar =
  (this && this.__importStar) ||
  function (mod) {
    if (mod && mod.__esModule) return mod
    var result = {}
    if (mod != null)
      for (var k in mod)
        if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k))
          __createBinding(result, mod, k)
    __setModuleDefault(result, mod)
    return result
  }
Object.defineProperty(exports, "__esModule", { value: true })
exports.restartable = exports.more = exports.halt = exports.fail = exports.emit = void 0
// tracing: off
require("../../Operator")
const A = __importStar(require("../../Collections/Immutable/Chunk"))
const Tp = __importStar(require("../../Collections/Immutable/Tuple"))
const E = __importStar(require("../../Either"))
const Function_1 = require("../../Function")
const T = __importStar(require("../_internal/effect"))
const M = __importStar(require("../_internal/managed"))
const R = __importStar(require("../_internal/ref"))
function emit(z, leftover) {
  return T.fail(Tp.tuple(E.right(z), leftover))
}
exports.emit = emit
function fail(e, leftover) {
  return T.fail(Tp.tuple(E.left(e), leftover))
}
exports.fail = fail
function halt(c) {
  return T.mapError_(T.halt(c), (e) => Tp.tuple(E.left(e), A.empty()))
}
exports.halt = halt
exports.more = T.unit
/**
 * Decorates a Push with a Effect value that re-initializes it with a fresh state.
 */
function restartable(sink) {
  return M.map_(
    M.bind_(
      M.bind_(
        M.bind_(M.do, "switchSink", () => M.switchable()),
        "initialSink",
        ({ switchSink }) => T.toManaged(switchSink(sink))
      ),
      "currSink",
      ({ initialSink }) => T.toManaged(R.makeRef(initialSink))
    ),
    ({ currSink, switchSink }) => {
      const restart = T.chain_(switchSink(sink), currSink.set)
      const newPush = (input) => T.chain_(currSink.get, (f) => f(input))
      return Tp.tuple(newPush, restart)
    }
  )
}
exports.restartable = restartable
//# sourceMappingURL=index.js.map
