import { environmentSchema } from "@scalar/oas-utils/entities/environment";
import { schemaModel } from "@scalar/oas-utils/helpers";
import { collectionSchema, requestExampleSchema, requestSchema, securitySchemeSchema, serverSchema, tagSchema } from "@scalar/oas-utils/entities/spec";
import { workspaceSchema } from "@scalar/oas-utils/entities/workspace";
import { DATA_VERSION, DATA_VERSION_LS_LEY, migrator } from "@scalar/oas-utils/migrations";
import { cookieSchema } from "@scalar/oas-utils/entities/cookie";
//#region src/libs/local-storage.ts
/** Loads the migrated resource into the mutator safely */
var loadResources = (resources, schema, add) => resources.forEach((payload) => {
	const resource = schemaModel(payload, schema, false);
	if (resource) add(resource);
});
/**
* Loads all resources from localStorage and applies any migrations, then loads into mutators
* We use the raw mutator.add here instead of the custom ones because we do NOT want any side effects
*
* Currently not working for workspace
*/
var loadAllResources = (mutators) => {
	const { collectionMutators, cookieMutators, environmentMutators, tagMutators, requestExampleMutators, requestMutators, serverMutators, securitySchemeMutators, workspaceMutators } = mutators;
	try {
		const { collections, cookies, environments, requestExamples, requests, servers, securitySchemes, tags, workspaces } = migrator().arrays;
		loadResources(collections, collectionSchema, collectionMutators.rawAdd);
		loadResources(cookies, cookieSchema, cookieMutators.add);
		loadResources(environments, environmentSchema, environmentMutators.add);
		loadResources(requestExamples, requestExampleSchema, requestExampleMutators.rawAdd);
		loadResources(requests, requestSchema, requestMutators.rawAdd);
		loadResources(servers, serverSchema, serverMutators.rawAdd);
		loadResources(securitySchemes, securitySchemeSchema, securitySchemeMutators.rawAdd);
		loadResources(tags, tagSchema, tagMutators.rawAdd);
		loadResources(workspaces, workspaceSchema, workspaceMutators.rawAdd);
		localStorage.setItem(DATA_VERSION_LS_LEY, DATA_VERSION);
	} catch (e) {
		console.error(e);
	}
};
//#endregion
export { loadAllResources };

//# sourceMappingURL=local-storage.js.map