import { useWorkspace } from "../../../../store/store.js";
import DataTableCell_default from "../../../../components/DataTable/DataTableCell.vue.js";
import DataTableRow_default from "../../../../components/DataTable/DataTableRow.vue.js";
import { updateScheme } from "../helpers/update-scheme.js";
import { restoreAuthFromLocalStorage } from "./helpers/restore-auth-from-local-storage.js";
import RequestAuthDataTableInput_default from "./RequestAuthDataTableInput.vue.js";
import OAuth2_default from "./OAuth2.vue.js";
import { Fragment, capitalize, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, mergeProps, normalizeClass, onMounted, openBlock, ref, renderList, renderSlot, toDisplayString, unref, withCtx } from "vue";
import { ScalarMarkdownSummary } from "@scalar/components";
//#region src/views/Request/RequestSection/RequestAuth/RequestAuthTab.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "bg-b-1 text-c-2 outline-b-3 top-0 z-1 h-full w-full overflow-hidden px-3 py-1.25 text-ellipsis group-hover/auth:absolute group-hover/auth:h-auto group-hover/auth:border-b *:first:line-clamp-1 *:first:text-ellipsis group-hover/auth:*:first:line-clamp-none" };
var _hoisted_2 = {
	key: 0,
	class: "flex min-h-8 border-t text-base"
};
var _hoisted_3 = { class: "flex h-8 max-w-full gap-2.5 overflow-x-auto px-3" };
var _hoisted_4 = ["onClick"];
var _hoisted_5 = { class: "relative z-10" };
var RequestAuthTab_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "RequestAuthTab",
	props: {
		collection: {},
		environment: {},
		envVariables: {},
		layout: {},
		persistAuth: {
			type: Boolean,
			default: false
		},
		securitySchemeUids: {},
		server: {},
		workspace: {}
	},
	emits: ["authorized"],
	setup(__props, { emit: __emit }) {
		const emits = __emit;
		const storeContext = useWorkspace();
		const { securitySchemes } = storeContext;
		const security = computed(() => __props.securitySchemeUids.map((uid) => ({ scheme: securitySchemes[uid] })));
		const activeFlow = ref("");
		const generateLabel = (scheme) => {
			const description = scheme.description ? `: ${scheme.description}` : "";
			const baseLabel = `${capitalize(scheme.nameKey)}${description || `: ${scheme.type}`}`;
			if (scheme.type === "apiKey") return `${capitalize(scheme.nameKey)}${description || `: ${scheme.in}`}`;
			if (scheme.type === "oauth2") {
				const firstFlow = Object.values(scheme.flows ?? {})[0];
				return `${capitalize(scheme.nameKey)}: ${activeFlow.value ? activeFlow.value : firstFlow?.type ?? ""}${description}`;
			}
			if (scheme.type === "http") return `${capitalize(scheme.nameKey)}: ${scheme.scheme}${description}`;
			return `${baseLabel}${description}`;
		};
		/** Wrapper for the updateScheme function */
		const updateScheme$1 = (uid, path, value) => {
			updateScheme(uid, path, value, storeContext, __props.persistAuth);
		};
		onMounted(() => {
			if (!__props.persistAuth) return;
			restoreAuthFromLocalStorage(storeContext, __props.collection.uid);
		});
		/** To make prop drilling a little easier */
		const dataTableInputProps = {
			environment: __props.environment,
			envVariables: __props.envVariables,
			workspace: __props.workspace
		};
		return (_ctx, _cache) => {
			return openBlock(true), createElementBlock(Fragment, null, renderList(security.value, ({ scheme }) => {
				return openBlock(), createElementBlock(Fragment, { key: scheme?.uid }, [
					security.value.length > 1 && scheme ? (openBlock(), createBlock(unref(DataTableRow_default), { key: 0 }, {
						default: withCtx(() => [createVNode(unref(DataTableCell_default), {
							"aria-label": generateLabel(scheme),
							class: "text-c-2 group/auth flex items-center leading-[22px] whitespace-nowrap outline-none hover:whitespace-normal"
						}, {
							default: withCtx(() => [createElementVNode("p", _hoisted_1, toDisplayString(generateLabel(scheme)), 1)]),
							_: 2
						}, 1032, ["aria-label"])]),
						_: 2
					}, 1024)) : createCommentVNode("", true),
					scheme?.description && security.value.length <= 1 ? (openBlock(), createBlock(unref(DataTableRow_default), { key: 1 }, {
						default: withCtx(() => [createVNode(unref(DataTableCell_default), {
							"aria-label": scheme.description,
							class: "max-h-[auto]"
						}, {
							default: withCtx(() => [createVNode(unref(ScalarMarkdownSummary), {
								class: "auth-description bg-b-1 text-c-2 min-w-0 flex-1 px-3 py-1.25",
								value: scheme.description
							}, null, 8, ["value"])]),
							_: 2
						}, 1032, ["aria-label"])]),
						_: 2
					}, 1024)) : createCommentVNode("", true),
					scheme?.type === "http" ? (openBlock(), createElementBlock(Fragment, { key: 2 }, [scheme.scheme === "bearer" ? (openBlock(), createBlock(unref(DataTableRow_default), { key: 0 }, {
						default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps({ ref_for: true }, dataTableInputProps, {
							containerClass: __props.layout === "reference" && "border-t",
							modelValue: scheme.token,
							placeholder: "Token",
							type: "password",
							"onUpdate:modelValue": (v) => updateScheme$1(scheme.uid, "token", v)
						}), {
							default: withCtx(() => [..._cache[1] || (_cache[1] = [createTextVNode(" Bearer Token ", -1)])]),
							_: 1
						}, 16, [
							"containerClass",
							"modelValue",
							"onUpdate:modelValue"
						])]),
						_: 2
					}, 1024)) : scheme?.scheme === "basic" ? (openBlock(), createElementBlock(Fragment, { key: 1 }, [createVNode(unref(DataTableRow_default), null, {
						default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps({ ref_for: true }, dataTableInputProps, {
							class: "text-c-2",
							modelValue: scheme.username,
							placeholder: "janedoe",
							required: "",
							"onUpdate:modelValue": (v) => updateScheme$1(scheme.uid, "username", v)
						}), {
							default: withCtx(() => [..._cache[2] || (_cache[2] = [createTextVNode(" Username ", -1)])]),
							_: 1
						}, 16, ["modelValue", "onUpdate:modelValue"])]),
						_: 2
					}, 1024), createVNode(unref(DataTableRow_default), null, {
						default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps({ ref_for: true }, dataTableInputProps, {
							modelValue: scheme.password,
							placeholder: "********",
							type: "password",
							"onUpdate:modelValue": (v) => updateScheme$1(scheme.uid, "password", v)
						}), {
							default: withCtx(() => [..._cache[3] || (_cache[3] = [createTextVNode(" Password ", -1)])]),
							_: 1
						}, 16, ["modelValue", "onUpdate:modelValue"])]),
						_: 2
					}, 1024)], 64)) : createCommentVNode("", true)], 64)) : scheme?.type === "apiKey" ? (openBlock(), createElementBlock(Fragment, { key: 3 }, [createVNode(unref(DataTableRow_default), null, {
						default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps({ ref_for: true }, dataTableInputProps, {
							containerClass: __props.layout === "reference" && "border-t",
							modelValue: scheme.name,
							placeholder: "api-key",
							"onUpdate:modelValue": (v) => updateScheme$1(scheme.uid, "name", v)
						}), {
							default: withCtx(() => [..._cache[4] || (_cache[4] = [createTextVNode(" Name ", -1)])]),
							_: 1
						}, 16, [
							"containerClass",
							"modelValue",
							"onUpdate:modelValue"
						])]),
						_: 2
					}, 1024), createVNode(unref(DataTableRow_default), null, {
						default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps({ ref_for: true }, dataTableInputProps, {
							modelValue: scheme.value,
							placeholder: "QUxMIFlPVVIgQkFTRSBBUkUgQkVMT05HIFRPIFVT",
							type: "password",
							"onUpdate:modelValue": (v) => updateScheme$1(scheme.uid, "value", v)
						}), {
							default: withCtx(() => [..._cache[5] || (_cache[5] = [createTextVNode(" Value ", -1)])]),
							_: 1
						}, 16, ["modelValue", "onUpdate:modelValue"])]),
						_: 2
					}, 1024)], 64)) : scheme?.type === "oauth2" ? (openBlock(), createElementBlock(Fragment, { key: 4 }, [createVNode(unref(DataTableRow_default), null, {
						default: withCtx(() => [Object.keys(scheme.flows).length > 1 ? (openBlock(), createElementBlock("div", _hoisted_2, [createElementVNode("div", _hoisted_3, [(openBlock(true), createElementBlock(Fragment, null, renderList(scheme?.flows, (_, key, ind) => {
							return openBlock(), createElementBlock("button", {
								key,
								class: normalizeClass(["floating-bg text-c-3 relative cursor-pointer border-b-[1px] border-transparent py-1 text-base font-medium", {
									"!text-c-1 !rounded-none border-b-[1px] !border-current": __props.layout !== "reference" && (activeFlow.value === key || ind === 0 && !activeFlow.value),
									"!text-c-1 !rounded-none border-b-[1px] !border-current opacity-100": __props.layout === "reference" && (activeFlow.value === key || ind === 0 && !activeFlow.value)
								}]),
								type: "button",
								onClick: ($event) => activeFlow.value = key
							}, [createElementVNode("span", _hoisted_5, toDisplayString(key), 1)], 10, _hoisted_4);
						}), 128))])])) : createCommentVNode("", true)]),
						_: 2
					}, 1024), (openBlock(true), createElementBlock(Fragment, null, renderList(scheme?.flows, (flow, key, ind) => {
						return openBlock(), createElementBlock(Fragment, { key }, [activeFlow.value === key || ind === 0 && !activeFlow.value ? (openBlock(), createBlock(OAuth2_default, mergeProps({
							key: 0,
							ref_for: true
						}, dataTableInputProps, {
							collection: __props.collection,
							flow,
							persistAuth: __props.persistAuth,
							scheme,
							server: __props.server,
							workspace: __props.workspace,
							onAuthorized: _cache[0] || (_cache[0] = ($event) => emits("authorized"))
						}), {
							"oauth-actions": withCtx(() => [renderSlot(_ctx.$slots, "oauth-actions")]),
							_: 3
						}, 16, [
							"collection",
							"flow",
							"persistAuth",
							"scheme",
							"server",
							"workspace"
						])) : createCommentVNode("", true)], 64);
					}), 128))], 64)) : scheme?.type === "openIdConnect" ? (openBlock(), createElementBlock("div", {
						key: 5,
						class: normalizeClass(["text-c-3 bg-b-1 flex min-h-[calc(4rem+1px)] items-center justify-center border-t border-b-0 px-4 text-base", { "rounded-b-lg": __props.layout === "reference" }])
					}, " Coming soon ", 2)) : createCommentVNode("", true)
				], 64);
			}), 128);
		};
	}
});
//#endregion
export { RequestAuthTab_vue_vue_type_script_setup_true_lang_default as default };

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