import { reactive } from "vue";
import { LS_KEYS } from "@scalar/helpers/object/local-storage";
import { createExampleFromRequest } 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/request-example.ts
/** Create top level examples storage for the workspace */
function createStoreRequestExamples(useLocalStorage) {
	/**
	* Each request has multiple examples associated with it
	* An example is a set of request params that is saved to the example
	* Multiple test cases can each be saved as an example and switched between
	*/
	const requestExamples = reactive({});
	return {
		requestExamples,
		requestExampleMutators: mutationFactory(requestExamples, reactive({}), useLocalStorage && LS_KEYS.REQUEST_EXAMPLE)
	};
}
/** Extended example data that needs store context */
function extendedExampleDataFactory({ requestExamples, requestExampleMutators, requestMutators, requests }) {
	/** Ensure we add to the base examples as well as the request it is in */
	const addRequestExample = (request, _name) => {
		const example = createExampleFromRequest(request, _name ?? iterateTitle((request.summary ?? "Example") + " #1", (t) => request.examples.some((uid) => requestExamples[uid]?.name === t)));
		requestExampleMutators.add(example);
		requestMutators.edit(request.uid, "examples", [...request.examples, example.uid]);
		return example;
	};
	/** Ensure we remove from the base as well as from the request it is in */
	const deleteRequestExample = (requestExample) => {
		if (!requestExample.requestUid) return;
		requestMutators.edit(requestExample.requestUid, "examples", requests[requestExample.requestUid]?.examples.filter((uid) => uid !== requestExample.uid) || []);
		requestExampleMutators.delete(requestExample.uid);
	};
	return {
		addRequestExample,
		deleteRequestExample
	};
}
//#endregion
export { createStoreRequestExamples, extendedExampleDataFactory };

//# sourceMappingURL=request-example.js.map