"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.defaultIfEmpty = exports.defaultIfEmpty_ = void 0;
// ets_tracing: off
const CK = __importStar(require("../../../../Collections/Immutable/Chunk"));
const CH = __importStar(require("../../Channel"));
const C = __importStar(require("../core"));
/**
 * Switches to the provided stream in case this one is empty.
 */
function defaultIfEmptyStream(self, stream) {
    const writer = () => CH.readWith((in_) => CK.isEmpty(in_)
        ? writer()
        : CH.zipRight_(CH.write(in_), CH.identity()), (e) => CH.fail(e), (_) => stream.channel);
    return new C.Stream(self.channel[">>>"](writer()));
}
/**
 * Produces the specified chunk if this stream is empty.
 */
function defaultIfEmptyChunk(self, chunk) {
    return defaultIfEmptyStream(self, new C.Stream(CH.write(chunk)));
}
/**
 * Produces the specified element if this stream is empty.
 */
function defaultIfEmptyValue(self, a) {
    return defaultIfEmptyChunk(self, CK.single(a));
}
function defaultIfEmpty_(self, emptyValue) {
    if (CK.isChunk(emptyValue)) {
        return defaultIfEmptyChunk(self, emptyValue);
    }
    if (C.isStream(emptyValue)) {
        return defaultIfEmptyStream(self, emptyValue);
    }
    return defaultIfEmptyValue(self, emptyValue);
}
exports.defaultIfEmpty_ = defaultIfEmpty_;
function defaultIfEmpty(emptyValue) {
    return (self) => defaultIfEmpty_(self, emptyValue);
}
exports.defaultIfEmpty = defaultIfEmpty;
//# sourceMappingURL=defaultIfEmpty.js.map