import { reactive } from "vue";
import { LS_KEYS } from "@scalar/helpers/object/local-storage";
import { securitySchemeSchema } from "@scalar/oas-utils/entities/spec";
import { mutationFactory } from "@scalar/object-utils/mutator-record";
//#region src/store/security-schemes.ts
/** Create storage entities for security schemes */
function createStoreSecuritySchemes(useLocalStorage) {
	const securitySchemes = reactive({});
	return {
		securitySchemes,
		securitySchemeMutators: mutationFactory(securitySchemes, reactive({}), useLocalStorage && LS_KEYS.SECURITY_SCHEME)
	};
}
/** Extended mutators and data for security schemas */
function extendedSecurityDataFactory({ securitySchemeMutators, collectionMutators, collections, requests, requestMutators }) {
	/** Adds a security scheme and appends it to either a collection or a request */
	const addSecurityScheme = (payload, collectionUid) => {
		const scheme = securitySchemeSchema.parse(payload);
		securitySchemeMutators.add(scheme);
		if (collectionUid && collections[collectionUid]) collectionMutators.edit(collectionUid, "securitySchemes", [...collections[collectionUid].securitySchemes, scheme.uid]);
		return scheme;
	};
	/** Delete a security scheme and remove the key from its corresponding parent */
	const deleteSecurityScheme = (schemeUid) => {
		Object.values(collections).forEach((c) => {
			if (c.securitySchemes.includes(schemeUid)) collectionMutators.edit(c.uid, "securitySchemes", c.securitySchemes.filter((s) => s !== schemeUid));
		});
		Object.values(requests).forEach((r) => {
			if (r.security?.some((s) => Object.keys(s).includes(schemeUid))) requestMutators.edit(r.uid, "security", requests[r.uid]?.security?.filter((s) => !Object.keys(s).includes(schemeUid)));
			if (r.selectedSecuritySchemeUids.flat().includes(schemeUid)) requestMutators.edit(r.uid, "selectedSecuritySchemeUids", r.selectedSecuritySchemeUids?.filter((uid) => {
				if (Array.isArray(uid)) return !uid.includes(schemeUid);
				return uid !== schemeUid;
			}));
		});
		securitySchemeMutators.delete(schemeUid);
	};
	return {
		addSecurityScheme,
		deleteSecurityScheme
	};
}
//#endregion
export { createStoreSecuritySchemes, extendedSecurityDataFactory };

//# sourceMappingURL=security-schemes.js.map