import { PathId } from "../../routes.js";
import { useActiveEntities } from "../../store/active-entities.js";
import { useWorkspace } from "../../store/store.js";
import CommandActionForm_default from "./CommandActionForm.vue.js";
import CommandActionInput_default from "./CommandActionInput.vue.js";
import HttpMethod_default from "../HttpMethod/HttpMethod.vue.js";
import { Fragment, computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createTextVNode, createVNode, defineComponent, openBlock, ref, renderList, toDisplayString, unref, withCtx } from "vue";
import { ScalarButton, ScalarDropdown, ScalarDropdownItem, ScalarIcon } from "@scalar/components";
import { useToasts } from "@scalar/use-toasts";
import { useRouter } from "vue-router";
import { isDefined } from "@scalar/oas-utils/helpers";
//#region src/components/CommandPalette/CommandPaletteExample.vue?vue&type=script&setup=true&lang.ts
var _hoisted_1 = { class: "flex items-center gap-2" };
var _hoisted_2 = { class: "custom-scroll max-h-40" };
var CommandPaletteExample_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
	__name: "CommandPaletteExample",
	props: { metaData: {} },
	emits: ["close", "back"],
	setup(__props, { emit: __emit }) {
		const props = __props;
		const emits = __emit;
		const { push } = useRouter();
		const { activeRequest, activeWorkspace, activeWorkspaceRequests } = useActiveEntities();
		const { requests, requestExampleMutators } = useWorkspace();
		const { toast } = useToasts();
		const exampleName = ref("");
		const selectedRequest = ref(requests[props.metaData?.itemUid ?? ""] ?? activeRequest.value);
		/** Select request in dropdown */
		const handleSelect = (request) => selectedRequest.value = request;
		/** Add a new request example */
		const handleSubmit = () => {
			if (!exampleName.value) {
				toast("Please enter a name before creating an example.", "error");
				return;
			}
			if (!selectedRequest.value) {
				toast("Please select a request before creating an example.", "error");
				return;
			}
			const example = requestExampleMutators.add(selectedRequest.value, exampleName.value);
			if (!example) return;
			push({
				name: "request.examples",
				params: {
					[PathId.Workspace]: activeWorkspace.value?.uid,
					[PathId.Request]: selectedRequest.value.uid,
					[PathId.Examples]: example.uid
				}
			});
			emits("close");
		};
		/** Requests for the active workspace */
		const visibleRequests = computed(() => activeWorkspaceRequests.value.map((uid) => requests?.[uid]).filter(isDefined));
		return (_ctx, _cache) => {
			return selectedRequest.value ? (openBlock(), createBlock(CommandActionForm_default, {
				key: 0,
				disabled: !exampleName.value.trim(),
				onSubmit: handleSubmit
			}, {
				options: withCtx(() => [createVNode(unref(ScalarDropdown), {
					placement: "bottom",
					resize: ""
				}, {
					items: withCtx(() => [createElementVNode("div", _hoisted_2, [(openBlock(true), createElementBlock(Fragment, null, renderList(visibleRequests.value, (request) => {
						return openBlock(), createBlock(unref(ScalarDropdownItem), {
							key: request.uid,
							class: "flex h-7 w-full items-center justify-between px-1 pr-[26px]",
							onClick: ($event) => handleSelect(request)
						}, {
							default: withCtx(() => [createTextVNode(toDisplayString(request.summary) + " ", 1), createVNode(HttpMethod_default, { method: request.method }, null, 8, ["method"])]),
							_: 2
						}, 1032, ["onClick"]);
					}), 128))])]),
					default: withCtx(() => [createVNode(unref(ScalarButton), {
						class: "hover:bg-b-2 max-h-8 w-full justify-between gap-1 p-2 text-xs",
						variant: "outlined",
						onClick: _cache[2] || (_cache[2] = ($event) => handleSelect(selectedRequest.value))
					}, {
						default: withCtx(() => [createTextVNode(toDisplayString(selectedRequest.value.summary) + " ", 1), createElementVNode("div", _hoisted_1, [createVNode(HttpMethod_default, { method: selectedRequest.value.method }, null, 8, ["method"]), createVNode(unref(ScalarIcon), {
							class: "text-c-3",
							icon: "ChevronDown",
							size: "md"
						})])]),
						_: 1
					})]),
					_: 1
				})]),
				submit: withCtx(() => [..._cache[3] || (_cache[3] = [createTextVNode("Create Example", -1)])]),
				default: withCtx(() => [createVNode(CommandActionInput_default, {
					modelValue: exampleName.value,
					"onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => exampleName.value = $event),
					label: "Example Name",
					placeholder: "Example Name",
					onOnDelete: _cache[1] || (_cache[1] = ($event) => emits("back", $event))
				}, null, 8, ["modelValue"])]),
				_: 1
			}, 8, ["disabled"])) : createCommentVNode("", true);
		};
	}
});
//#endregion
export { CommandPaletteExample_vue_vue_type_script_setup_true_lang_default as default };

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