import { useWorkspace } from "../../../../store/store.js";
import DataTableRow_default from "../../../../components/DataTable/DataTableRow.vue.js";
import { authorizeOauth2 } from "../../libs/oauth2.js";
import { updateScheme } from "../helpers/update-scheme.js";
import OAuthScopesInput_default from "./OAuthScopesInput.vue.js";
import RequestAuthDataTableInput_default from "./RequestAuthDataTableInput.vue.js";
import { Fragment, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, mergeProps, openBlock, renderSlot, unref, withCtx } from "vue";
import { ScalarButton, useLoadingState } from "@scalar/components";
import { useToasts } from "@scalar/use-toasts";
import { pkceOptions } from "@scalar/oas-utils/entities/spec";
//#region src/views/Request/RequestSection/RequestAuth/OAuth2.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "flex h-8 items-center justify-end border-t" };
var _hoisted_2 = { class: "flex h-8 w-full items-center justify-end border-t" };
var OAuth2_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "OAuth2",
	props: {
		collection: {},
		environment: {},
		envVariables: {},
		flow: {},
		persistAuth: {
			type: Boolean,
			default: false
		},
		scheme: {},
		server: {},
		workspace: {}
	},
	emits: ["authorized"],
	setup(__props, { emit: __emit }) {
		const emits = __emit;
		const loader = useLoadingState();
		const { toast } = useToasts();
		const storeContext = useWorkspace();
		/** Update the current scheme */
		const updateScheme$1 = (path, value) => updateScheme(__props.scheme.uid, path, value, storeContext, __props.persistAuth);
		/** Authorize the user using specified flow */
		const handleAuthorize = async () => {
			if (loader.isActive || !__props.collection?.uid) return;
			if (!__props.server) {
				toast("No server selected", "error");
				return;
			}
			loader.start();
			const [error, accessToken] = await authorizeOauth2(__props.flow, __props.server, __props.workspace?.proxyUrl);
			await loader.clear();
			if (accessToken) {
				updateScheme$1(`flows.${__props.flow.type}.token`, accessToken);
				emits("authorized");
			} else {
				console.error(error);
				toast(error?.message ?? "Failed to authorize", "error");
			}
		};
		/** To make prop drilling a little easier */
		const dataTableInputProps = {
			environment: __props.environment,
			envVariables: __props.envVariables,
			workspace: __props.workspace
		};
		return (_ctx, _cache) => {
			return openBlock(), createElementBlock(Fragment, null, [__props.flow.token ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [createVNode(unref(DataTableRow_default), null, {
				default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps(dataTableInputProps, {
					class: "border-r-transparent",
					modelValue: __props.flow.token,
					placeholder: "QUxMIFlPVVIgQkFTRSBBUkUgQkVMT05HIFRPIFVT",
					type: "password",
					"onUpdate:modelValue": _cache[0] || (_cache[0] = (v) => updateScheme$1(`flows.${__props.flow.type}.token`, v))
				}), {
					default: withCtx(() => [..._cache[10] || (_cache[10] = [createTextVNode(" Access Token ", -1)])]),
					_: 1
				}, 16, ["modelValue"])]),
				_: 1
			}), createVNode(unref(DataTableRow_default), { class: "min-w-full" }, {
				default: withCtx(() => [createElementVNode("div", _hoisted_1, [createVNode(unref(ScalarButton), {
					class: "mr-1 p-0 px-2 py-0.5",
					loader: unref(loader),
					size: "sm",
					variant: "outlined",
					onClick: _cache[1] || (_cache[1] = ($event) => updateScheme$1(`flows.${__props.flow.type}.token`, ""))
				}, {
					default: withCtx(() => [..._cache[11] || (_cache[11] = [createTextVNode(" Clear ", -1)])]),
					_: 1
				}, 8, ["loader"]), renderSlot(_ctx.$slots, "oauth-actions")])]),
				_: 3
			})], 64)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [
				createVNode(unref(DataTableRow_default), null, {
					default: withCtx(() => ["authorizationUrl" in __props.flow ? (openBlock(), createBlock(RequestAuthDataTableInput_default, mergeProps({ key: 0 }, dataTableInputProps, {
						containerClass: "border-r-0",
						modelValue: __props.flow.authorizationUrl,
						placeholder: "https://galaxy.scalar.com/authorize",
						"onUpdate:modelValue": _cache[2] || (_cache[2] = (v) => updateScheme$1(`flows.${__props.flow.type}.authorizationUrl`, v))
					}), {
						default: withCtx(() => [..._cache[12] || (_cache[12] = [createTextVNode(" Auth URL ", -1)])]),
						_: 1
					}, 16, ["modelValue"])) : createCommentVNode("", true), "tokenUrl" in __props.flow ? (openBlock(), createBlock(RequestAuthDataTableInput_default, mergeProps({ key: 1 }, dataTableInputProps, {
						modelValue: __props.flow.tokenUrl,
						placeholder: "https://galaxy.scalar.com/token",
						"onUpdate:modelValue": _cache[3] || (_cache[3] = (v) => updateScheme$1(`flows.${__props.flow.type}.tokenUrl`, v))
					}), {
						default: withCtx(() => [..._cache[13] || (_cache[13] = [createTextVNode(" Token URL ", -1)])]),
						_: 1
					}, 16, ["modelValue"])) : createCommentVNode("", true)]),
					_: 1
				}),
				"x-scalar-redirect-uri" in __props.flow ? (openBlock(), createBlock(unref(DataTableRow_default), { key: 0 }, {
					default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps(dataTableInputProps, {
						modelValue: __props.flow["x-scalar-redirect-uri"],
						placeholder: "https://galaxy.scalar.com/callback",
						"onUpdate:modelValue": _cache[4] || (_cache[4] = (v) => updateScheme$1(`flows.${__props.flow.type}.x-scalar-redirect-uri`, v))
					}), {
						default: withCtx(() => [..._cache[14] || (_cache[14] = [createTextVNode(" Redirect URL ", -1)])]),
						_: 1
					}, 16, ["modelValue"])]),
					_: 1
				})) : createCommentVNode("", true),
				__props.flow.type === "password" ? (openBlock(), createElementBlock(Fragment, { key: 1 }, [createVNode(unref(DataTableRow_default), null, {
					default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps(dataTableInputProps, {
						class: "text-c-2",
						modelValue: __props.flow.username,
						placeholder: "janedoe",
						"onUpdate:modelValue": _cache[5] || (_cache[5] = (v) => updateScheme$1(`flows.${__props.flow.type}.username`, v))
					}), {
						default: withCtx(() => [..._cache[15] || (_cache[15] = [createTextVNode(" Username ", -1)])]),
						_: 1
					}, 16, ["modelValue"])]),
					_: 1
				}), createVNode(unref(DataTableRow_default), null, {
					default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps(dataTableInputProps, {
						modelValue: __props.flow.password,
						placeholder: "********",
						type: "password",
						"onUpdate:modelValue": _cache[6] || (_cache[6] = (v) => updateScheme$1(`flows.${__props.flow.type}.password`, v))
					}), {
						default: withCtx(() => [..._cache[16] || (_cache[16] = [createTextVNode(" Password ", -1)])]),
						_: 1
					}, 16, ["modelValue"])]),
					_: 1
				})], 64)) : createCommentVNode("", true),
				createVNode(unref(DataTableRow_default), null, {
					default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps(dataTableInputProps, {
						modelValue: __props.flow["x-scalar-client-id"],
						placeholder: "12345",
						"onUpdate:modelValue": _cache[7] || (_cache[7] = (v) => updateScheme$1(`flows.${__props.flow.type}.x-scalar-client-id`, v))
					}), {
						default: withCtx(() => [..._cache[17] || (_cache[17] = [createTextVNode(" Client ID ", -1)])]),
						_: 1
					}, 16, ["modelValue"])]),
					_: 1
				}),
				"clientSecret" in __props.flow ? (openBlock(), createBlock(unref(DataTableRow_default), { key: 2 }, {
					default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps(dataTableInputProps, {
						modelValue: __props.flow.clientSecret,
						placeholder: "XYZ123",
						type: "password",
						"onUpdate:modelValue": _cache[8] || (_cache[8] = (v) => updateScheme$1(`flows.${__props.flow.type}.clientSecret`, v))
					}), {
						default: withCtx(() => [..._cache[18] || (_cache[18] = [createTextVNode(" Client Secret ", -1)])]),
						_: 1
					}, 16, ["modelValue"])]),
					_: 1
				})) : createCommentVNode("", true),
				"x-usePkce" in __props.flow ? (openBlock(), createBlock(unref(DataTableRow_default), { key: 3 }, {
					default: withCtx(() => [createVNode(RequestAuthDataTableInput_default, mergeProps(dataTableInputProps, {
						enum: unref(pkceOptions),
						modelValue: __props.flow["x-usePkce"],
						readOnly: "",
						"onUpdate:modelValue": _cache[9] || (_cache[9] = (v) => updateScheme$1(`flows.${__props.flow.type}.x-usePkce`, v))
					}), {
						default: withCtx(() => [..._cache[19] || (_cache[19] = [createTextVNode(" Use PKCE ", -1)])]),
						_: 1
					}, 16, ["enum", "modelValue"])]),
					_: 1
				})) : createCommentVNode("", true),
				Object.keys(__props.flow.scopes ?? {}).length ? (openBlock(), createBlock(unref(DataTableRow_default), { key: 4 }, {
					default: withCtx(() => [createVNode(OAuthScopesInput_default, {
						flow: __props.flow,
						updateScheme: updateScheme$1
					}, null, 8, ["flow"])]),
					_: 1
				})) : createCommentVNode("", true)
			], 64)), !__props.flow.token ? (openBlock(), createBlock(unref(DataTableRow_default), {
				key: 2,
				class: "min-w-full"
			}, {
				default: withCtx(() => [createElementVNode("div", _hoisted_2, [createVNode(unref(ScalarButton), {
					class: "mr-0.75 p-0 px-2 py-0.5",
					loader: unref(loader),
					size: "sm",
					variant: "outlined",
					onClick: handleAuthorize
				}, {
					default: withCtx(() => [..._cache[20] || (_cache[20] = [createTextVNode(" Authorize ", -1)])]),
					_: 1
				}, 8, ["loader"])])]),
				_: 1
			})) : createCommentVNode("", true)], 64);
		};
	}
});
//#endregion
export { OAuth2_vue_vue_type_script_setup_true_lang_default as default };

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