export function createMutationsApi(commit) {
    const rootTableCache = new Map();
    const apiBase = {
        $commit: async (fn, options) => {
            const { result, receipt } = await commit(fn, options);
            return { result, commit: receipt };
        },
        $table: (table) => {
            const cached = rootTableCache.get(table);
            if (cached)
                return cached;
            const tableApi = {
                async insert(values) {
                    const { result, receipt } = await commit(async (tx) => await tx[table].insert(values));
                    return { ...receipt, id: result };
                },
                async insertMany(rows) {
                    const { result, receipt } = await commit(async (tx) => await tx[table].insertMany(rows));
                    return { ...receipt, ids: result };
                },
                async update(id, patch, opts) {
                    const { receipt } = await commit(async (tx) => {
                        await tx[table].update(id, patch, opts);
                        return null;
                    });
                    return receipt;
                },
                async delete(id, opts) {
                    const { receipt } = await commit(async (tx) => {
                        await tx[table].delete(id, opts);
                        return null;
                    });
                    return receipt;
                },
                async upsert(id, patch, opts) {
                    const { receipt } = await commit(async (tx) => {
                        await tx[table].upsert(id, patch, opts);
                        return null;
                    });
                    return receipt;
                },
            };
            rootTableCache.set(table, tableApi);
            return tableApi;
        },
    };
    return new Proxy(apiBase, {
        get(target, prop) {
            if (prop === 'then')
                return undefined;
            if (typeof prop !== 'string')
                return undefined;
            if (hasOwn(target, prop)) {
                return target[prop];
            }
            return target.$table(prop);
        },
    });
}
function hasOwn(obj, key) {
    return Object.hasOwn(obj, key);
}
//# sourceMappingURL=mutations.js.map