import { reactive } from "vue";
import { schemaModel } from "@scalar/oas-utils/helpers";
import { LS_KEYS } from "@scalar/helpers/object/local-storage";
import { tagSchema } from "@scalar/oas-utils/entities/spec";
import { mutationFactory } from "@scalar/object-utils/mutator-record";
//#region src/store/tags.ts
/** Create cookie mutators for the workspace */
function createStoreTags(useLocalStorage) {
	const tags = reactive({});
	return {
		tags,
		tagMutators: mutationFactory(tags, reactive({}), useLocalStorage && LS_KEYS.TAG)
	};
}
/**
* Create the extended mutators for tag with side effects
* TODO:
* - tag nesting, add/remove into another tag
*/
function extendedTagDataFactory({ collectionMutators, collections, requests, requestMutators, tagMutators }) {
	/** Add tag */
	const addTag = (payload, collectionUid) => {
		const collection = collections[collectionUid];
		const tag = schemaModel(payload, tagSchema, false);
		if (!tag || !collection) return console.error("INVALID TAG DATA", payload);
		collectionMutators.edit(collectionUid, "tags", [...collection.tags, tag.uid]);
		collectionMutators.edit(collectionUid, "children", [...collection.children, tag.uid]);
		tagMutators.add(tag);
		return tag;
	};
	/** Delete Tag */
	const deleteTag = (tag, collectionUid) => {
		const collection = collections[collectionUid];
		if (!collection) return;
		collectionMutators.edit(collectionUid, "tags", collection.tags.filter((uid) => uid !== tag.uid));
		collectionMutators.edit(collectionUid, "children", collection.children.filter((uid) => uid !== tag.uid));
		tag.children.forEach((childUid) => {
			const request = requests[childUid];
			if (!request) return;
			const filteredTags = request.tags?.filter((tagName) => tagName !== tag.name);
			requestMutators.edit(request.uid, "tags", filteredTags);
			if (!filteredTags?.length && !collection.children.includes(childUid)) collectionMutators.edit(collectionUid, "children", [...collection.children, childUid]);
		});
		tagMutators.delete(tag.uid);
	};
	return {
		addTag,
		deleteTag
	};
}
//#endregion
export { createStoreTags, extendedTagDataFactory };

//# sourceMappingURL=tags.js.map