import {
  TRANSACT_WRITE_ITEM_LIMIT,
  executeMultiWrite
} from "./chunk-QJDHVMKT.mjs";
import {
  getDynamoControlService
} from "./chunk-EUIP2U5F.mjs";

// src/data/operations/control/rename-cascade/rename-cascade-rewrite-chunk-operation.ts
var RENAME_CASCADE_MAX_TARGETS_PER_CHUNK = 50;
async function rewriteRenameCascadeChunkOperation(params) {
  const { targets, tableName, token } = params;
  if (targets.length === 0) {
    return { targetsRewritten: 0, transactItemCount: 0 };
  }
  if (targets.length > RENAME_CASCADE_MAX_TARGETS_PER_CHUNK) {
    throw new Error(
      `rewriteRenameCascadeChunkOperation: chunk has ${targets.length} targets; limit is ${RENAME_CASCADE_MAX_TARGETS_PER_CHUNK}`
    );
  }
  const triples = [];
  for (const target of targets) {
    if (target.skRewriteRequired) {
      triples.push({
        entity: target.entity,
        action: "delete",
        item: { ...target.oldKey }
      });
      triples.push({
        entity: target.entity,
        action: "put",
        item: { ...target.newItem }
      });
    } else {
      triples.push({
        entity: target.entity,
        action: "put",
        item: { ...target.newItem }
      });
    }
  }
  if (triples.length > TRANSACT_WRITE_ITEM_LIMIT) {
    throw new Error(
      `rewriteRenameCascadeChunkOperation: chunk expanded to ${triples.length} transact items; DynamoDB TransactWriteItems is limited to ${TRANSACT_WRITE_ITEM_LIMIT}`
    );
  }
  const service = getDynamoControlService(tableName);
  await executeMultiWrite({ service, triples, token });
  return {
    targetsRewritten: targets.length,
    transactItemCount: triples.length
  };
}
function chunkRenameCascadeTargets(targets) {
  const chunks = [];
  for (let i = 0; i < targets.length; i += RENAME_CASCADE_MAX_TARGETS_PER_CHUNK) {
    chunks.push(targets.slice(i, i + RENAME_CASCADE_MAX_TARGETS_PER_CHUNK));
  }
  return chunks;
}

export {
  rewriteRenameCascadeChunkOperation,
  chunkRenameCascadeTargets
};
//# sourceMappingURL=chunk-YPTJJ35S.mjs.map