"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.repeatElementsWith = exports.repeatElementsWith_ = void 0;
const CK = __importStar(require("../../../../Collections/Immutable/Chunk"));
const T = __importStar(require("../../../../Effect"));
const Function_1 = require("../../../../Function");
const O = __importStar(require("../../../../Option"));
const SC = __importStar(require("../../../../Schedule"));
const CH = __importStar(require("../../Channel"));
const C = __importStar(require("../core"));
/**
 * Repeats each element of the stream using the provided schedule. When the schedule is finished,
 * then the output of the schedule will be emitted into the stream. Repetitions are done in
 * addition to the first execution, which means using `Schedule.recurs(1)` actually results in
 * the original effect, plus an additional recurrence, for a total of two repetitions of each
 * value in the stream.
 *
 * This function accepts two conversion functions, which allow the output of this stream and the
 * output of the provided schedule to be unified into a single type. For example, `Either` or
 * similar data type.
 */
function repeatElementsWith_(self, schedule, f, g) {
    return new C.Stream(self.channel[">>>"](CH.unwrap((T.map_(T.bind_(T.do, "driver", () => SC.driver(schedule)), ({ driver }) => {
        const feed = (in_) => O.fold_(CK.head(in_), () => loop(), (a) => CH.zipRight_(CH.write(CK.single(f(a))), step(CK.drop_(in_, 1), a)));
        const step = (in_, a) => {
            const advance = T.as_(driver.next(a), CH.zipRight_(CH.write(CK.single(f(a))), step(in_, a)));
            const reset = (T.map_(T.tap_(T.bind_(T.do, "b", () => T.orDie(driver.last)), () => driver.reset), ({ b }) => CH.zipRight_(CH.write(CK.single(g(b))), feed(in_))));
            return CH.unwrap(T.orElse_(advance, () => reset));
        };
        const loop = () => CH.readWith(feed, (_) => CH.fail(_), (_) => CH.unit);
        return loop();
    })))));
}
exports.repeatElementsWith_ = repeatElementsWith_;
/**
 * Repeats each element of the stream using the provided schedule. When the schedule is finished,
 * then the output of the schedule will be emitted into the stream. Repetitions are done in
 * addition to the first execution, which means using `Schedule.recurs(1)` actually results in
 * the original effect, plus an additional recurrence, for a total of two repetitions of each
 * value in the stream.
 *
 * This function accepts two conversion functions, which allow the output of this stream and the
 * output of the provided schedule to be unified into a single type. For example, `Either` or
 * similar data type.
 *
 * @ets_data_first repeatElementsWith_
 */
function repeatElementsWith(schedule, f, g) {
    return (self) => repeatElementsWith_(self, schedule, f, g);
}
exports.repeatElementsWith = repeatElementsWith;
//# sourceMappingURL=repeatElementsWith.js.map