import { reactive } from "vue";
import { LS_KEYS } from "@scalar/helpers/object/local-storage";
import { collectionSchema } from "@scalar/oas-utils/entities/spec";
import { mutationFactory } from "@scalar/object-utils/mutator-record";
//#region src/store/collections.ts
/** Initiate the workspace collections */
function createStoreCollections(useLocalStorage) {
	const collections = reactive({});
	return {
		collections,
		collectionMutators: mutationFactory(collections, reactive({}), useLocalStorage && LS_KEYS.COLLECTION)
	};
}
/** Extended mutators and data for collections that required workspace access */
function extendedCollectionDataFactory({ requests, requestMutators, requestExamples, requestExampleMutators, workspaces, workspaceMutators, collections, collectionMutators, tagMutators, serverMutators }) {
	const addCollection = (payload, workspaceUid) => {
		const collection = collectionSchema.parse(payload);
		const workspace = workspaces[workspaceUid];
		if (workspace) workspaceMutators.edit(workspaceUid, "collections", [...workspace.collections, collection.uid]);
		collectionMutators.add(collection);
		return collection;
	};
	const deleteCollection = (collection, workspace) => {
		if (!workspace.uid) return;
		if (collections[collection.uid]?.info?.title === "Drafts") {
			console.warn("The drafts collection cannot be deleted");
			return;
		}
		if (Object.values(collections).length === 1) {
			console.warn("You must have at least one collection");
			return;
		}
		collection.tags.forEach((uid) => tagMutators.delete(uid));
		collection.requests.forEach((uid) => {
			const request = requests[uid];
			if (!request) return;
			requestMutators.delete(uid);
			request.examples.forEach((e) => requestExamples[e] && requestExampleMutators.delete(e));
		});
		collection.servers.forEach((uid) => {
			if (uid) serverMutators.delete(uid);
		});
		workspaceMutators.edit(workspace.uid, "collections", workspace.collections.filter((uid) => uid !== collection.uid));
		collectionMutators.delete(collection.uid);
	};
	const addCollectionEnvironment = (environmentName, environment, collectionUid) => {
		const collection = collections[collectionUid];
		if (collection) {
			const currentEnvironments = collection["x-scalar-environments"] || {};
			collectionMutators.edit(collectionUid, "x-scalar-environments", {
				...currentEnvironments,
				[environmentName]: environment
			});
		}
	};
	const removeCollectionEnvironment = (environmentName, collectionUid) => {
		const collection = collections[collectionUid];
		if (collection) {
			const currentEnvironments = collection["x-scalar-environments"] || {};
			if (environmentName in currentEnvironments) {
				delete currentEnvironments[environmentName];
				collectionMutators.edit(collectionUid, "x-scalar-environments", currentEnvironments);
			}
		}
	};
	return {
		addCollection,
		deleteCollection,
		addCollectionEnvironment,
		removeCollectionEnvironment
	};
}
//#endregion
export { createStoreCollections, extendedCollectionDataFactory };

//# sourceMappingURL=collections.js.map