import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { useState } from "react";
import { HeaderContext } from "../Header";
import StartingPage from "../StartingPage";
import DatasetEditor from "../DatasetEditor";
import ErrorToasts from "../ErrorToasts";
import useErrors from "../../hooks/use-errors.js";
import LocalStorageDatasetManager from "udt-dataset-managers/dist/LocalStorageDatasetManager.js";
import useActiveDatasetManager from "../../hooks/use-active-dataset-manager";
import AppErrorBoundary from "../AppErrorBoundary";
import useEventCallback from "use-event-callback";
import usePreventNavigation from "../../hooks/use-prevent-navigation";
import { FileContext } from "../FileContext";
import usePosthog from "../../hooks/use-posthog";
import ManagePluginsDialog from "../ManagePluginsDialog";
import usePluginProvider from "../PluginProvider";
import download from "in-browser-download";
import toUDTCSV from "../../utils/to-udt-csv";
import useEnterCollaborativeSession from "../../hooks/use-enter-collaborative-session";
import useOpenFile from "../../hooks/use-open-file";
import useDesktopMenuFunctions from "../../hooks/use-desktop-menu-functions";
export default (function () {
  useEnterCollaborativeSession();
  useDesktopMenuFunctions();
  usePluginProvider();

  var _useActiveDatasetMana = useActiveDatasetManager(),
      _useActiveDatasetMana2 = _slicedToArray(_useActiveDatasetMana, 2),
      datasetManager = _useActiveDatasetMana2[0],
      setDatasetManager = _useActiveDatasetMana2[1];

  usePreventNavigation(Boolean(datasetManager));
  var posthog = usePosthog();

  var _useErrors = useErrors(),
      _useErrors2 = _slicedToArray(_useErrors, 1),
      errors = _useErrors2[0];

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      managePluginsDialogOpen = _useState2[0],
      setManagePluginsDialogOpen = _useState2[1];

  var _useState3 = useState("complete"),
      _useState4 = _slicedToArray(_useState3, 2),
      selectedBrush = _useState4[0],
      setSelectedBrush = _useState4[1];

  var onCreateTemplate = useEventCallback( /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(template) {
      var dm;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              dm = new LocalStorageDatasetManager();
              _context.next = 3;
              return dm.setDataset(_objectSpread(_objectSpread({}, template.dataset), {}, {
                name: "New ".concat(template.dataset.interface.type, " Dataset")
              }));

            case 3:
              setDatasetManager(dm);

            case 4:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function (_x) {
      return _ref.apply(this, arguments);
    };
  }());
  var openRecentItem = useEventCallback(function (item) {// TODO, recent items is currently non-functional
  });
  var onClickHome = useEventCallback(function () {
    setDatasetManager(null);
  });
  var onClickManagePlugins = useEventCallback(function () {
    return setManagePluginsDialogOpen(true);
  });
  var onDownload = useEventCallback( /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(format) {
      var ds, userProvidedFileName, outputName;
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              posthog.capture("download_file", {
                file_type: format
              });
              _context2.next = 3;
              return datasetManager.getDataset();

            case 3:
              ds = _context2.sent;
              userProvidedFileName = (datasetManager.sessionId || ds.name || "MyDataset").replace(/\.udt\.(csv|json)/, "");
              outputName = userProvidedFileName + ".udt." + format;

              if (format === "json") {
                download(JSON.stringify(ds), outputName);
              } else if (format === "csv") {
                download(toUDTCSV(ds), outputName);
              }

            case 7:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));

    return function (_x2) {
      return _ref2.apply(this, arguments);
    };
  }());
  var openFile = useOpenFile();

  var _useState5 = useState(false),
      _useState6 = _slicedToArray(_useState5, 2),
      sessionBoxOpen = _useState6[0],
      changeSessionBoxOpen = _useState6[1];

  var isWelcomePage = !datasetManager;
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(FileContext.Provider, {
    value: {}
    /*{ file, setFile }*/

  }, /*#__PURE__*/React.createElement(HeaderContext.Provider, {
    value: {
      recentItems: [],
      changeRecentItems: function changeRecentItems() {},
      onClickTemplate: onCreateTemplate,
      onClickHome: onClickHome,
      onClickManagePlugins: onClickManagePlugins,
      onOpenFile: openFile,
      onOpenRecentItem: openRecentItem,
      sessionBoxOpen: sessionBoxOpen,
      changeSessionBoxOpen: changeSessionBoxOpen,
      // onCreateSession: makeSession,
      fileOpen: Boolean(datasetManager),
      onDownload: onDownload,
      onChangeSelectedBrush: setSelectedBrush,
      selectedBrush: selectedBrush,
      isWelcomePage: isWelcomePage
    }
  }, /*#__PURE__*/React.createElement("div", null, isWelcomePage ? /*#__PURE__*/React.createElement(StartingPage, {
    onFileDrop: openFile,
    onOpenTemplate: onCreateTemplate,
    recentItems: [],
    onOpenRecentItem: openRecentItem,
    onClickOpenSession: function onClickOpenSession() {
      return changeSessionBoxOpen(true);
    }
  }) : /*#__PURE__*/React.createElement(AppErrorBoundary, null, /*#__PURE__*/React.createElement(DatasetEditor, {
    selectedBrush: selectedBrush,
    recentItems: []
    /* recentItems */

  })), /*#__PURE__*/React.createElement(ManagePluginsDialog, {
    open: managePluginsDialogOpen,
    onClose: function onClose() {
      return setManagePluginsDialogOpen(false);
    }
  })))), /*#__PURE__*/React.createElement(ErrorToasts, {
    errors: errors
  }));
});