import { createInitialRequest } from "./requests.js";
import { reactive } from "vue";
import { LS_KEYS } from "@scalar/helpers/object/local-storage";
import { collectionSchema, requestExampleSchema } from "@scalar/oas-utils/entities/spec";
import { mutationFactory } from "@scalar/object-utils/mutator-record";
import { workspaceSchema } from "@scalar/oas-utils/entities/workspace";
//#region src/store/workspace.ts
/** Create storage for workspace entities */
function createStoreWorkspaces(useLocalStorage) {
	/** Active workspace object (will be associated with an entry in the workspace collection) */
	const workspaces = reactive({});
	return {
		workspaces,
		workspaceMutators: mutationFactory(workspaces, reactive({}), useLocalStorage && LS_KEYS.WORKSPACE)
	};
}
/** Workspace entities that require store context */
function extendedWorkspaceDataFactory({ workspaces, workspaceMutators, collectionMutators, requestMutators, requestExampleMutators }) {
	const addWorkspace = (payload = {}) => {
		const { request } = createInitialRequest();
		const example = requestExampleSchema.parse({
			name: "Example",
			requestUid: request.uid
		});
		request.examples.push(example.uid);
		const collection = collectionSchema.parse({
			info: { title: "Drafts" },
			children: [request.uid],
			requests: [request.uid]
		});
		const workspace = workspaceSchema.parse({
			...payload,
			collections: [collection.uid]
		});
		requestExampleMutators.add(example);
		requestMutators.add(request);
		collectionMutators.add(collection);
		workspaceMutators.add(workspace);
		return workspace;
	};
	/** Prevent deletion of the default workspace */
	const deleteWorkspace = (uid) => {
		if (Object.keys(workspaces).length <= 1) {
			console.warn("The last workspace cannot be deleted.");
			return;
		}
		workspaceMutators.delete(uid);
	};
	return {
		addWorkspace,
		deleteWorkspace
	};
}
//#endregion
export { createStoreWorkspaces, extendedWorkspaceDataFactory };

//# sourceMappingURL=workspace.js.map