import { normalizeError } from "../libs/errors.js";
import { toRaw } from "vue";
import { createHash, fetchDocument } from "@scalar/oas-utils/helpers";
import { importSpecToWorkspace } from "@scalar/oas-utils/transforms";
//#region src/store/import-spec.ts
/** Maps the specs by URL */
var specDictionary = {};
/** Generate the import functions from a store context */
function importSpecFileFactory({ requestMutators, collectionMutators, serverMutators, tagMutators, securitySchemeMutators, requestExampleMutators, workspaceMutators, workspaces }) {
	const importSpecFile = async (_spec, workspaceUid, options = {}) => {
		const spec = toRaw(_spec);
		const workspaceEntities = await importSpecToWorkspace(spec, options);
		if (workspaceEntities.error) {
			console.group("IMPORT ERRORS");
			workspaceEntities.importWarnings.forEach((w) => console.warn(w));
			console.groupEnd();
			return;
		}
		if (options.documentUrl && typeof spec === "string") specDictionary[options.documentUrl] = {
			hash: createHash(spec),
			schema: workspaceEntities.schema
		};
		workspaceEntities.examples.forEach((e) => requestExampleMutators.add(e));
		workspaceEntities.requests.forEach((r) => requestMutators.add(r));
		workspaceEntities.tags.forEach((t) => tagMutators.add(t));
		workspaceEntities.servers.forEach((s) => serverMutators.add(s));
		workspaceEntities.securitySchemes.forEach((s) => securitySchemeMutators.add(s));
		collectionMutators.add(workspaceEntities.collection);
		workspaceMutators.edit(workspaceUid, "collections", [...workspaces[workspaceUid]?.collections ?? [], workspaceEntities.collection.uid]);
		return workspaceEntities;
	};
	/**
	* Function to fetch and import a spec from a URL
	*
	* returns true for success
	*/
	async function importSpecFromUrl(url, workspaceUid, { proxyUrl, ...options } = {}) {
		try {
			return [null, await importSpecFile(await fetchDocument(url, proxyUrl), workspaceUid, {
				documentUrl: url,
				...options
			})];
		} catch (error) {
			console.error("Failed to fetch spec from URL:", error);
			return [normalizeError(error), null];
		}
	}
	return {
		importSpecFile,
		importSpecFromUrl
	};
}
//#endregion
export { importSpecFileFactory, specDictionary };

//# sourceMappingURL=import-spec.js.map