import { reactive } from "vue";
import { schemaModel } from "@scalar/oas-utils/helpers";
import { LS_KEYS } from "@scalar/helpers/object/local-storage";
import { createExampleFromRequest, requestSchema } from "@scalar/oas-utils/entities/spec";
import { mutationFactory } from "@scalar/object-utils/mutator-record";
import { iterateTitle } from "@scalar/helpers/string/iterate-title";
//#region src/store/requests.ts
/** Create top level request handlers for a workspace */
function createStoreRequests(useLocalStorage) {
	/** Local list of all requests (will be associated with a database collection) */
	const requests = reactive({});
	return {
		requests,
		requestMutators: mutationFactory(requests, reactive({}), useLocalStorage && LS_KEYS.REQUEST)
	};
}
/**
* Create the extended mutators for request where access to the workspace is required
*/
function extendedRequestDataFactory({ requestExamples, requestExampleMutators, requestMutators, collectionMutators, collections, tags, tagMutators }, addTag) {
	/** Add request */
	const addRequest = (payload, collectionUid) => {
		const request = schemaModel(payload, requestSchema, false);
		if (!request) return console.error("INVALID REQUEST DATA", payload);
		const collection = collections[collectionUid];
		const example = createExampleFromRequest(request, iterateTitle((request.summary ?? "Example") + " #1", (t) => request.examples.some((uid) => t === requestExamples[uid]?.name)));
		request.examples.push(example.uid);
		requestMutators.add(request);
		requestExampleMutators.add(example);
		if (collection) collectionMutators.edit(collectionUid, "requests", [...collection.requests, request.uid]);
		if (request.tags?.length) request.tags.forEach((tagName) => {
			const tagUid = collection?.tags.find((uid) => tags[uid]?.name === tagName);
			if (tagUid && tags[tagUid]) tagMutators.edit(tagUid, "children", [...tags[tagUid].children, request.uid]);
			else addTag({
				name: tagName,
				children: [request.uid]
			}, collectionUid);
		});
		else if (collection) collectionMutators.edit(collectionUid, "children", [...collection.children, request.uid]);
		return request;
	};
	/** Delete request */
	const deleteRequest = (request, collectionUid) => {
		const collection = collections[collectionUid];
		request.examples.forEach((uid) => requestExampleMutators.delete(uid));
		if (collection) {
			collectionMutators.edit(collectionUid, "requests", collection.requests.filter((r) => r !== request.uid));
			collectionMutators.edit(collectionUid, "children", collection.children.filter((r) => r !== request.uid));
			request.tags?.forEach((tagName) => {
				const tagUid = collection.tags.find((uid) => tags[uid]?.name === tagName);
				if (!tagUid) return;
				tagMutators.edit(tagUid, "children", tags[tagUid]?.children.filter((r) => r !== request.uid) || []);
			});
		}
		requestMutators.delete(request.uid);
	};
	return {
		addRequest,
		deleteRequest,
		findRequestParents: findRequestParentsFactory({
			collections,
			tags
		})
	};
}
/** Factory function to allow testing of the function */
function findRequestParentsFactory({ collections, tags }) {
	/** Recursively find all parent folders (tags and collections) of a request */
	function findRequestParentss(r) {
		const collection = Object.values(collections).find((c) => c.requests?.includes(r.uid));
		if (!collection) return [];
		const tagChildren = Object.keys(tags).reduce((obj, uid) => {
			obj[uid] = [];
			return obj;
		}, { [collection?.uid]: [] });
		function addChildren(current, parentUids) {
			parentUids.forEach((p) => tagChildren[p]?.push(...current.children));
			current.children.forEach((t) => {
				if (tags[t]) addChildren(tags[t], [...parentUids, t]);
			});
		}
		addChildren(collection, [collection.uid]);
		const parents = /* @__PURE__ */ new Set();
		Object.entries(tagChildren).forEach(([tagUid, totalChildren]) => {
			if (totalChildren.includes(r.uid)) parents.add(tagUid);
		});
		return [...parents];
	}
	return findRequestParentss;
}
/** First draft request" */
function createInitialRequest() {
	return { request: requestSchema.parse({
		method: "get",
		parameters: [],
		path: "",
		summary: "My First Request",
		examples: []
	}) };
}
//#endregion
export { createInitialRequest, createStoreRequests, extendedRequestDataFactory };

//# sourceMappingURL=requests.js.map