"use strict";

exports.__esModule = true;
exports.ToolBar = ToolBar;
var _react = _interopRequireWildcard(require("react"));
var _lodash = require("lodash");
var _versionHistoryTool = _interopRequireDefault(require("./versionHistoryTool"));
var _MenuBar = _interopRequireDefault(require("../MenuBar"));
require("./style.css");
var _core = require("@blueprintjs/core");
var _downloadTool = _interopRequireDefault(require("./downloadTool"));
var _importTool = _interopRequireDefault(require("./importTool"));
var _cutsiteTool = _interopRequireDefault(require("./cutsiteTool"));
var _featureTool = _interopRequireDefault(require("./featureTool"));
var _partTool = _interopRequireDefault(require("./partTool"));
var _oligoTool = _interopRequireDefault(require("./oligoTool"));
var _orfTool = _interopRequireDefault(require("./orfTool"));
var _editTool = _interopRequireDefault(require("./editTool"));
var _findTool = _interopRequireDefault(require("./findTool"));
var _inlineFindTool = _interopRequireDefault(require("./inlineFindTool"));
var _alignmentTool = _interopRequireDefault(require("./alignmentTool"));
var _saveTool = _interopRequireDefault(require("./saveTool"));
var _visibilityTool = _interopRequireDefault(require("./visibilityTool"));
var _undoTool = _interopRequireDefault(require("./undoTool"));
var _redoTool = _interopRequireDefault(require("./redoTool"));
var _isMobile = _interopRequireDefault(require("is-mobile"));
var _excluded = ["modifyTools", "contentLeft", "showMenuBar", "displayMenuBarAboveTools", "isProtein", "openHotkeyDialog", "onSave", "userDefinedHandlersAndOpts", "editorName", "annotationsToSupport", "handleFullscreenClose", "closeFullscreen", "additionalTopRightToolbarButtons", "toolList"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var allTools = {
  downloadTool: _downloadTool["default"],
  importTool: _importTool["default"],
  cutsiteTool: _cutsiteTool["default"],
  alignmentTool: _alignmentTool["default"],
  featureTool: _featureTool["default"],
  partTool: _partTool["default"],
  oligoTool: _oligoTool["default"],
  orfTool: _orfTool["default"],
  editTool: _editTool["default"],
  findTool: _findTool["default"],
  inlineFindTool: _inlineFindTool["default"],
  versionHistoryTool: _versionHistoryTool["default"],
  saveTool: _saveTool["default"],
  visibilityTool: _visibilityTool["default"],
  undoTool: _undoTool["default"],
  redoTool: _redoTool["default"]
};
function ToolBar(props) {
  var modifyTools = props.modifyTools,
    contentLeft = props.contentLeft,
    showMenuBar = props.showMenuBar,
    displayMenuBarAboveTools = props.displayMenuBarAboveTools,
    isProtein = props.isProtein,
    openHotkeyDialog = props.openHotkeyDialog,
    onSave = props.onSave,
    userDefinedHandlersAndOpts = props.userDefinedHandlersAndOpts,
    editorName = props.editorName,
    annotationsToSupport = props.annotationsToSupport,
    handleFullscreenClose = props.handleFullscreenClose,
    closeFullscreen = props.closeFullscreen,
    additionalTopRightToolbarButtons = props.additionalTopRightToolbarButtons,
    _props$toolList = props.toolList,
    toolList = _props$toolList === void 0 ? defaultToolList : _props$toolList,
    rest = _objectWithoutPropertiesLoose(props, _excluded);
  var userDefinedProps = _extends({}, (0, _lodash.pick)(props, userDefinedHandlersAndOpts));
  var toolListToUse = (0, _react.useMemo)(function () {
    return (0, _lodash.flatMap)(toolList, function (toolNameOrOverrides) {
      var toolName;
      var toolOverride;
      if ((0, _lodash.isString)(toolNameOrOverrides)) {
        toolName = toolNameOrOverrides;
      } else {
        toolOverride = toolNameOrOverrides;
        toolName = toolNameOrOverrides.name;
      }
      if (toolName === "oligoTool" && annotationsToSupport && annotationsToSupport.primers === false) return [];
      var Tool = toolOverride ? allTools[toolOverride.name] : allTools[toolName];
      if (!Tool) {
        console.error("You're trying to load a tool that doesn't appear to exist: " + toolName);
        return [];
      }
      if (isProtein) {
        if (toolName === "cutsiteTool" || toolName === "orfTool" || toolName === "alignmentTool") {
          return [];
        }
      }
      if (toolName === "saveTool" && !onSave) {
        return [];
      } //don't show the option to save if no onSave handler is passed

      return {
        toolName: toolName,
        toolOverride: toolOverride,
        Tool: Tool
      };
    });
  }, [toolList, annotationsToSupport, isProtein, onSave]);
  var items = (0, _lodash.flatMap)(toolListToUse, function (_ref, index) {
    var toolName = _ref.toolName,
      toolOverride = _ref.toolOverride,
      Tool = _ref.Tool;
    if (!Tool) return [];
    return /*#__PURE__*/_react["default"].createElement(Tool, _extends({}, rest, userDefinedProps, {
      toolbarItemProps: _extends({}, userDefinedProps, {
        index: index,
        toolName: toolName,
        editorName: editorName
      }, toolOverride),
      editorName: editorName,
      key: toolName
    }));
  });
  if (modifyTools) {
    items = modifyTools(items);
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "veToolbar-outer",
    style: {
      display: "flex"
    }
  }, contentLeft, /*#__PURE__*/_react["default"].createElement("div", {
    style: _extends({}, displayMenuBarAboveTools && showMenuBar ? {
      display: "flex",
      width: "100%",
      flexDirection: "column",
      alignItems: "flex-start"
    } : {
      display: "flex",
      width: "100%",
      justifyContent: "center",
      flexWrap: "wrap"
    }),
    className: "veToolbar"
  }, showMenuBar && /*#__PURE__*/_react["default"].createElement(_MenuBar["default"], _extends({
    openHotkeyDialog: openHotkeyDialog
  }, userDefinedProps, {
    onSave: onSave //needs to be passed so that editor commands will have it
    ,
    style: {
      marginLeft: 0
    },
    editorName: editorName
  })), displayMenuBarAboveTools && showMenuBar ? /*#__PURE__*/_react["default"].createElement("div", {
    className: "veTools-displayMenuBarAboveTools",
    style: _extends({
      display: "flex",
      paddingLeft: 15,
      paddingRight: 15,
      flexWrap: "wrap"
    }, (0, _isMobile["default"])() && {
      overflow: "auto",
      flexWrap: "nowrap",
      width: "100%"
    })
  }, items) : items), additionalTopRightToolbarButtons, closeFullscreen && /*#__PURE__*/_react["default"].createElement(CloseFullscreenButton, {
    onClick: handleFullscreenClose
  }));
}
var CloseFullscreenButton = function CloseFullscreenButton(props) {
  return /*#__PURE__*/_react["default"].createElement(_core.Tooltip, {
    content: "Close Fullscreen Mode"
  }, /*#__PURE__*/_react["default"].createElement(_core.Button, {
    minimal: true,
    style: {
      marginTop: 2,
      marginRight: 2
    },
    onClick: props.onClick,
    className: "ve-close-fullscreen-button",
    icon: "minimize"
  }));
};
var defaultToolList = ["saveTool", "downloadTool", "importTool", "undoTool", "redoTool", "cutsiteTool", "featureTool", "partTool", "oligoTool", "orfTool", "alignmentTool", "editTool", "findTool", "visibilityTool"];