import { w as writable } from './Index-f62c49f9.js';

const Tabs_svelte_svelte_type_style_lang = '';

/* home/runner/work/gradio/gradio/js/tabs/shared/Tabs.svelte generated by Svelte v4.2.2 */
const {
	SvelteComponent,
	append,
	attr,
	component_subscribe,
	create_slot,
	destroy_block,
	detach,
	element,
	empty,
	ensure_array_like,
	get_all_dirty_from_scope,
	get_slot_changes,
	init,
	insert,
	listen,
	safe_not_equal,
	set_data,
	set_store_value,
	space,
	text,
	toggle_class,
	transition_in,
	transition_out,
	update_keyed_each,
	update_slot_base
} = window.__gradio__svelte__internal;
const { setContext, createEventDispatcher, tick } = window.__gradio__svelte__internal;
function get_each_context(ctx, list, i) {
	const child_ctx = ctx.slice();
	child_ctx[14] = list[i];
	child_ctx[16] = i;
	return child_ctx;
}

// (46:3) {:else}
function create_else_block(ctx) {
	let button;
	let t0_value = /*t*/ ctx[14].name + "";
	let t0;
	let t1;
	let button_id_value;
	let mounted;
	let dispose;

	function click_handler() {
		return /*click_handler*/ ctx[12](/*t*/ ctx[14], /*i*/ ctx[16]);
	}

	return {
		c() {
			button = element("button");
			t0 = text(t0_value);
			t1 = space();

			attr(button, "id", button_id_value = /*t*/ ctx[14].elem_id
			? /*t*/ ctx[14].elem_id + "-button"
			: null);

			attr(button, "class", "svelte-kqij2n");
		},
		m(target, anchor) {
			insert(target, button, anchor);
			append(button, t0);
			append(button, t1);

			if (!mounted) {
				dispose = listen(button, "click", click_handler);
				mounted = true;
			}
		},
		p(new_ctx, dirty) {
			ctx = new_ctx;
			if (dirty & /*tabs*/ 8 && t0_value !== (t0_value = /*t*/ ctx[14].name + "")) set_data(t0, t0_value);

			if (dirty & /*tabs*/ 8 && button_id_value !== (button_id_value = /*t*/ ctx[14].elem_id
			? /*t*/ ctx[14].elem_id + "-button"
			: null)) {
				attr(button, "id", button_id_value);
			}
		},
		d(detaching) {
			if (detaching) {
				detach(button);
			}

			mounted = false;
			dispose();
		}
	};
}

// (42:3) {#if t.id === $selected_tab}
function create_if_block(ctx) {
	let button;
	let t0_value = /*t*/ ctx[14].name + "";
	let t0;
	let t1;
	let button_id_value;

	return {
		c() {
			button = element("button");
			t0 = text(t0_value);
			t1 = space();
			attr(button, "class", "selected svelte-kqij2n");

			attr(button, "id", button_id_value = /*t*/ ctx[14].elem_id
			? /*t*/ ctx[14].elem_id + "-button"
			: null);
		},
		m(target, anchor) {
			insert(target, button, anchor);
			append(button, t0);
			append(button, t1);
		},
		p(ctx, dirty) {
			if (dirty & /*tabs*/ 8 && t0_value !== (t0_value = /*t*/ ctx[14].name + "")) set_data(t0, t0_value);

			if (dirty & /*tabs*/ 8 && button_id_value !== (button_id_value = /*t*/ ctx[14].elem_id
			? /*t*/ ctx[14].elem_id + "-button"
			: null)) {
				attr(button, "id", button_id_value);
			}
		},
		d(detaching) {
			if (detaching) {
				detach(button);
			}
		}
	};
}

// (41:2) {#each tabs as t, i (t.id)}
function create_each_block(key_1, ctx) {
	let first;
	let if_block_anchor;

	function select_block_type(ctx, dirty) {
		if (/*t*/ ctx[14].id === /*$selected_tab*/ ctx[4]) return create_if_block;
		return create_else_block;
	}

	let current_block_type = select_block_type(ctx);
	let if_block = current_block_type(ctx);

	return {
		key: key_1,
		first: null,
		c() {
			first = empty();
			if_block.c();
			if_block_anchor = empty();
			this.first = first;
		},
		m(target, anchor) {
			insert(target, first, anchor);
			if_block.m(target, anchor);
			insert(target, if_block_anchor, anchor);
		},
		p(new_ctx, dirty) {
			ctx = new_ctx;

			if (current_block_type === (current_block_type = select_block_type(ctx)) && if_block) {
				if_block.p(ctx, dirty);
			} else {
				if_block.d(1);
				if_block = current_block_type(ctx);

				if (if_block) {
					if_block.c();
					if_block.m(if_block_anchor.parentNode, if_block_anchor);
				}
			}
		},
		d(detaching) {
			if (detaching) {
				detach(first);
				detach(if_block_anchor);
			}

			if_block.d(detaching);
		}
	};
}

function create_fragment(ctx) {
	let div1;
	let div0;
	let each_blocks = [];
	let each_1_lookup = new Map();
	let t_1;
	let div1_class_value;
	let current;
	let each_value = ensure_array_like(/*tabs*/ ctx[3]);
	const get_key = ctx => /*t*/ ctx[14].id;

	for (let i = 0; i < each_value.length; i += 1) {
		let child_ctx = get_each_context(ctx, each_value, i);
		let key = get_key(child_ctx);
		each_1_lookup.set(key, each_blocks[i] = create_each_block(key, child_ctx));
	}

	const default_slot_template = /*#slots*/ ctx[11].default;
	const default_slot = create_slot(default_slot_template, ctx, /*$$scope*/ ctx[10], null);

	return {
		c() {
			div1 = element("div");
			div0 = element("div");

			for (let i = 0; i < each_blocks.length; i += 1) {
				each_blocks[i].c();
			}

			t_1 = space();
			if (default_slot) default_slot.c();
			attr(div0, "class", "tab-nav scroll-hide svelte-kqij2n");
			attr(div1, "class", div1_class_value = "tabs " + /*elem_classes*/ ctx[2].join(' ') + " svelte-kqij2n");
			attr(div1, "id", /*elem_id*/ ctx[1]);
			toggle_class(div1, "hide", !/*visible*/ ctx[0]);
		},
		m(target, anchor) {
			insert(target, div1, anchor);
			append(div1, div0);

			for (let i = 0; i < each_blocks.length; i += 1) {
				if (each_blocks[i]) {
					each_blocks[i].m(div0, null);
				}
			}

			append(div1, t_1);

			if (default_slot) {
				default_slot.m(div1, null);
			}

			current = true;
		},
		p(ctx, [dirty]) {
			if (dirty & /*tabs, $selected_tab, change_tab, dispatch*/ 408) {
				each_value = ensure_array_like(/*tabs*/ ctx[3]);
				each_blocks = update_keyed_each(each_blocks, dirty, get_key, 1, ctx, each_value, each_1_lookup, div0, destroy_block, create_each_block, null, get_each_context);
			}

			if (default_slot) {
				if (default_slot.p && (!current || dirty & /*$$scope*/ 1024)) {
					update_slot_base(
						default_slot,
						default_slot_template,
						ctx,
						/*$$scope*/ ctx[10],
						!current
						? get_all_dirty_from_scope(/*$$scope*/ ctx[10])
						: get_slot_changes(default_slot_template, /*$$scope*/ ctx[10], dirty, null),
						null
					);
				}
			}

			if (!current || dirty & /*elem_classes*/ 4 && div1_class_value !== (div1_class_value = "tabs " + /*elem_classes*/ ctx[2].join(' ') + " svelte-kqij2n")) {
				attr(div1, "class", div1_class_value);
			}

			if (!current || dirty & /*elem_id*/ 2) {
				attr(div1, "id", /*elem_id*/ ctx[1]);
			}

			if (!current || dirty & /*elem_classes, visible*/ 5) {
				toggle_class(div1, "hide", !/*visible*/ ctx[0]);
			}
		},
		i(local) {
			if (current) return;
			transition_in(default_slot, local);
			current = true;
		},
		o(local) {
			transition_out(default_slot, local);
			current = false;
		},
		d(detaching) {
			if (detaching) {
				detach(div1);
			}

			for (let i = 0; i < each_blocks.length; i += 1) {
				each_blocks[i].d();
			}

			if (default_slot) default_slot.d(detaching);
		}
	};
}

const TABS = {};

function instance($$self, $$props, $$invalidate) {
	let $selected_tab_index;
	let $selected_tab;
	let { $$slots: slots = {}, $$scope } = $$props;
	let { visible = true } = $$props;
	let { elem_id = "id" } = $$props;
	let { elem_classes = [] } = $$props;
	let { selected } = $$props;
	let tabs = [];
	const selected_tab = writable(false);
	component_subscribe($$self, selected_tab, value => $$invalidate(4, $selected_tab = value));
	const selected_tab_index = writable(0);
	component_subscribe($$self, selected_tab_index, value => $$invalidate(13, $selected_tab_index = value));
	const dispatch = createEventDispatcher();

	setContext(TABS, {
		register_tab: tab => {
			tabs.push({
				name: tab.name,
				id: tab.id,
				elem_id: tab.elem_id
			});

			selected_tab.update(current => current ?? tab.id);
			$$invalidate(3, tabs);
			return tabs.length - 1;
		},
		unregister_tab: tab => {
			const i = tabs.findIndex(t => t.id === tab.id);
			tabs.splice(i, 1);

			selected_tab.update(current => current === tab.id
			? tabs[i]?.id || tabs[tabs.length - 1]?.id
			: current);
		},
		selected_tab,
		selected_tab_index
	});

	function change_tab(id) {
		$$invalidate(9, selected = id);
		set_store_value(selected_tab, $selected_tab = id, $selected_tab);
		set_store_value(selected_tab_index, $selected_tab_index = tabs.findIndex(t => t.id === id), $selected_tab_index);
		dispatch("change");
	}

	const click_handler = (t, i) => {
		change_tab(t.id);
		dispatch("select", { value: t.name, index: i });
	};

	$$self.$$set = $$props => {
		if ('visible' in $$props) $$invalidate(0, visible = $$props.visible);
		if ('elem_id' in $$props) $$invalidate(1, elem_id = $$props.elem_id);
		if ('elem_classes' in $$props) $$invalidate(2, elem_classes = $$props.elem_classes);
		if ('selected' in $$props) $$invalidate(9, selected = $$props.selected);
		if ('$$scope' in $$props) $$invalidate(10, $$scope = $$props.$$scope);
	};

	$$self.$$.update = () => {
		if ($$self.$$.dirty & /*selected*/ 512) {
			selected !== null && change_tab(selected);
		}
	};

	return [
		visible,
		elem_id,
		elem_classes,
		tabs,
		$selected_tab,
		selected_tab,
		selected_tab_index,
		dispatch,
		change_tab,
		selected,
		$$scope,
		slots,
		click_handler
	];
}

class Tabs extends SvelteComponent {
	constructor(options) {
		super();

		init(this, options, instance, create_fragment, safe_not_equal, {
			visible: 0,
			elem_id: 1,
			elem_classes: 2,
			selected: 9
		});
	}
}

export { Tabs as T, TABS as a };
//# sourceMappingURL=Tabs-cdccf481.js.map
