import {} from '@rocicorp/logger';
import { assert } from "../../shared/src/asserts.js";
import {} from "../../zero-protocol/src/push.js";
import { splitMutatorKey } from "../../zql/src/mutate/custom.js";
import { handleMutationRequest, } from "../../zero-server/src/process-mutations.js";
import { must } from "../../shared/src/must.js";
export class PushProcessor {
    #dbProvider;
    #logLevel;
    constructor(dbProvider, logLevel = 'info') {
        this.#dbProvider = dbProvider;
        this.#logLevel = logLevel;
    }
    process(mutators, queryOrQueryString, body) {
        if (queryOrQueryString instanceof Request) {
            return handleMutationRequest((transact, mutations) => this.#processMutation(mutators, transact, mutations), queryOrQueryString, this.#logLevel);
        }
        return handleMutationRequest((transact, mutation) => this.#processMutation(mutators, transact, mutation), queryOrQueryString, must(body), this.#logLevel);
    }
    #processMutation(mutators, transact, _mutation) {
        return transact(this.#dbProvider, (tx, name, args) => this.#dispatchMutation(mutators, tx, name, args));
    }
    #dispatchMutation(mutators, dbTx, key, args) {
        const [namespace, name] = splitMutatorKey(key);
        if (name === undefined) {
            const mutator = mutators[namespace];
            assert(typeof mutator === 'function', () => `could not find mutator ${key}`);
            return mutator(dbTx, args);
        }
        const mutatorGroup = mutators[namespace];
        assert(typeof mutatorGroup === 'object', () => `could not find mutators for namespace ${namespace}`);
        const mutator = mutatorGroup[name];
        assert(typeof mutator === 'function', () => `could not find mutator ${key}`);
        return mutator(dbTx, args);
    }
}
//# sourceMappingURL=push-processor.js.map