import { r as useWorkspace } from "./store-DpSUVhjp.js";
import { a as useLayout } from "./useSidebar-DLTwHDI-.js";
import { n as formatMs } from "./formatters-DOGXZq9R.js";
import { t as _plugin_vue_export_helper_default } from "./_plugin-vue_export-helper-BmmBcIzD.js";
import { t as HttpMethod_default } from "./HttpMethod-rnWEgC3T.js";
import { t as CodeInput_default } from "./CodeInput-C-igR77V.js";
import { n as ServerDropdown_default } from "./Server-DnA_BzPS.js";
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createSlots, createVNode, defineComponent, normalizeClass, normalizeStyle, openBlock, ref, renderList, toDisplayString, unref, useId, watch, withCtx } from "vue";
import { ScalarButton, ScalarFloating, ScalarFloatingBackdrop, ScalarIcon } from "@scalar/components";
import { httpStatusCodes } from "@scalar/oas-utils/helpers";
import { Menu, MenuButton, MenuItem, MenuItems } from "@headlessui/vue";
import { REQUEST_METHODS } from "@scalar/helpers/http/http-info";
import { ScalarIconPlay } from "@scalar/icons";
//#region src/components/AddressBar/httpStatusCodeColors.ts
var STATUS_CODES_COLORS = {
	100: { color: "text-yellow" },
	200: { color: "text-green" },
	202: { color: "text-green" },
	300: { color: "text-blue" },
	304: { color: "text-blue" },
	400: { color: "text-red" },
	401: { color: "text-orange" },
	422: { color: "text-yellow" },
	423: { color: "text-purple" },
	505: { color: "text-orange" }
};
var getStatusCodeColor = (statusCode) => {
	return STATUS_CODES_COLORS[statusCode] || { color: "text-grey" };
};
//#endregion
//#region src/components/AddressBar/AddressBarHistory.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1$1 = { class: "min-w-0" };
var _hoisted_2$1 = { class: "text-c-1 min-w-0 truncate" };
//#endregion
//#region src/components/AddressBar/AddressBarHistory.vue
var AddressBarHistory_default = /* @__PURE__ */ _plugin_vue_export_helper_default(/* @__PURE__ */ defineComponent({
	__name: "AddressBarHistory",
	props: {
		operation: {},
		target: {}
	},
	setup(__props) {
		const { requestHistory } = useWorkspace();
		/** Use a local copy to prevent mutation of the reactive object */
		const history = computed(() => requestHistory.filter((entry) => entry.request.requestUid === __props.operation.uid).slice().reverse());
		function handleHistoryClick(requestHistoryItem) {
			console.warn("Restoring from the request history doesn't work yet. Request History Item:", requestHistoryItem);
		}
		return (_ctx, _cache) => {
			return openBlock(), createBlock(unref(Menu), { as: "div" }, {
				default: withCtx(({ open }) => [createVNode(unref(ScalarFloating), {
					offset: 0,
					resize: "",
					target: __props.target
				}, createSlots({
					default: withCtx(() => [history.value?.length ? (openBlock(), createBlock(unref(MenuButton), {
						key: 0,
						class: "address-bar-history-button z-context-plus text-c-3 focus:text-c-1 relative mr-1 rounded-lg p-1.5"
					}, {
						default: withCtx(() => [createVNode(unref(ScalarIcon), {
							icon: "History",
							size: "sm",
							thickness: "2.25"
						}), _cache[0] || (_cache[0] = createElementVNode("span", { class: "sr-only" }, "Request History", -1))]),
						_: 1
					})) : createCommentVNode("", true)]),
					_: 2
				}, [open ? {
					name: "floating",
					fn: withCtx(({ width }) => [createVNode(unref(MenuItems), {
						class: "custom-scroll grid max-h-[inherit] grid-cols-[44px_1fr_repeat(3,auto)] items-center border-t p-0.75",
						static: "",
						style: normalizeStyle({ width })
					}, {
						default: withCtx(() => [(openBlock(true), createElementBlock(Fragment, null, renderList(history.value, (entry, index) => {
							return openBlock(), createBlock(unref(MenuItem), {
								key: entry.timestamp,
								as: "button",
								class: "font-code ui-active:*:bg-b-2 text-c-2 contents text-sm font-medium *:flex *:h-8 *:cursor-pointer *:items-center *:rounded-none *:px-1.5 *:first:rounded-l *:last:rounded-r",
								value: index,
								onClick: ($event) => handleHistoryClick(entry)
							}, {
								default: withCtx(() => [
									entry.response.method ? (openBlock(), createBlock(HttpMethod_default, {
										key: 0,
										class: "text-[11px]",
										method: entry.response.method
									}, null, 8, ["method"])) : createCommentVNode("", true),
									createElementVNode("div", _hoisted_1$1, [createElementVNode("div", _hoisted_2$1, toDisplayString(entry.response.path), 1)]),
									createElementVNode("div", null, toDisplayString(unref(formatMs)(entry.response.duration)), 1),
									createElementVNode("div", { class: normalizeClass([unref(getStatusCodeColor)(entry.response.status).color]) }, toDisplayString(entry.response.status), 3),
									createElementVNode("div", null, toDisplayString(unref(httpStatusCodes)[entry.response.status]?.name), 1)
								]),
								_: 2
							}, 1032, ["value", "onClick"]);
						}), 128))]),
						_: 1
					}, 8, ["style"]), createVNode(unref(ScalarFloatingBackdrop), { class: "-top-(--scalar-address-bar-height) rounded-lg" })]),
					key: "0"
				} : void 0]), 1032, ["target"])]),
				_: 1
			});
		};
	}
}), [["__scopeId", "data-v-a93fa60f"]]);
//#endregion
//#region src/components/AddressBar/AddressBar.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = ["id"];
var _hoisted_2 = { class: "address-bar-bg-states text-xxs group relative order-last flex w-full max-w-[calc(100dvw-24px)] flex-1 flex-row items-stretch rounded-lg p-0.75 lg:order-none lg:max-w-[580px] lg:min-w-[580px] xl:max-w-[720px] xl:min-w-[720px]" };
var _hoisted_3 = { class: "pointer-events-none absolute top-0 left-0 block h-full w-full overflow-hidden rounded-lg border" };
var _hoisted_4 = { class: "z-context-plus flex gap-1" };
var _hoisted_5 = { class: "scroll-timeline-x scroll-timeline-x-hidden z-context-plus relative flex w-full bg-blend-normal" };
var _hoisted_6 = {
	"aria-hidden": "true",
	class: "inline-flex items-center gap-1"
};
var _hoisted_7 = { class: "sr-only" };
//#endregion
//#region src/components/AddressBar/AddressBar.vue
var AddressBar_default = /* @__PURE__ */ _plugin_vue_export_helper_default(/* @__PURE__ */ defineComponent({
	__name: "AddressBar",
	props: {
		collection: {},
		operation: {},
		server: {},
		environment: {},
		envVariables: {},
		workspace: {}
	},
	emits: ["importCurl"],
	setup(__props) {
		const id = useId();
		const { requestMutators, events } = useWorkspace();
		const { layout } = useLayout();
		const addressBarRef = ref(null);
		const sendButtonRef = ref(null);
		/** update the instance path parameters on change */
		const onUrlChange = (newPath) => {
			if (__props.operation.path === newPath) return;
			requestMutators.edit(__props.operation.uid, "path", newPath);
		};
		/** watch for changes in the URL */
		watch(() => __props.operation.path, (newURL) => {
			if (!newURL) return;
			onUrlChange(newURL);
		});
		/** The amount remaining to load from 100 -> 0 */
		const percentage = ref(100);
		/** Keeps track of how much was left when the request finished */
		const remaining = ref(0);
		/** Whether or not there is a request loading */
		const isRequesting = ref(false);
		/** The loading interval */
		const interval = ref();
		function load() {
			if (isRequesting.value) percentage.value -= (percentage.value - 15) / 60;
			else percentage.value -= remaining.value / 20;
			if (percentage.value <= 0) {
				clearInterval(interval.value);
				interval.value = void 0;
				percentage.value = 100;
				isRequesting.value = false;
			}
		}
		function startLoading() {
			if (interval.value) return;
			isRequesting.value = true;
			interval.value = setInterval(load, 20);
		}
		function stopLoading() {
			remaining.value = percentage.value;
			isRequesting.value = false;
		}
		function abortLoading() {
			clearInterval(interval.value);
			interval.value = void 0;
			percentage.value = 100;
			isRequesting.value = false;
		}
		events.requestStatus.on((status) => {
			if (status === "start") return startLoading();
			if (status === "stop") return stopLoading();
			if (status === "abort") return abortLoading();
		});
		/** Focus the address bar (or the send button if in modal layout) */
		events.focusAddressBar.on(() => {
			if (layout === "modal") sendButtonRef.value?.$el?.focus();
			else addressBarRef.value?.focus();
		});
		function updateRequestMethod(method) {
			requestMutators.edit(__props.operation.uid, "method", method);
		}
		function getBackgroundColor() {
			const { method } = __props.operation;
			return REQUEST_METHODS[method].colorVar;
		}
		function handleExecuteRequest() {
			if (isRequesting.value) return;
			isRequesting.value = true;
			events.executeRequest.emit({ requestUid: __props.operation.uid });
		}
		/** Handle hotkeys */
		events.hotKeys.on((event) => {
			if (event?.focusAddressBar) addressBarRef.value?.focus();
			if (event?.executeRequest) handleExecuteRequest();
		});
		/**
		* TODO: Should we handle query params here somehow?
		*/
		function updateRequestPath(url) {
			requestMutators.edit(__props.operation.uid, "path", url);
		}
		return (_ctx, _cache) => {
			return openBlock(), createElementBlock("div", {
				id: unref(id),
				class: "scalar-address-bar order-last flex h-(--scalar-address-bar-height) w-full [--scalar-address-bar-height:32px] lg:order-none lg:w-auto"
			}, [createElementVNode("div", _hoisted_2, [
				createElementVNode("div", _hoisted_3, [createElementVNode("div", {
					class: "absolute top-0 left-0 z-[1002] h-full w-full",
					style: normalizeStyle({
						backgroundColor: `color-mix(in srgb, transparent 90%, ${getBackgroundColor()})`,
						transform: `translate3d(-${percentage.value}%,0,0)`
					})
				}, null, 4)]),
				createElementVNode("div", _hoisted_4, [createVNode(HttpMethod_default, {
					isEditable: unref(layout) !== "modal",
					isSquare: "",
					method: __props.operation.method,
					teleport: "",
					onChange: updateRequestMethod
				}, null, 8, ["isEditable", "method"])]),
				createElementVNode("div", _hoisted_5, [
					__props.collection.servers.length ? (openBlock(), createBlock(unref(ServerDropdown_default), {
						key: 0,
						collection: __props.collection,
						layout: "client",
						operation: __props.operation,
						server: __props.server,
						target: unref(id)
					}, null, 8, [
						"collection",
						"operation",
						"server",
						"target"
					])) : createCommentVNode("", true),
					_cache[1] || (_cache[1] = createElementVNode("div", { class: "fade-left" }, null, -1)),
					createVNode(CodeInput_default, {
						ref_key: "addressBarRef",
						ref: addressBarRef,
						"aria-label": "Path",
						class: "min-w-fit outline-none",
						disableCloseBrackets: "",
						disabled: unref(layout) === "modal",
						disableEnter: "",
						disableTabIndent: "",
						emitOnBlur: false,
						envVariables: __props.envVariables,
						environment: __props.environment,
						importCurl: "",
						modelValue: __props.operation.path,
						placeholder: __props.server?.uid && __props.collection.servers.includes(__props.server.uid) ? "" : "Enter a URL or cURL command",
						server: "",
						workspace: __props.workspace,
						onCurl: _cache[0] || (_cache[0] = ($event) => _ctx.$emit("importCurl", $event)),
						onSubmit: handleExecuteRequest,
						"onUpdate:modelValue": updateRequestPath
					}, null, 8, [
						"disabled",
						"envVariables",
						"environment",
						"modelValue",
						"placeholder",
						"workspace"
					]),
					_cache[2] || (_cache[2] = createElementVNode("div", { class: "fade-right" }, null, -1))
				]),
				createVNode(AddressBarHistory_default, {
					operation: __props.operation,
					target: unref(id)
				}, null, 8, ["operation", "target"]),
				createVNode(unref(ScalarButton), {
					ref_key: "sendButtonRef",
					ref: sendButtonRef,
					class: "z-context-plus relative h-auto shrink-0 overflow-hidden py-1 pr-2.5 pl-2 font-bold",
					disabled: isRequesting.value,
					onClick: handleExecuteRequest
				}, {
					default: withCtx(() => [createElementVNode("span", _hoisted_6, [createVNode(unref(ScalarIconPlay), {
						class: "relative shrink-0",
						size: "xs",
						weight: "fill"
					}), _cache[3] || (_cache[3] = createElementVNode("span", { class: "text-xxs hidden lg:flex" }, "Send", -1))]), createElementVNode("span", _hoisted_7, " Send " + toDisplayString(__props.operation.method) + " request to " + toDisplayString(__props.server?.url ?? "") + toDisplayString(__props.operation.path), 1)]),
					_: 1
				}, 8, ["disabled"])
			])], 8, _hoisted_1);
		};
	}
}), [["__scopeId", "data-v-cb2a35da"]]);
//#endregion
export { AddressBar_default as t };

//# sourceMappingURL=AddressBar-B4xnl66I.js.map