import { CLIENT_LS_KEYS, safeLocalStorage } from "@scalar/helpers/object/local-storage";
import { isDefined } from "@scalar/helpers/array/is-defined";
//#region src/views/Request/RequestSection/RequestAuth/helpers/restore-auth-from-local-storage.ts
/**
* Manually restore the auth from local storage
*
* Only use this if you need to restore auth manually, in the client web + app we load the whole store from
* local storage so it isn't needed there.
*/
var restoreAuthFromLocalStorage = (store, collectionUid) => {
	try {
		const { collectionMutators, securitySchemes, securitySchemeMutators } = store;
		const auth = JSON.parse(safeLocalStorage().getItem(CLIENT_LS_KEYS.AUTH) ?? "{}");
		/** Map the security scheme name key to the uid */
		const dict = Object.keys(securitySchemes).reduce((acc, key) => {
			const scheme = securitySchemes[key];
			if (scheme) acc[scheme.nameKey] = scheme.uid;
			return acc;
		}, {});
		/** Now we can use the dict to restore the auth from local storage */
		Object.entries(auth).forEach(([key, entry]) => {
			const uid = dict[key];
			if (uid) Object.entries(entry).forEach(([path, value]) => {
				securitySchemeMutators.edit(uid, path, value);
			});
		});
		const uids = JSON.parse(safeLocalStorage().getItem(CLIENT_LS_KEYS.SELECTED_SECURITY_SCHEMES) ?? "[]").map((nameKeys) => {
			if (Array.isArray(nameKeys)) return nameKeys.map((key) => dict[key]).filter(isDefined);
			return dict[nameKeys];
		}).filter(isDefined);
		collectionMutators.edit(collectionUid, "selectedSecuritySchemeUids", uids);
	} catch (e) {
		console.error(e);
	}
};
//#endregion
export { restoreAuthFromLocalStorage };

//# sourceMappingURL=restore-auth-from-local-storage.js.map