import { schemaToSelvaBuffer, updateTypeDefs } from '@based/schema/def';
import { execCtxQueue } from './flushModify.js';
export const setLocalClientSchema = (client, schema) => {
    if (client.schema && client.schema.hash === schema.hash) {
        return client.schema;
    }
    const { schemaTypesParsed, schemaTypesParsedById } = updateTypeDefs(schema);
    client.schema = schema;
    client.schemaTypesParsed = schemaTypesParsed;
    client.schemaTypesParsedById = schemaTypesParsedById;
    // Adds bidrectional refs on defs
    schemaToSelvaBuffer(client.schemaTypesParsed);
    // this has to happen before the listeners
    if (client.modifyCtx.len > 8) {
        console.info('Modify cancelled - schema updated');
    }
    // cancel modify queue
    const resCtx = client.modifyCtx.ctx;
    client.modifyCtx.reset();
    execCtxQueue(resCtx, true);
    // resubscribe
    for (const [q, store] of client.subs) {
        store.resubscribe(q);
    }
    process.nextTick(() => {
        client.emit('schema', schema);
    });
    return schema;
};
//# sourceMappingURL=setLocalClientSchema.js.map