import { useLayout } from "../../../hooks/useLayout.js";
import { usePluginManager } from "../../../plugins/hooks/usePluginManager.js";
import { matchesDomain } from "../../../libs/send-request/set-request-cookies.js";
import { useWorkspace } from "../../../store/store.js";
import SectionFilter_default from "../../../components/SectionFilter.vue.js";
import ViewLayoutSection_default from "../../../components/ViewLayout/ViewLayoutSection.vue.js";
import RequestBody_default from "./RequestBody.vue.js";
import RequestParams_default from "./RequestParams.vue.js";
import RequestPathParams_default from "./RequestPathParams.vue.js";
import RequestAuth_default from "./RequestAuth/RequestAuth.vue.js";
import RequestCodeExample_default from "./RequestCodeExample.vue.js";
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createVNode, defineComponent, openBlock, ref, renderList, resolveDynamicComponent, toDisplayString, unref, useId, vShow, watch, withCtx, withDirectives } from "vue";
import { ScalarErrorBoundary } from "@scalar/components";
import { REGEX, canMethodHaveBody, isDefined } from "@scalar/oas-utils/helpers";
//#region src/views/Request/RequestSection/RequestSection.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "group pointer-events-none flex flex-1 items-center gap-1 lg:pr-24" };
var _hoisted_2 = ["for"];
var _hoisted_3 = [
	"id",
	"placeholder",
	"value"
];
var _hoisted_4 = {
	key: 2,
	class: "text-c-1 flex h-8 items-center"
};
var _hoisted_5 = ["id", "role"];
var RequestSection_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "RequestSection",
	props: {
		collection: {},
		environment: {},
		envVariables: {},
		example: {},
		invalidParams: {},
		operation: {},
		selectedSecuritySchemeUids: {},
		server: {},
		workspace: {}
	},
	setup(__props) {
		const requestSections = [
			"Auth",
			"Path Parameters",
			"Cookies",
			"Headers",
			"Query",
			"Body"
		];
		const { requestMutators, cookies, securitySchemes } = useWorkspace();
		const { layout } = useLayout();
		const filters = computed(() => {
			const allSections = new Set(["All", ...requestSections]);
			if (!__props.example.parameters.path.length) allSections.delete("Path Parameters");
			if (!canMethodHaveBody(__props.operation.method ?? "get")) allSections.delete("Body");
			if (isAuthHidden.value) allSections.delete("Auth");
			return [...allSections];
		});
		/** A list of section ids */
		const filterIds = computed(() => Object.fromEntries(filters.value.map((section) => [section, useId()])));
		const isAuthHidden = computed(() => layout === "modal" && !__props.operation.security && !Object.keys(securitySchemes ?? {}).length);
		const selectedFilter = ref("All");
		watch(() => __props.operation, (newOperation) => {
			if (selectedFilter.value === "Body" && newOperation && !canMethodHaveBody(newOperation.method)) selectedFilter.value = "All";
		});
		const updateRequestNameHandler = (event) => {
			const target = event.target;
			requestMutators.edit(__props.operation.uid, "summary", target.value);
		};
		/**
		* Add the global cookies as static entries to the cookies section
		*/
		const activeWorkspaceCookies = computed(() => (__props.workspace.cookies ?? []).map((uid) => cookies[uid]).filter(isDefined).filter((cookie) => cookie.name).filter((cookie) => matchesDomain(__props.server?.url || __props.operation.path, cookie.domain)).map((cookie) => ({
			key: cookie.name,
			value: cookie.value,
			route: {
				name: "cookies",
				params: { cookies: cookie.uid }
			},
			enabled: true
		})));
		const handleRequestNamePlaceholder = () => {
			return __props.operation.summary ? __props.operation.summary : __props.operation.path.replace(REGEX.PROTOCOL, "") ? __props.operation.path.replace(REGEX.PROTOCOL, "") : "Request Name";
		};
		const labelRequestNameId = useId();
		const requestSectionViews = usePluginManager().getViewComponents("request.section");
		const updateOperationHandler = (key, value) => requestMutators.edit(__props.operation.uid, key, value);
		watch(() => isAuthHidden.value, (authHidden) => {
			if (authHidden && selectedFilter.value === "Auth") selectedFilter.value = "All";
		});
		return (_ctx, _cache) => {
			return openBlock(), createBlock(ViewLayoutSection_default, { "aria-label": `Request: ${__props.operation.summary}` }, {
				title: withCtx(() => [createElementVNode("div", _hoisted_1, [unref(layout) !== "modal" ? (openBlock(), createElementBlock("label", {
					key: 0,
					class: "pointer-events-auto absolute top-0 left-0 h-full w-full cursor-text opacity-0",
					for: unref(labelRequestNameId)
				}, null, 8, _hoisted_2)) : createCommentVNode("", true), unref(layout) !== "modal" ? (openBlock(), createElementBlock("input", {
					key: 1,
					id: unref(labelRequestNameId),
					class: "text-c-1 group-hover-input pointer-events-auto relative z-10 -ml-0.5 h-8 w-full rounded pl-1.25 has-[:focus-visible]:outline md:-ml-1.25",
					placeholder: handleRequestNamePlaceholder(),
					value: __props.operation.summary,
					onInput: updateRequestNameHandler
				}, null, 40, _hoisted_3)) : (openBlock(), createElementBlock("span", _hoisted_4, toDisplayString(__props.operation.summary), 1))]), createVNode(SectionFilter_default, {
					modelValue: selectedFilter.value,
					"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => selectedFilter.value = $event),
					filterIds: filterIds.value,
					filters: filters.value
				}, null, 8, [
					"modelValue",
					"filterIds",
					"filters"
				])]),
				default: withCtx(() => [createElementVNode("div", {
					id: filterIds.value.All,
					class: "request-section-content custom-scroll relative flex flex-1 flex-col",
					role: selectedFilter.value === "All" ? "tabpanel" : "none"
				}, [
					__props.collection && __props.workspace && (unref(layout) !== "modal" || Object.keys(unref(securitySchemes) ?? {}).length) ? withDirectives((openBlock(), createBlock(RequestAuth_default, {
						key: 0,
						id: filterIds.value.Auth,
						class: "request-section-content-auth",
						collection: __props.collection,
						envVariables: __props.envVariables,
						environment: __props.environment,
						isReadOnly: unref(layout) === "modal",
						layout: "client",
						operation: __props.operation,
						role: selectedFilter.value === "All" ? "none" : "tabpanel",
						selectedSecuritySchemeUids: __props.selectedSecuritySchemeUids,
						server: __props.server,
						title: "Authentication",
						workspace: __props.workspace
					}, null, 8, [
						"id",
						"collection",
						"envVariables",
						"environment",
						"isReadOnly",
						"operation",
						"role",
						"selectedSecuritySchemeUids",
						"server",
						"workspace"
					])), [[vShow, !isAuthHidden.value && (selectedFilter.value === "All" || selectedFilter.value === "Auth")]]) : createCommentVNode("", true),
					withDirectives(createVNode(RequestPathParams_default, {
						id: filterIds.value["Path Parameters"],
						class: "request-section-content-path-params",
						envVariables: __props.envVariables,
						environment: __props.environment,
						example: __props.example,
						invalidParams: __props.invalidParams,
						operation: __props.operation,
						paramKey: "path",
						role: selectedFilter.value === "All" ? "none" : "tabpanel",
						title: "Path Parameters",
						workspace: __props.workspace
					}, null, 8, [
						"id",
						"envVariables",
						"environment",
						"example",
						"invalidParams",
						"operation",
						"role",
						"workspace"
					]), [[vShow, (selectedFilter.value === "All" || selectedFilter.value === "Path Parameters") && __props.example.parameters.path.length]]),
					withDirectives(createVNode(RequestParams_default, {
						id: filterIds.value.Cookies,
						class: "request-section-content-cookies",
						envVariables: __props.envVariables,
						environment: __props.environment,
						example: __props.example,
						invalidParams: __props.invalidParams,
						label: "Cookie",
						operation: __props.operation,
						paramKey: "cookies",
						readOnlyEntries: activeWorkspaceCookies.value,
						role: selectedFilter.value === "All" ? "none" : "tabpanel",
						title: "Cookies",
						workspace: __props.workspace
					}, null, 8, [
						"id",
						"envVariables",
						"environment",
						"example",
						"invalidParams",
						"operation",
						"readOnlyEntries",
						"role",
						"workspace"
					]), [[vShow, selectedFilter.value === "All" || selectedFilter.value === "Cookies"]]),
					withDirectives(createVNode(RequestParams_default, {
						id: filterIds.value.Headers,
						class: "request-section-content-headers",
						envVariables: __props.envVariables,
						environment: __props.environment,
						example: __props.example,
						invalidParams: __props.invalidParams,
						label: "Header",
						operation: __props.operation,
						paramKey: "headers",
						role: selectedFilter.value === "All" ? "none" : "tabpanel",
						title: "Headers",
						workspace: __props.workspace
					}, null, 8, [
						"id",
						"envVariables",
						"environment",
						"example",
						"invalidParams",
						"operation",
						"role",
						"workspace"
					]), [[vShow, selectedFilter.value === "All" || selectedFilter.value === "Headers"]]),
					withDirectives(createVNode(RequestParams_default, {
						id: filterIds.value.Query,
						class: "request-section-content-query",
						envVariables: __props.envVariables,
						environment: __props.environment,
						example: __props.example,
						invalidParams: __props.invalidParams,
						label: "Parameter",
						operation: __props.operation,
						paramKey: "query",
						role: selectedFilter.value === "All" ? "none" : "tabpanel",
						title: "Query Parameters",
						workspace: __props.workspace
					}, null, 8, [
						"id",
						"envVariables",
						"environment",
						"example",
						"invalidParams",
						"operation",
						"role",
						"workspace"
					]), [[vShow, selectedFilter.value === "All" || selectedFilter.value === "Query"]]),
					__props.operation.method && (selectedFilter.value === "All" || selectedFilter.value === "Body") && unref(canMethodHaveBody)(__props.operation.method) ? (openBlock(), createBlock(RequestBody_default, {
						key: 1,
						id: filterIds.value.Body,
						class: "request-section-content-body",
						envVariables: __props.envVariables,
						environment: __props.environment,
						example: __props.example,
						operation: __props.operation,
						role: selectedFilter.value === "All" ? "none" : "tabpanel",
						title: "Body",
						workspace: __props.workspace
					}, null, 8, [
						"id",
						"envVariables",
						"environment",
						"example",
						"operation",
						"role",
						"workspace"
					])) : createCommentVNode("", true),
					(openBlock(true), createElementBlock(Fragment, null, renderList(unref(requestSectionViews), (view, index) => {
						return openBlock(), createBlock(unref(ScalarErrorBoundary), { key: view.title ?? index }, {
							default: withCtx(() => [withDirectives((openBlock(), createBlock(resolveDynamicComponent(view.component), {
								operation: __props.operation,
								"onUpdate:operation": updateOperationHandler
							}, null, 40, ["operation"])), [[vShow, selectedFilter.value === "All" || selectedFilter.value === view.title]])]),
							_: 2
						}, 1024);
					}), 128)),
					_cache[1] || (_cache[1] = createElementVNode("div", { class: "flex flex-grow" }, null, -1)),
					createVNode(unref(ScalarErrorBoundary), null, {
						default: withCtx(() => [createVNode(RequestCodeExample_default, {
							class: "request-section-content-code-example -mt-1/2 border-t",
							collection: __props.collection,
							environment: __props.envVariables,
							example: __props.example,
							operation: __props.operation,
							server: __props.server,
							workspace: __props.workspace
						}, null, 8, [
							"collection",
							"environment",
							"example",
							"operation",
							"server",
							"workspace"
						])]),
						_: 1
					})
				], 8, _hoisted_5)]),
				_: 1
			}, 8, ["aria-label"]);
		};
	}
});
//#endregion
export { RequestSection_vue_vue_type_script_setup_true_lang_default as default };

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