import { useActiveEntities } from "../../store/active-entities.js";
import { useWorkspace } from "../../store/store.js";
import ServerVariablesForm_default from "../../components/Server/ServerVariablesForm.vue.js";
import Form_default from "../../components/Form/Form.vue.js";
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createVNode, defineComponent, normalizeClass, openBlock, watch } from "vue";
import { REGEX } from "@scalar/oas-utils/helpers";
//#region src/views/Collection/CollectionServerForm.vue?vue&type=script&setup=true&lang.ts
var CollectionServerForm_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "CollectionServerForm",
	props: {
		collectionId: { default: "" },
		serverUid: { default: "" }
	},
	setup(__props) {
		const props = __props;
		const { activeWorkspaceCollections } = useActiveEntities();
		const { servers, serverMutators } = useWorkspace();
		const options = [{
			label: "URL",
			key: "url",
			placeholder: "https://void.scalar.com",
			type: "text"
		}, {
			label: "Description",
			key: "description",
			placeholder: "Production",
			type: "text"
		}];
		const activeServer = computed(() => {
			const activeCollection = activeWorkspaceCollections.value.find((collection) => collection.uid === props.collectionId);
			return servers[activeCollection && typeof props.serverUid === "string" && props.serverUid === "default" ? activeCollection.servers[0] ?? "" : activeCollection?.servers.find((uid) => uid === props.serverUid) ?? ""];
		});
		watch(computed(() => {
			if (!activeServer.value?.url) return [];
			return activeServer.value.url.match(REGEX.PATH)?.map((m) => m.slice(1, -1)) ?? [];
		}), (newPathVariables) => {
			if (!activeServer.value) return;
			const variables = activeServer.value.variables ? { ...activeServer.value.variables } : {};
			Object.keys(variables).forEach((key) => {
				if (!newPathVariables.includes(key)) delete variables[key];
			});
			newPathVariables.forEach((variable) => {
				if (!variables[variable]) variables[variable] = { default: "" };
			});
			serverMutators.edit(activeServer.value.uid, "variables", variables);
		}, { immediate: true });
		const updateServer = (key, value) => {
			if (!activeWorkspaceCollections.value || !activeServer.value) return;
			serverMutators.edit(activeServer.value.uid, key, value);
		};
		const updateServerVariable = (key, value) => {
			if (!activeServer.value) return;
			const variables = activeServer.value.variables || {};
			variables[key] = {
				...variables[key],
				default: value
			};
			serverMutators.edit(activeServer.value.uid, "variables", variables);
		};
		return (_ctx, _cache) => {
			return openBlock(), createElementBlock("div", { class: normalizeClass(["divide-0.5 flex w-full flex-col divide-y rounded-b-lg text-sm", activeServer.value?.variables && "bg-b-1"]) }, [activeServer.value ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [createVNode(Form_default, {
				data: activeServer.value,
				onUpdate: updateServer,
				options
			}, null, 8, ["data"]), activeServer.value.variables ? (openBlock(), createBlock(ServerVariablesForm_default, {
				key: 0,
				variables: activeServer.value.variables,
				"onUpdate:variable": updateServerVariable
			}, null, 8, ["variables"])) : createCommentVNode("", true)], 64)) : createCommentVNode("", true)], 2);
		};
	}
});
//#endregion
export { CollectionServerForm_vue_vue_type_script_setup_true_lang_default as default };

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