import { BindingError } from "@contember/binding";
const toLatestFormat = (editor, potentiallyOldNode) => {
  if (potentiallyOldNode.formatVersion === editor.formatVersion) {
    return potentiallyOldNode;
  }
  if (potentiallyOldNode.formatVersion > editor.formatVersion) {
    return potentiallyOldNode;
  }
  let formatNumber = potentiallyOldNode.formatVersion;
  try {
    for (; formatNumber < editor.formatVersion; formatNumber++) {
      potentiallyOldNode = {
        formatVersion: formatNumber + 1,
        children: potentiallyOldNode.children.map((oldChild) => editor.upgradeFormatBySingleVersion(oldChild, formatNumber))
      };
    }
    return potentiallyOldNode;
  } catch (e) {
    const genericMessage = `Failed to upgrade editor format from version ${formatNumber}.`;
    if (e instanceof RangeError) {
      throw new BindingError(`${genericMessage}
Detected a stack overflow. Perhaps you incorrectly implemented 'upgradeFormatBySingleVersion'?`);
    }
    throw new BindingError(genericMessage);
  }
};
export { toLatestFormat };
//# sourceMappingURL=toLatestFormat.js.map
