"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.repeatWith = exports.repeatWith_ = void 0;
const CK = __importStar(require("../../../../Collections/Immutable/Chunk"));
const T = __importStar(require("../../../../Effect"));
const Function_1 = require("../../../../Function");
const SC = __importStar(require("../../../../Schedule"));
const CH = __importStar(require("../../Channel"));
const C = __importStar(require("../core"));
const Map = __importStar(require("./map"));
const Unwrap = __importStar(require("./unwrap"));
/**
 * Repeats the entire stream using the specified schedule. The stream will execute normally,
 * and then repeat again according to the provided schedule. The schedule output will be emitted at
 * the end of each repetition and can be unified with the stream elements using the provided functions.
 */
function repeatWith_(self, schedule, f, g) {
    return Unwrap.unwrap((T.map_(T.bind_(T.do, "driver", () => SC.driver(schedule)), ({ driver }) => {
        const scheduleOutput = T.map_(T.orDie(driver.last), g);
        const process = Map.map_(self, f).channel;
        const loop = CH.unwrap(T.fold_(driver.next(undefined), (_) => CH.unit, (_) => CH.zipRight_(CH.zipRight_(process, CH.unwrap(T.map_(scheduleOutput, (o) => CH.write(CK.single(o))))), loop)));
        return new C.Stream(CH.zipRight_(process, loop));
    })));
}
exports.repeatWith_ = repeatWith_;
/**
 * Repeats the entire stream using the specified schedule. The stream will execute normally,
 * and then repeat again according to the provided schedule. The schedule output will be emitted at
 * the end of each repetition and can be unified with the stream elements using the provided functions.
 *
 * @ets_data_first repeatWith_
 */
function repeatWith(schedule, f, g) {
    return (self) => repeatWith_(self, schedule, f, g);
}
exports.repeatWith = repeatWith;
//# sourceMappingURL=repeatWith.js.map