import { p as prop, k as each, m as setup_stores, f as set_style, q as store_get } from './i18n-CGlVUOvE.js';
import { M as push, ab as state, ac as proxy, ad as user_effect, $ as set, t as template_effect, a as append, O as pop, R as from_html, Z as get, Q as child, T as reset } from './index-Bq2njFOY.js';
import { T as ToastContent } from './StreamingBar.svelte_svelte_type_style_lang-DWGn5tT_.js';
import { s as spring } from './index-1GCmrSlD.js';

var root_1 = from_html(`<div class="toast-item svelte-1qhecvt"><!></div>`);
var root = from_html(`<div class="toast-wrap svelte-1qhecvt"></div>`);

function Toast($$anchor, $$props) {
	push($$props, true);

	const $top = () => store_get(top, '$top', $$stores);
	const [$$stores, $$cleanup] = setup_stores();
	let messages = prop($$props, 'messages', 19, () => []);
	const top = spring(0, { stiffness: 0.4, damping: 0.5 });
	let grouped_messages = state(proxy([]));

	user_effect(() => {
		scroll_to_top(messages());
	});

	user_effect(() => {
		set(grouped_messages, group_messages(messages()), true);
	});

	function group_messages(msgs) {
		const groups = new Map();

		msgs.forEach((msg) => {
			const key = msg.type;

			if (!groups.has(key)) {
				groups.set(key, { type: msg.type, messages: [], expanded: true });
			}

			groups.get(key).messages.push(msg);
		});

		return Array.from(groups.values());
	}

	function scroll_to_top(_messages) {
		if (_messages.length > 0) {
			if ("parentIFrame" in window) {
				window.parentIFrame?.getPageInfo((page_info) => {
					if (page_info.scrollTop < page_info.offsetTop) {
						top.set(0);
					} else {
						top.set(page_info.scrollTop - page_info.offsetTop);
					}
				});
			}
		}
	}

	function toggle_group(type) {
		set(
			grouped_messages,
			get(grouped_messages).map((group) => {
				if (group.type === type) {
					return { ...group, expanded: !group.expanded };
				}

				return group;
			}),
			true
		);
	}

	var div = root();

	each(div, 21, () => get(grouped_messages), (group) => group.type, ($$anchor, group) => {
		var div_1 = root_1();
		var node = child(div_1);

		ToastContent(node, {
			get type() {
				return get(group).type;
			},

			get messages() {
				return get(group).messages;
			},

			get expanded() {
				return get(group).expanded;
			},
			ontoggle: () => toggle_group(get(group).type),
			onclose: (id) => $$props.on_close(id)
		});

		reset(div_1);
		append($$anchor, div_1);
	});

	reset(div);
	template_effect(() => set_style(div, `--toast-top: ${$top() ?? ''}px;`));
	append($$anchor, div);
	pop();
	$$cleanup();
}

/* state.svelte.ts generated by Svelte v5.48.0 */

class LoadingStatus {
	current = {};
	fn_outputs = {};
	fn_inputs = {};
	pending_outputs = /* @__PURE__ */ new Map();
	fn_status = {};
	show_progress = {};

	register(dependency_id, outputs, inputs, show_progress) {
		this.fn_outputs[dependency_id] = outputs;
		this.fn_inputs[dependency_id] = inputs;
		this.show_progress[dependency_id] = show_progress;
	}

	clear(id) {
		if (id in this.current) {
			this.current[id] = {};
		}
	}

	update(args) {
		const updates = this.resolve_args(args);

		updates.forEach((
			{
				id,
				queue_position,
				queue_size,
				eta,
				status,
				message,
				progress,
				stream_state,
				time_limit,
				type
			}
		) => {
			this.current[id] = {
				queue: args.queue || false,
				queue_size,
				queue_position,
				eta,
				stream_state,
				message,
				progress: progress || void 0,
				status,
				fn_index: args.fn_index,
				time_limit,
				type,
				show_progress: this.show_progress[args.fn_index]
			};
		});
	}

	set_status(id, status) {
		this.current[id].status = status;
	}

	resolve_args(args) {
		const {
			fn_index,
			status,
			size = void 0,
			position = null,
			eta = null,
			message = null,
			stream_state = null,
			time_limit = null,
			progress_data = null
		} = args;

		const outputs = this.fn_outputs[fn_index];
		const last_status = this.fn_status[fn_index];
		const inputs = this.fn_inputs[fn_index];
		const all_ids = outputs.concat(inputs);

		return all_ids.map((id) => {
			let new_status;
			const pending_count = this.pending_outputs.get(id) || 0;

			if (last_status === "pending" && status !== "pending") {
				let new_count = pending_count - 1;

				this.pending_outputs.set(id, new_count < 0 ? 0 : new_count);
				new_status = new_count > 0 ? "pending" : status;
			} else if (last_status === "pending" && status === "pending") {
				new_status = "pending";
			} else if (last_status !== "pending" && status === "pending") {
				new_status = "pending";
				this.pending_outputs.set(id, pending_count + 1);
			} else {
				new_status = status;
			}

			const type = inputs.includes(id) && stream_state ? "input" : outputs.includes(id) ? "output" : "skip";

			return {
				id,
				queue_position: position,
				queue_size: size,
				eta,
				status: new_status,
				message,
				progress: progress_data,
				stream_state,
				time_limit,
				type
			};
		}).filter((update) => update.type !== "skip");
	}
}

export { LoadingStatus as L, Toast as T };
//# sourceMappingURL=state.svelte-eX_HNnCz.js.map
