import { sync as syncCache } from './cache.js';
let _byType;
function byType() {
    _byType ||= Object.groupBy(syncCache.data.objects, o => o.$type);
    return _byType;
}
const schemas = new Map();
export function useSchema(type, schema) {
    schemas.set(type, schema);
}
export function get(type) {
    const value = byType()[type] || [];
    const schema = schemas.get(type);
    if (!schema)
        return value;
    return value.map(obj => schema.parse(obj));
}
export function save(type, objects) {
    _byType ||= {};
    _byType[type] = objects;
    syncCache.data.objects = syncCache.data.objects.filter(o => o.$type !== type);
    for (const object of objects)
        syncCache.data.objects.push(Object.assign(object, { $type: type }));
}
