import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import { useEffect } from "react";
import useEventCallback from "use-event-callback";
import useElectron from "../../hooks/use-electron.js";
import { useToasts } from "../../components/Toasts";
import { setIn } from "seamless-immutable";
import toUDTCSV from "../to-udt-csv.js";
import useIsDesktop from "../../hooks/use-is-desktop";
var webReturn = {
  saveFile: function saveFile() {
    return null;
  }
};
export default (function (file, changeFile) {
  var isDesktop = useIsDesktop();
  if (!isDesktop) return webReturn;

  var _useToasts = useToasts(),
      addToast = _useToasts.addToast;

  var _useElectron = useElectron(),
      remote = _useElectron.remote;

  var saveFile = useEventCallback(function () {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        _ref$saveAs = _ref.saveAs,
        saveAs = _ref$saveAs === void 0 ? false : _ref$saveAs;

    if (!file) return;

    function saveFileAsync() {
      return _saveFileAsync.apply(this, arguments);
    }

    function _saveFileAsync() {
      _saveFileAsync = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var filePath, _yield$remote$dialog$, cancelled, newFilePath;

        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                filePath = file.filePath;

                if (!(!file.filePath || saveAs)) {
                  _context.next = 12;
                  break;
                }

                _context.next = 4;
                return remote.dialog.showSaveDialog({
                  filters: [{
                    name: ".udt.json",
                    extensions: ["udt.json"]
                  }, {
                    name: ".udt.csv",
                    extensions: ["udt.csv"]
                  }]
                });

              case 4:
                _yield$remote$dialog$ = _context.sent;
                cancelled = _yield$remote$dialog$.cancelled;
                newFilePath = _yield$remote$dialog$.filePath;
                filePath = !newFilePath || newFilePath.endsWith(".json") || newFilePath.endsWith(".csv") ? newFilePath : "".concat(newFilePath, ".udt.json");

                if (!(cancelled || !filePath)) {
                  _context.next = 11;
                  break;
                }

                addToast("Could not save");
                return _context.abrupt("return");

              case 11:
                changeFile(_objectSpread(_objectSpread({}, file), {}, {
                  filePath: filePath,
                  fileName: filePath.split("/").slice(-1)[0]
                }));

              case 12:
                _context.next = 14;
                return remote.require("fs").promises.writeFile(filePath, filePath.endsWith(".csv") ? toUDTCSV(file.content) : JSON.stringify(file.content, null, "  "));

              case 14:
                addToast("File Saved!");

              case 15:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));
      return _saveFileAsync.apply(this, arguments);
    }

    saveFileAsync();
  });
  useEffect(function () {
    if (!file || !file.filePath) return;

    if (file.fileName !== file.filePath.split("/").slice(-1)[0]) {
      changeFile(setIn(file, ["filePath"], file.filePath.split("/").slice(0, -1).concat([file.fileName + (file.fileName.endsWith(".json") ? "" : ".dataset.json")]).join("/")));
    }
  }, [file, changeFile]);
  return {
    saveFile: saveFile
  };
});