import { fetchWrapper } from "../fetchWrapper";
import { removeEmptyFields } from "./changesObjects";

export function getRequestChangeData(
  createData,
  updateData,
  initData,
  key,
  id
) {
  let isInitDataEmpty = [...Object.keys(removeEmptyFields(initData))].length;
  if (Object.keys(removeEmptyFields(updateData)).length) {
    if (!isInitDataEmpty) {
      return fetchWrapper.post(
        `general/${key}/create/`,
        removeEmptyFields(createData)
      );
    }
    if (JSON.stringify(updateData) !== JSON.stringify(initData)) {
      return fetchWrapper.post(
        `general/${key}/${id}/update/`,
        removeEmptyFields(updateData)
      );
    }
    return Promise.resolve();
  }
  return Promise.resolve();
}
