import { u as state, v as proxy, w as get, x as set, y as user_effect, z as untrack } from './index-BvBk1Iap.js';
import { s as snapshot } from './clone-DaB-S2nH.js';
import { $ as $locale } from './i18n-CMo5lpzy.js';

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

const I18N_MARKER = "__i18n__";

const TRANSLATABLE_PROPS = [
	"label",
	"info",
	"placeholder",
	"description",
	"title",
	"value"
];

class ShareError extends Error {
	constructor(message) {
		super(message);
		this.name = "ShareError";
	}
}

async function uploadToHuggingFace(data, type) {
	if (window.__gradio_space__ == null) {
		throw new ShareError("Must be on Spaces to share.");
	}

	let blob;
	let contentType;
	let filename;

	{
		let url;

		if (typeof data === "object" && data.url) {
			url = data.url;
		} else if (typeof data === "string") {
			url = data;
		} else {
			throw new Error("Invalid data format for URL type");
		}

		const response = await fetch(url);

		blob = await response.blob();
		contentType = response.headers.get("content-type") || "";
		filename = response.headers.get("content-disposition") || "";
	}

	const file = new File([blob], filename, { type: contentType });

	const uploadResponse = await fetch("https://huggingface.co/uploads", {
		method: "POST",
		body: file,
		headers: {
			"Content-Type": file.type,
			"X-Requested-With": "XMLHttpRequest"
		}
	});

	if (!uploadResponse.ok) {
		if (uploadResponse.headers.get("content-type")?.includes("application/json")) {
			const error = await uploadResponse.json();

			throw new ShareError(`Upload failed: ${error.error}`);
		}

		throw new ShareError(`Upload failed.`);
	}

	const result = await uploadResponse.text();

	return result;
}

function copy(node) {
	node.addEventListener("click", handle_copy);

	async function handle_copy(event) {
		const path = event.composedPath();
		const [copy_button] = path.filter((e) => e?.tagName === "BUTTON" && e.classList.contains("copy_code_button"));

		if (copy_button) {
			let copy_feedback = function (_copy_sucess_button) {
				_copy_sucess_button.style.opacity = "1";

				setTimeout(
					() => {
						_copy_sucess_button.style.opacity = "0";
					},
					2e3
				);
			};

			event.stopImmediatePropagation();

			const copy_text = copy_button.parentElement.innerText.trim();
			const copy_sucess_button = Array.from(copy_button.children)[1];
			const copied = await copy_to_clipboard(copy_text);

			if (copied) copy_feedback(copy_sucess_button);
		}
	}

	return {
		destroy() {
			node.removeEventListener("click", handle_copy);
		}
	};
}

async function copy_to_clipboard(value) {
	let copied = false;

	if ("clipboard" in navigator) {
		await navigator.clipboard.writeText(value);
		copied = true;
	} else {
		const textArea = document.createElement("textarea");

		textArea.value = value;
		textArea.style.position = "absolute";
		textArea.style.left = "-999999px";
		document.body.prepend(textArea);
		textArea.select();

		try {
			document.execCommand("copy");
			copied = true;
		} catch(error) {
			console.error(error);
			copied = false;
		} finally {
			textArea.remove();
		}
	}

	return copied;
}

const format_time = (seconds) => {
	const hours = Math.floor(seconds / 3600);
	const minutes = Math.floor(seconds % 3600 / 60);
	const seconds_remainder = Math.round(seconds) % 60;
	const padded_minutes = `${minutes < 10 ? "0" : ""}${minutes}`;
	const padded_seconds = `${seconds_remainder < 10 ? "0" : ""}${seconds_remainder}`;

	if (hours > 0) {
		return `${hours}:${padded_minutes}:${padded_seconds}`;
	}

	return `${minutes}:${padded_seconds}`;
};

const allowed_shared_props = [
	"elem_id",
	"elem_classes",
	"visible",
	"interactive",
	"server_fns",
	"server",
	"id",
	"target",
	"theme_mode",
	"version",
	"root",
	"autoscroll",
	"max_file_size",
	"formatter",
	"client",
	"load_component",
	"scale",
	"min_width",
	"theme",
	"padding",
	"loading_status",
	"label",
	"show_label",
	"validation_error",
	"show_progress",
	"api_prefix",
	"container",
	"attached_events",
	"register_component",
	"dispatcher"
];

function has_i18n_marker(value) {
	return typeof value === "string" && value.includes(I18N_MARKER);
}

function translate_i18n_marker(value, translate) {
	const start = value.indexOf(I18N_MARKER);

	if (start === -1) return value;

	const json_start = start + I18N_MARKER.length;
	const json_end = value.indexOf("}", json_start) + 1;

	if (json_end === 0) return value;

	try {
		const metadata = JSON.parse(value.slice(json_start, json_end));

		if (metadata?.key) {
			const translated = translate(metadata.key);
			const result = translated !== metadata.key ? translated : metadata.key;

			return value.slice(0, start) + result + value.slice(json_end);
		}
	} catch {}

	return value;
}

class Gradio {
	load_component;
	#shared = state(proxy({}));

	get shared() {
		return get(this.#shared);
	}

	set shared(value) {
		set(this.#shared, value, true);
	}

	#props = state(proxy({}));

	get props() {
		return get(this.#props);
	}

	set props(value) {
		set(this.#props, value, true);
	}

	#i18n = state((v) => v);

	get i18n() {
		return get(this.#i18n);
	}

	set i18n(value) {
		set(this.#i18n, value, true);
	}

	translatable_props = {};
	dispatcher;
	last_update = null;
	shared_props = allowed_shared_props;
	mounted = false;
	old_value;
	register_component;

	constructor(_props, default_values) {
		for (const key in _props.shared_props) {
			this.shared[key] = _props.shared_props[key];
		}

		for (const key in _props.props) {
			this.props[key] = _props.props[key];
		}

		if (default_values) {
			for (const key in default_values) {
				if (this.props[key] === void 0) {
					this.props[key] = default_values[key];
				}
			}
		}

		this.i18n = this.props.i18n ?? ((v) => v);

		for (const key of TRANSLATABLE_PROPS) {
			this.shared[key] = this._translate_and_store(
				"shared",
				key,
				// @ts-ignore
				_props.shared_props[key]
			);

			this.props[key] = this._translate_and_store(
				"props",
				key,
				// @ts-ignore
				_props.props[key]
			);
		}

		this.load_component = this.shared.load_component;
		this.register_component = this.shared.register_component || (() => {});
		this.dispatcher = this.shared.dispatcher || (() => {});

		this.register_component(
			_props.shared_props.id,
			// @ts-ignore
			this.set_data.bind(this),
			this.get_data.bind(this)
		);

		user_effect(() => {
			for (const key in _props.shared_props) {
				if (this._is_i18n_managed(`shared.${key}`, _props.shared_props[key])) continue;

				this.shared[key] = _props.shared_props[key];
			}

			for (const key in _props.props) {
				if (this._is_i18n_managed(`props.${key}`, _props.props[key])) continue;

				this.props[key] = _props.props[key];
			}

			this.register_component(
				_props.shared_props.id,
				// @ts-ignore
				this.set_data.bind(this),
				this.get_data.bind(this)
			);

			untrack(() => {
				this.shared.id = _props.shared_props.id;
			});
		});

		if (Object.keys(this.translatable_props).length > 0) {
			$locale.subscribe(() => {
				for (const [full_key, original] of Object.entries(this.translatable_props)) {
					const [target, key] = full_key.split(".");
					const translated = this.i18n(original);

					if (target === "shared") this.shared[key] = translated; else this.props[key] = translated;
				}
			});
		}
	}

	// check if props are translatable
	_is_i18n_managed(key, new_value) {
		const original_marker = this.translatable_props[key];

		if (!original_marker) return false;
		if (new_value === original_marker) return true;

		delete this.translatable_props[key];

		return false;
	}

	_translate_and_store(target, key, value) {
		if (typeof value !== "string") return value;

		const translated = this.i18n(value);

		if (translated !== value) {
			this.translatable_props[`${target}.${key}`] = value;
		}

		return translated;
	}

	dispatch(event_name, data) {
		this.dispatcher(this.shared.id, event_name, data);
	}

	async get_data() {
		return snapshot(this.props);
	}

	update(data) {
		this.set_data(data);
	}

	set_data(data) {
		for (const key in data) {
			const value = data[key];

			const translated = has_i18n_marker(value)
				? this._translate_and_store(this.shared_props.includes(key) ? "shared" : "props", key, value)
				: value;

			if (this.shared_props.includes(key)) {
				const _key = key;

				this.shared[_key] = translated;

				continue;
			}

			this.props[key] = translated;
		}
	}

	watch_for_change() {
		user_effect(() => {
			if (!this.mounted) {
				this.old_value = this.props.value;
				this.mounted = true;
			}

			if (this.old_value != this.props.value) {
				this.old_value = this.props.value;
				this.dispatch("change");
			}
		});
	}
}

const css_units = (dimension_value) => {
	return typeof dimension_value === "number" ? dimension_value + "px" : dimension_value;
};

function should_show_scroll_fade(container) {
	if (!container) return false;

	const has_overflow = container.scrollHeight > container.clientHeight;
	const at_bottom = container.scrollTop >= container.scrollHeight - container.clientHeight - 1;

	return has_overflow && !at_bottom;
}

export { Gradio as G, I18N_MARKER as I, ShareError as S, copy as a, allowed_shared_props as b, css_units as c, format_time as f, should_show_scroll_fade as s, translate_i18n_marker as t, uploadToHuggingFace as u };
//# sourceMappingURL=utils.svelte-CYLB_jIA.js.map
