import { CRUD_MUTATION_NAME } from "../../../zero-protocol/src/push.js";
import { customMutatorKey } from "../../../zql/src/mutate/custom.js";
import { isMutatorRegistry } from "../../../zql/src/mutate/mutator-registry.js";
import { isMutator } from "../../../zql/src/mutate/mutator.js";
import { Internal } from "./client-error-kind-enum.js";
import { makeCRUDMutator } from "./crud.js";
import { TransactionImpl, makeReplicacheMutator as makeReplicacheMutator$1 } from "./custom.js";
import { ClientError } from "./error.js";
function extendReplicacheMutators(lc, context, mutators, schema, mutateObject) {
  const processMutators = (mutators2, path) => {
    for (const [key, mutator] of Object.entries(mutators2)) {
      if (key === "~") {
        continue;
      }
      path.push(key);
      if (isMutator(mutator)) {
        const fullKey = customMutatorKey(".", path);
        mutateObject[fullKey] = makeReplicacheMutator(
          lc,
          mutator,
          schema,
          context
        );
      } else if (typeof mutator === "function") {
        const fullKey = customMutatorKey("|", path);
        mutateObject[fullKey] = makeReplicacheMutator$1(
          lc,
          // oxlint-disable-next-line no-explicit-any
          mutator,
          schema,
          context
        );
      } else if (mutator !== null && typeof mutator === "object") {
        processMutators(mutator, path);
      }
      path.pop();
    }
  };
  processMutators(mutators, []);
}
function makeReplicacheMutator(lc, mutator, schema, context) {
  return async (repTx, args) => {
    const tx = new TransactionImpl(lc, repTx, schema);
    await mutator.fn({
      args,
      ctx: context,
      tx
    });
  };
}
function makeReplicacheMutators(schema, mutators, context, lc) {
  const { enableLegacyMutators = false } = schema;
  const replicacheMutators = {
    [CRUD_MUTATION_NAME]: enableLegacyMutators ? makeCRUDMutator(schema) : (
      // TODO(arv): This code is unreachable since the public API prevents
      // calling CRUD mutators when enableLegacyMutators is false. Remove this.
      (() => Promise.reject(
        new ClientError({
          kind: Internal,
          message: "Zero CRUD mutators are not enabled."
        })
      ))
    )
  };
  if (mutators) {
    if (isMutatorRegistry(mutators)) {
      extendFromMutatorRegistry(
        lc,
        context,
        mutators,
        schema,
        replicacheMutators
      );
    } else {
      extendReplicacheMutators(
        lc,
        context,
        mutators,
        schema,
        replicacheMutators
      );
    }
  }
  return replicacheMutators;
}
function extendFromMutatorRegistry(lc, context, registry, schema, mutateObject) {
  const walk = (node) => {
    if (typeof node !== "object" || node === null) {
      return;
    }
    for (const value of Object.values(node)) {
      if (isMutator(value)) {
        mutateObject[value.mutatorName] = (repTx, args) => {
          const tx = new TransactionImpl(lc, repTx, schema);
          return value.fn({ args, ctx: context, tx });
        };
      } else if (typeof value === "object" && value !== null) {
        walk(value);
      }
    }
  };
  walk(registry);
}
export {
  extendReplicacheMutators,
  makeReplicacheMutators
};
//# sourceMappingURL=make-replicache-mutators.js.map
