import { promiseVoid } from "../../../shared/src/resolved-promises.js";
import { CRUD_MUTATION_NAME } from "../../../zero-protocol/src/push.js";
import * as crudImpl from "./crud-impl.js";
function makeCRUDMutateBatch(schema, repMutate) {
  if (schema.enableLegacyMutators !== true) {
    return void 0;
  }
  const { [CRUD_MUTATION_NAME]: zeroCRUD } = repMutate;
  const mutateBatch = async (body) => {
    const ops = [];
    const m = {};
    for (const name of Object.keys(schema.tables)) {
      m[name] = makeBatchCRUDMutate(name, schema, ops);
    }
    const rv = await body(m);
    await zeroCRUD({ ops });
    return rv;
  };
  return mutateBatch;
}
function addTableCRUDProperties(schema, mutate, repMutate) {
  const { [CRUD_MUTATION_NAME]: zeroCRUD } = repMutate;
  for (const [name, tableSchema] of Object.entries(schema.tables)) {
    mutate[name] = makeEntityCRUDMutate(
      name,
      tableSchema.primaryKey,
      zeroCRUD
    );
  }
}
function makeEntityCRUDMutate(tableName, primaryKey, zeroCRUD) {
  return {
    insert: (value) => {
      const op = {
        op: "insert",
        tableName,
        primaryKey,
        value
      };
      return zeroCRUD({ ops: [op] });
    },
    upsert: (value) => {
      const op = {
        op: "upsert",
        tableName,
        primaryKey,
        value
      };
      return zeroCRUD({ ops: [op] });
    },
    update: (value) => {
      const op = {
        op: "update",
        tableName,
        primaryKey,
        value
      };
      return zeroCRUD({ ops: [op] });
    },
    delete: (id) => {
      const op = {
        op: "delete",
        tableName,
        primaryKey,
        value: id
      };
      return zeroCRUD({ ops: [op] });
    }
  };
}
function makeBatchCRUDMutate(tableName, schema, ops) {
  const { primaryKey } = schema.tables[tableName];
  return {
    insert: (value) => {
      const op = {
        op: "insert",
        tableName,
        primaryKey,
        value
      };
      ops.push(op);
      return promiseVoid;
    },
    upsert: (value) => {
      const op = {
        op: "upsert",
        tableName,
        primaryKey,
        value
      };
      ops.push(op);
      return promiseVoid;
    },
    update: (value) => {
      const op = {
        op: "update",
        tableName,
        primaryKey,
        value
      };
      ops.push(op);
      return promiseVoid;
    },
    delete: (id) => {
      const op = {
        op: "delete",
        tableName,
        primaryKey,
        value: id
      };
      ops.push(op);
      return promiseVoid;
    }
  };
}
function makeCRUDExecutor(tx, schema, ivmBranch) {
  return (tableName, kind, value) => {
    const { primaryKey } = schema.tables[tableName];
    return crudImpl[kind](
      tx,
      // oxlint-disable-next-line @typescript-eslint/no-explicit-any
      { op: kind, tableName, primaryKey, value },
      schema,
      ivmBranch
    );
  };
}
function makeCRUDMutator(schema) {
  return async (tx, crudArg) => {
    const executor = makeCRUDExecutor(tx, schema, void 0);
    for (const op of crudArg.ops) {
      await executor(op.tableName, op.op, op.value);
    }
  };
}
export {
  addTableCRUDProperties,
  makeBatchCRUDMutate,
  makeCRUDExecutor,
  makeCRUDMutateBatch,
  makeCRUDMutator
};
//# sourceMappingURL=crud.js.map
