import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { useState, createContext } from "react";
import MuiButton from "@material-ui/core/Button";
import { styled } from "@material-ui/core/styles";
import AssignmentReturnedIcon from "@material-ui/icons/AssignmentReturned";
import CreateNewFolderIcon from "@material-ui/icons/CreateNewFolder";
import TextFieldsIcon from "@material-ui/icons/TextFields";
import DescriptionIcon from "@material-ui/icons/Description";
import PetsIcon from "@material-ui/icons/Pets";
import * as colors from "@material-ui/core/colors";
import PasteUrlsDialog from "../PasteUrlsDialog";
import ImportFromCognitoS3Dialog from "../ImportFromCognitoS3Dialog";
import ImportFromS3Dialog from "../ImportFromS3Dialog";
import UploadToS3Dialog from "../UploadToS3Dialog";
import ImportTextSnippetsDialog from "../ImportTextSnippetsDialog";
import useElectron from "../../hooks/use-electron";
import classnames from "classnames";
import S3Icon from "./S3Icon";
import isEmpty from "lodash/isEmpty";
import useEventCallback from "use-event-callback";
import ImportFromGoogleDriveDialog from "../ImportFromGoogleDriveDialog";
import ImportUDTFileDialog from "../ImportUDTFileDialog";
import ImportToyDataset from "../ImportToyDatasetDialog";
import ImportFromYoutubeUrls from "../ImportFromYoutubeUrls";
import ImportFromCOCODialog from "../ImportFromCOCODialog";
import { FaGoogleDrive, FaYoutube } from "react-icons/fa";
import usePosthog from "../../hooks/use-posthog";
import promptAndGetSamplesFromLocalDirectory from "./prompt-and-get-samples-from-local-directory.js";
import { useTranslation } from "react-i18next";
import useAuth from "../../utils/auth-handlers/use-auth.js";
import { useAppConfig } from "../AppConfig";
import PhotoLibraryIcon from "@material-ui/icons/PhotoLibrary";
import useInterface from "../../hooks/use-interface";
import useAddSamples from "../../hooks/use-add-samples";
import useDatasetProperty from "../../hooks/use-dataset-property";
var ButtonBase = styled(MuiButton)({
  width: 240,
  height: 140,
  display: "inline-flex",
  flexDirection: "column",
  "&.disabled": {
    backgroundColor: colors.grey[200]
  },
  margin: 8,
  "& .icon": {
    width: 48,
    height: 48,
    color: colors.grey[600],
    "&.disabled": {
      color: colors.grey[400]
    }
  }
});
var DesktopOnlyText = styled("div")({
  fontSize: 11,
  fontWeight: "bold",
  color: colors.grey[600],
  "&.disabled": {
    color: colors.grey[500]
  }
});
var SelectDialogContext = createContext();

var Button = function Button(_ref) {
  var Icon = _ref.Icon,
      desktopOnly = _ref.desktopOnly,
      isDesktop = _ref.isDesktop,
      children = _ref.children,
      dialog = _ref.dialog,
      authConfiguredOnly = _ref.authConfiguredOnly,
      signedInOnly = _ref.signedInOnly,
      disabledReason = _ref.disabledReason,
      user = _ref.user,
      onlySupportType = _ref.onlySupportType,
      type = _ref.type;
  var posthog = usePosthog();

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

  var isDisabled = function isDisabled() {
    if (disabledReason) {
      return {
        disabled: true,
        disabledText: disabledReason
      };
    } else if (desktopOnly) {
      return {
        disabled: !isDesktop,
        disabledText: "DESKTOP ONLY"
      };
    } else if (onlySupportType && !onlySupportType.includes(type)) {
      return {
        disabled: true,
        disabledText: "DOESN'T SUPPORT THIS INTERFACE"
      };
    } else if (authConfiguredOnly && !isLoggedIn) {
      if (signedInOnly) {
        return {
          disabled: isEmpty(user),
          disabledText: "MUST BE SIGNED IN"
        };
      } else {
        return {
          disabled: isEmpty(authConfig),
          disabledText: "AUTH MUST BE CONFIGURED"
        };
      }
    } else {
      return {
        disabled: false,
        disabledText: ""
      };
    }
  };

  var _isDisabled = isDisabled(),
      disabled = _isDisabled.disabled,
      disabledText = _isDisabled.disabledText;

  return /*#__PURE__*/React.createElement(SelectDialogContext.Consumer, null, function (_ref2) {
    var onChangeDialog = _ref2.onChangeDialog;
    return /*#__PURE__*/React.createElement(ButtonBase, {
      onClick: function onClick() {
        posthog.capture("import_button_clicked", {
          import_button: dialog
        });
        onChangeDialog(dialog);
      },
      className: classnames({
        disabled: disabled
      }),
      variant: "outlined",
      disabled: disabled
    }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Icon, {
      className: classnames("icon", {
        disabled: disabled
      })
    }), /*#__PURE__*/React.createElement("div", null, children), disabled && /*#__PURE__*/React.createElement(DesktopOnlyText, {
      className: classnames({
        disabled: disabled
      })
    }, disabledText)));
  });
};

export default (function (_ref3) {
  var isDesktop = _ref3.isDesktop,
      authConfig = _ref3.authConfig,
      user = _ref3.user;

  var _useTranslation = useTranslation(),
      t = _useTranslation.t;

  var _useState = useState(),
      _useState2 = _slicedToArray(_useState, 2),
      selectedDialog = _useState2[0],
      changeDialog = _useState2[1];

  var electron = useElectron();

  var _useAppConfig = useAppConfig(),
      fromConfig = _useAppConfig.fromConfig;

  var _useInterface = useInterface(),
      iface = _useInterface.iface;

  var _useDatasetProperty = useDatasetProperty("usedToyDataset"),
      updateUsedToyDataset = _useDatasetProperty.updateUsedToyDataset;

  var addSamples = useAddSamples();

  var onChangeDialog = /*#__PURE__*/function () {
    var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(dialog) {
      var localSamples;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.t0 = dialog;
              _context.next = _context.t0 === "upload-directory" ? 3 : 12;
              break;

            case 3:
              if (electron) {
                _context.next = 5;
                break;
              }

              return _context.abrupt("return");

            case 5:
              _context.next = 7;
              return promptAndGetSamplesFromLocalDirectory({
                electron: electron
              });

            case 7:
              localSamples = _context.sent;

              if (!(typeof localSamples === "undefined")) {
                _context.next = 10;
                break;
              }

              return _context.abrupt("return");

            case 10:
              addSamples(localSamples);
              return _context.abrupt("return");

            case 12:
              return _context.abrupt("return", changeDialog(dialog));

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

    return function onChangeDialog(_x) {
      return _ref4.apply(this, arguments);
    };
  }();

  var closeDialog = function closeDialog() {
    return changeDialog(null);
  };

  var onAddSamples = useEventCallback( /*#__PURE__*/function () {
    var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(samplesToAdd) {
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return addSamples(samplesToAdd);

            case 2:
              closeDialog();

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

    return function (_x2) {
      return _ref5.apply(this, arguments);
    };
  }());
  var onAddSamplesAsToyDataset = useEventCallback( /*#__PURE__*/function () {
    var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(samplesToAdd) {
      return _regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _context3.next = 2;
              return addSamples(samplesToAdd);

            case 2:
              _context3.next = 4;
              return updateUsedToyDataset(true);

            case 4:
              closeDialog();

            case 5:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    }));

    return function (_x3) {
      return _ref6.apply(this, arguments);
    };
  }());
  return /*#__PURE__*/React.createElement(SelectDialogContext.Provider, {
    value: {
      onChangeDialog: onChangeDialog
    }
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Button, {
    isDesktop: isDesktop,
    dialog: "paste-image-urls",
    Icon: AssignmentReturnedIcon,
    authConfig: authConfig,
    signedInOnly: false,
    user: user
  }, t("paste-urls")), /*#__PURE__*/React.createElement(Button, {
    desktopOnly: true,
    isDesktop: isDesktop,
    dialog: "upload-directory",
    Icon: CreateNewFolderIcon,
    authConfig: authConfig,
    signedInOnly: false,
    user: user
  }, t("files-from-directory")), /*#__PURE__*/React.createElement(Button, {
    dialog: "import-text-snippets",
    Icon: TextFieldsIcon,
    onlySupportType: ["text_entity_recognition", "text_classification", "text_entity_relations"],
    type: iface === null || iface === void 0 ? void 0 : iface.type
  }, t("import-text-snippets")), /*#__PURE__*/React.createElement(Button, {
    isDesktop: isDesktop,
    dialog: "import-toy-dataset",
    Icon: PetsIcon
  }, t("import-toy-dataset")), /*#__PURE__*/React.createElement(Button, {
    isDesktop: isDesktop,
    dialog: "youtube-urls",
    Icon: FaYoutube,
    desktopOnly: true
  }, t("import-from"), " Youtube URLs"), /*#__PURE__*/React.createElement(Button, {
    Icon: S3Icon,
    dialog: "import-from-s3",
    disabledReason: fromConfig("auth.s3iam.accessKeyId") ? null : "NEED AWS IAM AUTH"
  }, t("import-from-s3")), /*#__PURE__*/React.createElement(Button, {
    Icon: S3Icon,
    dialog: "upload-to-s3",
    disabledReason: fromConfig("auth.s3iam.accessKeyId") ? null : "NEED AWS IAM AUTH"
  }, t("upload-to-s3")), /*#__PURE__*/React.createElement(Button, {
    isDesktop: isDesktop,
    dialog: "import-from-cognito-s3",
    Icon: S3Icon,
    authConfiguredOnly: true,
    signedInOnly: true,
    user: user
  }, t("import-from-cognito-s3")), /*#__PURE__*/React.createElement(Button, {
    isDesktop: isDesktop,
    dialog: "google-drive-file-picker",
    Icon: FaGoogleDrive,
    onAddSamples: onAddSamples
  }, t("import-from"), " Google Drive"), /*#__PURE__*/React.createElement(Button, {
    dialog: "import-csv-json",
    Icon: DescriptionIcon
  }, t("import-from"), " CSV / JSON"), /*#__PURE__*/React.createElement(Button, {
    isDesktop: isDesktop,
    dialog: "import-from-coco",
    Icon: PhotoLibraryIcon
  }, t("import-from-coco")), /*#__PURE__*/React.createElement(ImportFromCOCODialog, {
    open: selectedDialog === "import-from-coco",
    onClose: closeDialog // dataset={dataset}
    // onChangeDataset={onChangeDataset}

  }), /*#__PURE__*/React.createElement(ImportTextSnippetsDialog, {
    open: selectedDialog === "import-text-snippets",
    onClose: closeDialog,
    onAddSamples: onAddSamples
  }), /*#__PURE__*/React.createElement(PasteUrlsDialog, {
    open: selectedDialog === "paste-image-urls",
    onClose: closeDialog,
    onAddSamples: onAddSamples
  }), /*#__PURE__*/React.createElement(ImportFromS3Dialog, {
    open: selectedDialog === "import-from-s3" // onChangeFile={onChangeFile}
    ,
    onClose: closeDialog,
    user: user,
    onAddSamples: onAddSamples
  }), /*#__PURE__*/React.createElement(UploadToS3Dialog, {
    open: selectedDialog === "upload-to-s3" // onChangeFile={onChangeFile}
    ,
    onClose: closeDialog,
    user: user,
    onAddSamples: onAddSamples
  }), /*#__PURE__*/React.createElement(ImportFromCognitoS3Dialog, {
    open: selectedDialog === "import-from-cognito-s3",
    onClose: closeDialog,
    onAddSamples: onAddSamples
  }), /*#__PURE__*/React.createElement(ImportFromGoogleDriveDialog, {
    open: selectedDialog === "google-drive-file-picker",
    onClose: closeDialog,
    onAddSamples: onAddSamples
  }), /*#__PURE__*/React.createElement(ImportToyDataset, {
    open: selectedDialog === "import-toy-dataset",
    onClose: closeDialog,
    onAddSamples: onAddSamplesAsToyDataset
  }), /*#__PURE__*/React.createElement(ImportFromYoutubeUrls, {
    open: selectedDialog === "youtube-urls",
    onClose: closeDialog,
    onAddSamples: onAddSamples
  }), /*#__PURE__*/React.createElement(ImportUDTFileDialog, {
    open: selectedDialog === "import-csv-json",
    onClose: closeDialog,
    onAddSamples: onAddSamples
  })));
});