import { useWorkspace } from "../../../store/store.js";
import ViewLayoutCollapse_default from "../../../components/ViewLayout/ViewLayoutCollapse.vue.js";
import RequestTable_default from "./RequestTable.vue.js";
import { computed, createBlock, createCommentVNode, createTextVNode, defineComponent, openBlock, toDisplayString, watch, withCtx } from "vue";
import { REGEX } from "@scalar/oas-utils/helpers";
//#region src/views/Request/RequestSection/RequestPathParams.vue?vue&type=script&setup=true&lang.ts
var RequestPathParams_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "RequestPathParams",
	props: {
		example: {},
		operation: {},
		paramKey: {},
		title: {},
		environment: {},
		envVariables: {},
		workspace: {},
		invalidParams: {}
	},
	setup(__props) {
		const { requestMutators, requestExampleMutators } = useWorkspace();
		const params = computed(() => __props.example.parameters[__props.paramKey].map((param) => ({
			...param,
			enum: param.enum
		})));
		/** Update a field in a parameter row */
		const updateRow = (rowIdx, field, value) => {
			const parameters = __props.example.parameters[__props.paramKey];
			const oldKey = parameters[rowIdx]?.key;
			if (!oldKey) return;
			/** Change variable in path as well */
			if (field === "key") {
				if (parameters[rowIdx]?.required)
 /** Prevent updating the key of a required item */
				return;
				if (!value) {
					/** Remove parameter if path params table key is empty */
					parameters.splice(rowIdx, 1);
					const regx = new RegExp(`/:${encodeURIComponent(oldKey)}(?=[/?#]|$)`, "g");
					const newPath = __props.operation.path.replace(regx, "");
					requestMutators.edit(__props.operation.uid, "path", newPath);
				} else {
					/** Update URL with path params table key */
					const encodedOldKey = encodeURIComponent(oldKey);
					const encodedNewKey = encodeURIComponent(value);
					const regx = new RegExp(`(?<=/):${encodedOldKey}(?=[/?#]|$)`, "g");
					const newPath = __props.operation.path.replace(regx, `:${encodedNewKey}`);
					requestMutators.edit(__props.operation.uid, "path", newPath);
				}
			}
			requestExampleMutators.edit(__props.example.uid, `parameters.${__props.paramKey}.${rowIdx}.${field}`, value);
		};
		const deleteRow = (rowIdx) => {
			const currentParams = params.value;
			if (currentParams.length > rowIdx) {
				const updatedParams = [...currentParams];
				updatedParams.splice(rowIdx, 1);
				requestExampleMutators.edit(__props.example.uid, `parameters.${__props.paramKey}`, updatedParams);
			}
		};
		const setPathVariable = (url) => {
			const pathVariables = url.match(REGEX.PATH)?.map((v) => v.slice(1, -1)) || [];
			const parameters = __props.example.parameters[__props.paramKey];
			const paramMap = new Map(parameters.map((param) => [param.key, param]));
			const updatedParameters = pathVariables.map((key) => paramMap.get(key) || {
				key,
				value: "",
				enabled: true
			});
			parameters.forEach((param) => {
				/** prevent removing required parameters or with a value */
				if (!pathVariables.includes(param.key) && (param.value || param.required)) updatedParameters.push(param);
			});
			parameters.splice(0, parameters.length, ...updatedParameters);
			requestExampleMutators.edit(__props.example.uid, `parameters.${__props.paramKey}`, parameters);
		};
		const handlePathVariableUpdate = (url) => {
			if (url) setPathVariable(url);
		};
		watch(() => __props.operation.path, (newURL) => {
			if (newURL) handlePathVariableUpdate(newURL);
		});
		return (_ctx, _cache) => {
			return openBlock(), createBlock(ViewLayoutCollapse_default, { itemCount: params.value.length }, {
				title: withCtx(() => [createTextVNode(toDisplayString(__props.title), 1)]),
				default: withCtx(() => [params.value.length ? (openBlock(), createBlock(RequestTable_default, {
					key: 0,
					class: "flex-1",
					columns: [
						"32px",
						"",
						""
					],
					envVariables: __props.envVariables,
					environment: __props.environment,
					invalidParams: __props.invalidParams,
					items: params.value,
					workspace: __props.workspace,
					onUpdateRow: updateRow,
					onDeleteRow: deleteRow
				}, null, 8, [
					"envVariables",
					"environment",
					"invalidParams",
					"items",
					"workspace"
				])) : createCommentVNode("", true)]),
				_: 1
			}, 8, ["itemCount"]);
		};
	}
});
//#endregion
export { RequestPathParams_vue_vue_type_script_setup_true_lang_default as default };

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