import { useEffect, useRef, useCallback } from "react";
import UpdateAWSStorage from "./update-aws-storage";
import isEmpty from "lodash/isEmpty";
import useAuth from "../../auth-handlers/use-auth";
import getFilesDifferences from "../../dataset-helper/get-files-differences.js";
var workingInterfaces = ["video_segmentation", "image_classification", "image_segmentation", "text_entity_recognition", "text_classification", "audio_transcription", "composite", "data_entry"];
export default (function (_ref) {
  var file = _ref.file,
      setFile = _ref.setFile;

  var _useAuth = useAuth(),
      isLoggedIn = _useAuth.isLoggedIn,
      authProvider = _useAuth.authProvider,
      authConfig = _useAuth.authConfig;

  var lastObjectRef = useRef([]);
  var shouldUpdateAWSStorage = useCallback(function () {
    if (!isLoggedIn || authProvider !== "cognito") return;
    var changes = getFilesDifferences(lastObjectRef.current, file);
    if (isEmpty(file) || !changes.content.samples && !changes.fileName || !workingInterfaces.includes(file.content.interface.type) || file.fileName === "unnamed") return false;
    return true;
  }, [file, isLoggedIn, authProvider]);
  useEffect(function () {
    if (!isLoggedIn || authProvider !== "cognito") return;

    if (!isEmpty(authConfig)) {
      console.log("should update", shouldUpdateAWSStorage());
      if (shouldUpdateAWSStorage()) UpdateAWSStorage(file);
      lastObjectRef.current = file;
    }
  }, [shouldUpdateAWSStorage, authConfig, file, isLoggedIn, authProvider]);
});