import { assertObject } from "../../../shared/src/asserts.js";
import { deepFreeze } from "../frozen-json.js";
//#region ../replicache/src/sync/patch.ts
async function apply(lc, dbWrite, patch) {
	for (const p of patch) switch (p.op) {
		case "put": {
			const frozen = deepFreeze(p.value);
			await dbWrite.put(lc, p.key, frozen);
			break;
		}
		case "update": {
			const existing = await dbWrite.get(p.key);
			const entries = [];
			const addToEntries = (toAdd) => {
				for (const [key, value] of Object.entries(toAdd)) if (!p.constrain || p.constrain.length === 0 || p.constrain.includes(key)) entries.push([key, value]);
			};
			if (existing !== void 0) {
				assertObject(existing);
				addToEntries(existing);
			}
			if (p.merge) addToEntries(p.merge);
			const frozen = deepFreeze(Object.fromEntries(entries));
			await dbWrite.put(lc, p.key, frozen);
			break;
		}
		case "del":
			if (await dbWrite.get(p.key) === void 0) continue;
			await dbWrite.del(lc, p.key);
			break;
		case "clear":
			await dbWrite.clear();
			break;
	}
}
//#endregion
export { apply };

//# sourceMappingURL=patch.js.map