import CodeInput_default from "../../../components/code-input/CodeInput.vue.js";
import DataTable_default from "../../../components/data-table/DataTable.vue.js";
import DataTableCell_default from "../../../components/data-table/DataTableCell.vue.js";
import DataTableCheckbox_default from "../../../components/data-table/DataTableCheckbox.vue.js";
import DataTableHeader_default from "../../../components/data-table/DataTableHeader.vue.js";
import DataTableRow_default from "../../../components/data-table/DataTableRow.vue.js";
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createTextVNode, createVNode, defineComponent, openBlock, renderList, unref, withCtx } from "vue";
import { ScalarButton } from "@scalar/components";
import { ScalarIconTrash } from "@scalar/icons";
//#region src/v2/features/global-cookies/components/CookiesTable.vue?vue&type=script&setup=true&lang.ts
var CookiesTable_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "CookiesTable",
	props: {
		cookies: {},
		eventBus: {},
		collectionType: {}
	},
	setup(__props) {
		/** Column widths: Enabled checkbox, Name, Value, Domain, Actions (delete button) */
		const COLUMNS = [
			"32px",
			"1fr",
			"1fr",
			"1fr",
			"36px"
		];
		/** Adds an empty row at the end for creating new cookies */
		const displayRows = computed(() => {
			/** Normalize the cookies to have name, value, and domain */
			const cookieRows = __props.cookies.map((cookie) => ({
				name: cookie.name,
				value: cookie.value,
				domain: cookie.domain ?? "",
				isDisabled: cookie.isDisabled ?? false
			}));
			const lastCookie = __props.cookies.at(-1);
			const hasContentInLastRow = lastCookie && (lastCookie.name !== "" || lastCookie.value !== "" || lastCookie.domain !== "");
			if (!lastCookie || hasContentInLastRow) return [...cookieRows, {
				name: "",
				value: "",
				domain: "",
				isDisabled: true
			}];
			return cookieRows;
		});
		/** Updates an existing cookie or creates a new one */
		const handleCookieChange = (name, value, domain, isDisabled, index) => {
			const isNewCookie = index >= __props.cookies.length;
			/**
			* Do not allow adding a new cookie with an empty name on the last row,
			* we allow it on other rows as it will delete the row
			*/
			if (!name && isNewCookie) return;
			if (index >= __props.cookies.length) {
				__props.eventBus.emit("cookie:upsert:cookie", {
					payload: {
						name,
						value,
						domain,
						isDisabled: false
					},
					collectionType: __props.collectionType
				});
				return;
			}
			__props.eventBus.emit("cookie:upsert:cookie", {
				payload: {
					name,
					value,
					domain,
					isDisabled
				},
				collectionType: __props.collectionType,
				index
			});
		};
		/** Deletes a cookie by index */
		const handleCookieDelete = (index) => __props.eventBus.emit("cookie:delete:cookie", {
			cookieName: __props.cookies[index]?.name ?? "",
			index,
			collectionType: __props.collectionType
		});
		return (_ctx, _cache) => {
			return openBlock(), createBlock(unref(DataTable_default), {
				class: "group/table data-table h-min flex-1 rounded border border-t-0",
				columns: COLUMNS
			}, {
				default: withCtx(() => [createVNode(unref(DataTableRow_default), { class: "sr-only !block" }, {
					default: withCtx(() => [
						createVNode(unref(DataTableHeader_default), null, {
							default: withCtx(() => [..._cache[0] || (_cache[0] = [createTextVNode("Enabled", -1)])]),
							_: 1
						}),
						createVNode(unref(DataTableHeader_default), null, {
							default: withCtx(() => [..._cache[1] || (_cache[1] = [createTextVNode("Name", -1)])]),
							_: 1
						}),
						createVNode(unref(DataTableHeader_default), null, {
							default: withCtx(() => [..._cache[2] || (_cache[2] = [createTextVNode("Value", -1)])]),
							_: 1
						}),
						createVNode(unref(DataTableHeader_default), null, {
							default: withCtx(() => [..._cache[3] || (_cache[3] = [createTextVNode("Domain", -1)])]),
							_: 1
						}),
						createVNode(unref(DataTableHeader_default), null, {
							default: withCtx(() => [..._cache[4] || (_cache[4] = [createTextVNode("Actions", -1)])]),
							_: 1
						})
					]),
					_: 1
				}), (openBlock(true), createElementBlock(Fragment, null, renderList(displayRows.value, (row, index) => {
					return openBlock(), createBlock(unref(DataTableRow_default), {
						key: index,
						class: "group/row"
					}, {
						default: withCtx(() => [
							createVNode(unref(DataTableCheckbox_default), {
								class: "!border-r",
								modelValue: !row.isDisabled,
								"onUpdate:modelValue": (value) => handleCookieChange(row.name, row.value, row.domain, !value, index)
							}, null, 8, ["modelValue", "onUpdate:modelValue"]),
							createVNode(unref(DataTableCell_default), null, {
								default: withCtx(() => [createVNode(unref(CodeInput_default), {
									"aria-label": "Cookie Name",
									disableCloseBrackets: "",
									disableTabIndent: "",
									environment: void 0,
									lineWrapping: "",
									modelValue: row.name,
									placeholder: "Name",
									"onUpdate:modelValue": (name) => handleCookieChange(name, row.value, row.domain, row.isDisabled, index)
								}, null, 8, ["modelValue", "onUpdate:modelValue"])]),
								_: 2
							}, 1024),
							createVNode(unref(DataTableCell_default), null, {
								default: withCtx(() => [createVNode(unref(CodeInput_default), {
									"aria-label": "Cookie Value",
									disableTabIndent: "",
									environment: void 0,
									lineWrapping: "",
									modelValue: row.value,
									placeholder: "Value",
									"onUpdate:modelValue": (value) => handleCookieChange(row.name, value, row.domain, row.isDisabled, index)
								}, null, 8, ["modelValue", "onUpdate:modelValue"])]),
								_: 2
							}, 1024),
							createVNode(unref(DataTableCell_default), null, {
								default: withCtx(() => [createVNode(unref(CodeInput_default), {
									"aria-label": "Cookie Domain",
									disableCloseBrackets: "",
									disableTabIndent: "",
									environment: void 0,
									lineWrapping: "",
									modelValue: row.domain,
									placeholder: "Domain",
									"onUpdate:modelValue": (domain) => handleCookieChange(row.name, row.value, domain, row.isDisabled, index)
								}, null, 8, ["modelValue", "onUpdate:modelValue"])]),
								_: 2
							}, 1024),
							createVNode(unref(DataTableCell_default), { class: "flex items-center justify-center" }, {
								default: withCtx(() => [index < __props.cookies.length ? (openBlock(), createBlock(unref(ScalarButton), {
									key: 0,
									class: "text-c-2 hover:text-c-1 hover:bg-b-2 hidden h-fit rounded p-1 group-focus-within:flex group-hover/row:flex",
									size: "sm",
									variant: "ghost",
									onClick: ($event) => handleCookieDelete(index)
								}, {
									default: withCtx(() => [createVNode(unref(ScalarIconTrash), { class: "size-3.5" })]),
									_: 1
								}, 8, ["onClick"])) : createCommentVNode("", true)]),
								_: 2
							}, 1024)
						]),
						_: 2
					}, 1024);
				}), 128))]),
				_: 1
			});
		};
	}
});
//#endregion
export { CookiesTable_vue_vue_type_script_setup_true_lang_default as default };

//# sourceMappingURL=CookiesTable.vue.script.js.map