const require_constants = require("../constants.cjs");
const require_utils = require("../utils.cjs");
const require_write = require("./write.cjs");
let _langchain_core_runnables = require("@langchain/core/runnables");
//#region src/pregel/read.ts
var ChannelRead = class ChannelRead extends require_utils.RunnableCallable {
	lc_graph_name = "ChannelRead";
	channel;
	fresh = false;
	mapper;
	constructor(channel, mapper, fresh = false) {
		super({
			trace: false,
			func: (_, config) => ChannelRead.doRead(config, this.channel, this.fresh, this.mapper)
		});
		this.fresh = fresh;
		this.mapper = mapper;
		this.channel = channel;
		this.name = Array.isArray(channel) ? `ChannelRead<${channel.join(",")}>` : `ChannelRead<${channel}>`;
	}
	static doRead(config, channel, fresh, mapper) {
		const read = config.configurable?.[require_constants.CONFIG_KEY_READ];
		if (!read) throw new Error("Runnable is not configured with a read function. Make sure to call in the context of a Pregel process");
		if (mapper) return mapper(read(channel, fresh));
		else return read(channel, fresh);
	}
};
const defaultRunnableBound = /* @__PURE__ */ new _langchain_core_runnables.RunnablePassthrough();
var PregelNode = class PregelNode extends _langchain_core_runnables.RunnableBinding {
	lc_graph_name = "PregelNode";
	channels;
	triggers = [];
	mapper;
	writers = [];
	bound = defaultRunnableBound;
	kwargs = {};
	metadata = {};
	tags = [];
	retryPolicy;
	cachePolicy;
	timeout;
	subgraphs;
	ends;
	isErrorHandler;
	errorHandlerNode;
	constructor(fields) {
		const { channels, triggers, mapper, writers, bound, kwargs, metadata, retryPolicy, cachePolicy, timeout, tags, subgraphs, ends, isErrorHandler, errorHandlerNode } = fields;
		const mergedTags = [...fields.config?.tags ? fields.config.tags : [], ...tags ?? []];
		super({
			...fields,
			bound: fields.bound ?? defaultRunnableBound,
			config: {
				...fields.config ? fields.config : {},
				tags: mergedTags
			}
		});
		this.channels = channels;
		this.triggers = triggers;
		this.mapper = mapper;
		this.writers = writers ?? this.writers;
		this.bound = bound ?? this.bound;
		this.kwargs = kwargs ?? this.kwargs;
		this.metadata = metadata ?? this.metadata;
		this.tags = mergedTags;
		this.retryPolicy = retryPolicy;
		this.cachePolicy = cachePolicy;
		this.timeout = timeout;
		this.subgraphs = subgraphs;
		this.ends = ends;
		this.isErrorHandler = isErrorHandler;
		this.errorHandlerNode = errorHandlerNode;
	}
	getWriters() {
		const newWriters = [...this.writers];
		while (newWriters.length > 1 && newWriters[newWriters.length - 1] instanceof require_write.ChannelWrite && newWriters[newWriters.length - 2] instanceof require_write.ChannelWrite) {
			const endWriters = newWriters.slice(-2);
			const combinedWrites = endWriters[0].writes.concat(endWriters[1].writes);
			newWriters[newWriters.length - 2] = new require_write.ChannelWrite(combinedWrites, endWriters[0].config?.tags);
			newWriters.pop();
		}
		return newWriters;
	}
	getNode() {
		const writers = this.getWriters();
		if (this.bound === defaultRunnableBound && writers.length === 0) return;
		else if (this.bound === defaultRunnableBound && writers.length === 1) return writers[0];
		else if (this.bound === defaultRunnableBound) return new _langchain_core_runnables.RunnableSequence({
			first: writers[0],
			middle: writers.slice(1, writers.length - 1),
			last: writers[writers.length - 1],
			omitSequenceTags: true
		});
		else if (writers.length > 0) return new _langchain_core_runnables.RunnableSequence({
			first: this.bound,
			middle: writers.slice(0, writers.length - 1),
			last: writers[writers.length - 1],
			omitSequenceTags: true
		});
		else return this.bound;
	}
	join(channels) {
		if (!Array.isArray(channels)) throw new Error("channels must be a list");
		if (typeof this.channels !== "object") throw new Error("all channels must be named when using .join()");
		return new PregelNode({
			channels: {
				...this.channels,
				...Object.fromEntries(channels.map((chan) => [chan, chan]))
			},
			triggers: this.triggers,
			mapper: this.mapper,
			writers: this.writers,
			bound: this.bound,
			kwargs: this.kwargs,
			config: this.config,
			retryPolicy: this.retryPolicy,
			cachePolicy: this.cachePolicy,
			timeout: this.timeout
		});
	}
	pipe(coerceable) {
		if (require_write.ChannelWrite.isWriter(coerceable)) return new PregelNode({
			channels: this.channels,
			triggers: this.triggers,
			mapper: this.mapper,
			writers: [...this.writers, coerceable],
			bound: this.bound,
			config: this.config,
			kwargs: this.kwargs,
			retryPolicy: this.retryPolicy,
			cachePolicy: this.cachePolicy,
			timeout: this.timeout
		});
		else if (this.bound === defaultRunnableBound) return new PregelNode({
			channels: this.channels,
			triggers: this.triggers,
			mapper: this.mapper,
			writers: this.writers,
			bound: (0, _langchain_core_runnables._coerceToRunnable)(coerceable),
			config: this.config,
			kwargs: this.kwargs,
			retryPolicy: this.retryPolicy,
			cachePolicy: this.cachePolicy,
			timeout: this.timeout
		});
		else return new PregelNode({
			channels: this.channels,
			triggers: this.triggers,
			mapper: this.mapper,
			writers: this.writers,
			bound: this.bound.pipe(coerceable),
			config: this.config,
			kwargs: this.kwargs,
			retryPolicy: this.retryPolicy,
			cachePolicy: this.cachePolicy,
			timeout: this.timeout
		});
	}
};
//#endregion
exports.ChannelRead = ChannelRead;
exports.PregelNode = PregelNode;

//# sourceMappingURL=read.cjs.map