import { flattenEnvVars } from "../libs/string-template.js";
import { PathId } from "../routes.js";
import { getRouterParams } from "./router-params.js";
import { computed, inject } from "vue";
import { environmentSchema } from "@scalar/oas-utils/entities/environment";
import { isDefined } from "@scalar/oas-utils/helpers";
//#region src/store/active-entities.ts
/**
* Create the active entities store
*
* We need the factory function to pass the router instance
*/
var createActiveEntitiesStore = ({ collections, requestExamples, requests, router, servers, workspaces }) => {
	/** Gives the required UID usually per route */
	const activeRouterParams = computed(getRouterParams(router));
	/** The currently selected workspace OR the first one */
	const activeWorkspace = computed(() => workspaces[activeRouterParams.value[PathId.Workspace]] ?? Object.values(workspaces)[0]);
	/** Ordered list of the active workspace's collections with drafts last */
	const activeWorkspaceCollections = computed(() => activeWorkspace.value?.collections.map((uid) => collections[uid]).filter(isDefined).sort((a, b) => {
		if (a.info?.title === "Drafts") return 1;
		if (b.info?.title === "Drafts") return -1;
		return 0;
	}) ?? []);
	/** Simplified list of servers in the workspace for displaying */
	const activeWorkspaceServers = computed(() => activeWorkspaceCollections.value?.flatMap((collection) => collection.servers.map((uid) => servers[uid])));
	/** Simplified list of requests in the workspace for displaying */
	const activeWorkspaceRequests = computed(() => activeWorkspaceCollections.value?.flatMap((collection) => collection.requests) ?? []);
	/** The currently selected environment */
	const activeEnvironment = computed(() => {
		if (!activeWorkspace.value?.activeEnvironmentId) return environmentSchema.parse({
			uid: "default",
			color: "#FFFFFF",
			name: "No Environment",
			value: JSON.stringify(activeWorkspace.value?.environments, null, 2)
		});
		const activeEnvironmentCollection = activeWorkspaceCollections.value.find((c) => c["x-scalar-environments"]?.[activeWorkspace.value?.activeEnvironmentId ?? ""]);
		if (activeEnvironmentCollection && activeWorkspace.value?.activeEnvironmentId) return environmentSchema.parse({
			uid: activeWorkspace.value.activeEnvironmentId,
			name: activeWorkspace.value.activeEnvironmentId,
			value: JSON.stringify(activeEnvironmentCollection["x-scalar-environments"]?.[activeWorkspace.value?.activeEnvironmentId]?.variables, null, 2),
			color: activeEnvironmentCollection["x-scalar-environments"]?.[activeWorkspace.value?.activeEnvironmentId]?.color || "#FFFFFF",
			isDefault: false
		});
		return environmentSchema.parse({
			uid: "default",
			color: "#FFFFFF",
			name: "No Environment",
			value: JSON.stringify(activeWorkspace.value.environments, null, 2)
		});
	});
	/**
	* Request associated with the current route
	*
	* undefined must be handled as we may have no requests
	*/
	const activeRequest = computed(() => {
		const key = activeRouterParams.value[PathId.Request];
		const collection = collections[activeRouterParams.value.collection] || collections[activeWorkspace.value?.collections[0] ?? ""];
		return requests[key] || requests[collection?.requests[0] ?? ""] || Object.values(requests)[0];
	});
	/** Grabs the currently active example using the path param */
	const activeExample = computed(() => {
		return requestExamples[activeRouterParams.value[PathId.Examples] === "default" ? activeRequest.value?.examples[0] || "" : activeRouterParams.value[PathId.Examples]];
	});
	/**
	* First collection that the active request is in
	*/
	const activeCollection = computed(() => {
		const collection = collections[activeRouterParams.value[PathId.Collection]];
		if (collection) return collection;
		const requestUid = activeRequest.value?.uid;
		if (requestUid) return Object.values(collections).find((c) => c.requests?.includes(requestUid));
		return collections[activeWorkspace.value?.collections[0] ?? collections[0]?.uid ?? ""];
	});
	/** The currently selected server in the addressBar */
	const activeServer = computed(() => {
		if (activeRequest.value?.selectedServerUid) {
			const server = servers[activeRequest.value.selectedServerUid];
			if (server) return server;
		}
		if (activeCollection.value?.selectedServerUid) {
			const server = servers[activeCollection.value.selectedServerUid];
			if (server) return server;
		}
	});
	return {
		activeCollection,
		activeCookieId: computed(() => activeRouterParams.value[PathId.Cookies] === "default" ? activeWorkspace.value?.cookies[0] ?? "default" : activeRouterParams.value[PathId.Cookies]),
		activeExample,
		activeRequest,
		activeRouterParams,
		activeEnvironment,
		activeServer,
		activeWorkspace,
		activeWorkspaceCollections,
		activeWorkspaceServers,
		activeEnvVariables: computed(() => {
			const globalEnvironment = activeWorkspace.value?.environments ?? {};
			const collectionEnvironment = activeEnvironment.value.uid ? JSON.parse(activeEnvironment.value.value) : {};
			const globalEnvVars = flattenEnvVars(globalEnvironment).map(([key, value]) => ({
				key,
				value,
				source: "global"
			}));
			const collectionEnvVars = flattenEnvVars(collectionEnvironment).map(([key, value]) => ({
				key,
				value,
				source: "collection"
			}));
			const mergedEnvVars = /* @__PURE__ */ new Map();
			collectionEnvVars.forEach((envVar) => {
				mergedEnvVars.set(envVar.key, envVar);
			});
			globalEnvVars.forEach((envVar) => {
				if (!mergedEnvVars.has(envVar.key)) mergedEnvVars.set(envVar.key, envVar);
			});
			return Array.from(mergedEnvVars.values());
		}),
		activeWorkspaceRequests
	};
};
var ACTIVE_ENTITIES_SYMBOL = Symbol();
/**
* The active entities store
*
* This store returns anything related to the currently active entities
* The only reason this is a store and not a simple hook is due to storing the current router here
*
* @deprecated due to components being used outside of the client now, we should prop drill instead of this hook/store
*/
var useActiveEntities = () => {
	const store = inject(ACTIVE_ENTITIES_SYMBOL);
	if (!store) throw new Error("Active entities store not provided");
	return store;
};
//#endregion
export { ACTIVE_ENTITIES_SYMBOL, createActiveEntitiesStore, useActiveEntities };

//# sourceMappingURL=active-entities.js.map