text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.DocumentPanelHeader = DocumentPanelHeader;
  
  var _upperFirst2 = _interopRequireDefault(require("lodash/upperFirst"));
  
  var _negate2 = _interopRequireDefault(require("lodash/negate"));
  
  var _hooks = require("@sanity/base/hooks");
  
  var _classnames = _interopRequireDefault(require("classnames"));
  
  var _closeIcon = _interopRequireDefault(require("part:@sanity/base/close-icon"));
  
  var _splitHorizontalIcon = _interopRequireDefault(require("part:@sanity/base/split-horizontal-icon"));
  
  var _default = _interopRequireDefault(require("part:@sanity/components/buttons/default"));
  
  var _languageSelectComponent = _interopRequireDefault(require("part:@sanity/desk-tool/language-select-component?"));
  
  var _react = _interopRequireWildcard(require("react"));
  
  var _DropdownButton = require("../../../../components/DropdownButton");
  
  var _features = require("../../../../features");
  
  var _timeline = require("../../timeline");
  
  var _contextMenu = require("./contextMenu");
  
  var _tabs = require("./tabs");
  
  var _validationMenu = require("./validationMenu");
  
  var _header = _interopRequireDefault(require("./header.css"));
  
  function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
  
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  
  function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  
  var isActionButton = item => item.showAsAction;
  
  var isMenuButton = (0, _negate2.default)(isActionButton); // eslint-disable-next-line complexity
  
  function DocumentPanelHeader(props) {
    var activeViewId = props.activeViewId,
        idPrefix = props.idPrefix,
        isClosable = props.isClosable,
        isCollapsed = props.isCollapsed,
        isTimelineOpen = props.isTimelineOpen,
        markers = props.markers,
        menuItems = props.menuItems,
        menuItemGroups = props.menuItemGroups,
        onCloseView = props.onCloseView,
        onContextMenuAction = props.onContextMenuAction,
        onCollapse = props.onCollapse,
        onExpand = props.onExpand,
        onSetActiveView = props.onSetActiveView,
        onSplitPane = props.onSplitPane,
        onTimelineOpen = props.onTimelineOpen,
        rev = props.rev,
        rootElement = props.rootElement,
        schemaType = props.schemaType,
        onSetFormInputFocus = props.onSetFormInputFocus,
        timelineMode = props.timelineMode,
        title = props.title,
        versionSelectRef = props.versionSelectRef,
        views = props.views;
    var features = (0, _features.useDeskToolFeatures)();
    var contextMenuItems = menuItems.filter(isMenuButton);
  
    var _useState = (0, _react.useState)(false),
        _useState2 = _slicedToArray(_useState, 2),
        isContextMenuOpen = _useState2[0],
        setContextMenuOpen = _useState2[1];
  
    var _React$useState = _react.default.useState(false),
        _React$useState2 = _slicedToArray(_React$useState, 2),
        isValidationOpen = _React$useState2[0],
        setValidationOpen = _React$useState2[1];
  
    var handleTitleClick = (0, _react.useCallback)(() => {
      if (isCollapsed && onExpand) onExpand();
      if (!isCollapsed && onCollapse) onCollapse();
    }, [isCollapsed, onExpand, onCollapse]); // This is needed to stop the ClickOutside-handler (in the Popover) to treat the click
    // as an outside-click.
  
    var ignoreClickOutside = (0, _react.useCallback)(evt => {
      evt.stopPropagation();
    }, []);
    var showTabs = views.length > 1;
    var showVersionMenu = features.reviewChanges || views.length === 1;
    var menuOpen = isTimelineOpen && timelineMode === 'rev';
    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _classnames.default)(_header.default.root, isCollapsed && _header.default.isCollapsed)
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _header.default.mainNav
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _header.default.title,
      onClick: handleTitleClick
    }, /*#__PURE__*/_react.default.createElement("strong", null, title)), /*#__PURE__*/_react.default.createElement("div", {
      className: _header.default.paneActions
    }, _languageSelectComponent.default && /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_languageSelectComponent.default, null)), /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_validationMenu.ValidationMenu, {
      boundaryElement: rootElement,
      isOpen: isValidationOpen,
      markers: markers,
      schemaType: schemaType,
      setFocusPath: onSetFormInputFocus,
      setOpen: setValidationOpen
    })), /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_contextMenu.DocumentPanelContextMenu, {
      boundaryElement: rootElement,
      isCollapsed: isCollapsed,
      itemGroups: menuItemGroups,
      items: contextMenuItems,
      onAction: onContextMenuAction,
      open: isContextMenuOpen,
      setOpen: setContextMenuOpen
    })), features.splitViews && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, onSplitPane && views.length > 1 && /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_default.default, {
      icon: _splitHorizontalIcon.default,
      kind: "simple",
      onClick: onSplitPane,
      padding: "small",
      title: "Split pane right",
      type: "button"
    })), onSplitPane && isClosable && /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_default.default, {
      icon: _closeIcon.default,
      kind: "simple",
      onClick: onCloseView,
      padding: "small",
      title: "Close pane",
      type: "button"
    }))))), (showTabs || showVersionMenu) && /*#__PURE__*/_react.default.createElement("div", {
      className: _header.default.viewNav
    }, showTabs && /*#__PURE__*/_react.default.createElement("div", {
      className: _header.default.tabsContainer
    }, /*#__PURE__*/_react.default.createElement(_tabs.DocumentHeaderTabs, {
      activeViewId: activeViewId,
      idPrefix: idPrefix,
      onSetActiveView: onSetActiveView,
      views: views
    })), showVersionMenu && /*#__PURE__*/_react.default.createElement("div", {
      className: _header.default.versionSelectContainer,
      ref: versionSelectRef
    }, /*#__PURE__*/_react.default.createElement(_DropdownButton.DropdownButton, {
      onMouseUp: ignoreClickOutside,
      onClick: onTimelineOpen,
      selected: isTimelineOpen && timelineMode === 'rev'
    }, menuOpen ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, "Select version") : rev ? /*#__PURE__*/_react.default.createElement(TimelineButtonLabel, {
      rev: rev
    }) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, "Current version")))));
  }
  
  function TimelineButtonLabel(_ref) {
    var rev = _ref.rev;
    var timeAgo = (0, _hooks.useTimeAgo)(rev.endTimestamp, {
      agoSuffix: true
    });
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, (0, _upperFirst2.default)((0, _timeline.formatTimelineEventLabel)(rev.type)), " ", timeAgo);
  }