// Applies a form's pending attachment change to the record it has just saved,
// and returns the record the form should hand back to its parent.
//
// This necessarily runs after the metadata write: the file endpoints address a
// record that must already exist. The order means a create-with-file is two
// calls, and the second one failing leaves a saved record with no attachment —
// which is why the caller reports it as an attachment error rather than a failed
// save, because the metadata is already committed.
export async function applyAttachment(files, saved, { file, remove }) {
  if (file) return files.upload(saved.id, file);
  if (remove) {
    await files.remove(saved.id);
    return { ...saved, fileName: "", hasFile: false };
  }
  return saved;
}
