text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.DocumentPanel = DocumentPanel;
  
  var _ui = require("@sanity/ui");
  
  var _classnames = _interopRequireDefault(require("classnames"));
  
  var _react = _interopRequireWildcard(require("react"));
  
  var _components = require("@sanity/base/components");
  
  var _features = require("../../../features");
  
  var _documentHistory = require("../documentHistory");
  
  var _usePreviewUrl = require("../usePreviewUrl");
  
  var _title = require("./header/title");
  
  var _header = require("./header/header");
  
  var _menuItems = require("./menuItems");
  
  var _views = require("./views");
  
  var _documentPanel = _interopRequireDefault(require("./documentPanel.css"));
  
  var _constants = require("./constants");
  
  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 _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) { var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]); if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_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; }
  
  function DocumentPanel(props) {
    var toggleInspect = props.toggleInspect,
        isHistoryOpen = props.isHistoryOpen,
        views = props.views,
        activeViewId = props.activeViewId;
    var parentPortal = (0, _ui.usePortal)();
    var features = (0, _features.useDeskToolFeatures)();
    var portalRef = (0, _react.useRef)(null);
  
    var _useState = (0, _react.useState)(null),
        _useState2 = _slicedToArray(_useState, 2),
        documentViewerContainerElement = _useState2[0],
        setDocumentViewerContainerElement = _useState2[1];
  
    var _useDocumentHistory = (0, _documentHistory.useDocumentHistory)(),
        displayed = _useDocumentHistory.displayed,
        historyController = _useDocumentHistory.historyController,
        openHistory = _useDocumentHistory.open;
  
    var activeView = (0, _react.useMemo)(() => views.find(view => view.id === activeViewId) || views[0] || {
      type: 'form'
    }, [activeViewId, views]);
    var revTime = historyController.revTime;
    var hasValue = Boolean(props.value);
    var previewUrl = (0, _usePreviewUrl.usePreviewUrl)(props.value);
    var menuItems = (0, _react.useMemo)(() => {
      return (0, _menuItems.getMenuItems)({
        features,
        hasValue,
        isHistoryOpen: props.isHistoryOpen,
        previewUrl
      }) || [];
    }, [features, hasValue, props.isHistoryOpen, previewUrl]);
    var handleContextMenuAction = (0, _react.useCallback)(item => {
      if (item.action === 'production-preview') {
        window.open(item.url);
        return true;
      }
  
      if (item.action === 'inspect') {
        toggleInspect(true);
        return true;
      }
  
      if (item.action === 'reviewChanges') {
        openHistory();
        return true;
      }
  
      return false;
    }, [openHistory, toggleInspect]); // Use a local portal container when split panes is supported
  
    var portalElement = features.splitPanes ? portalRef.current || parentPortal.element : parentPortal.element; // Calculate the height of the header
  
    var screenIsNarrow = !features.splitPanes;
    var margins = screenIsNarrow ? _constants.MARGINS_NARROW_SCREEN_WITH_TABS : _constants.DEFAULT_MARGINS;
    return /*#__PURE__*/_react.default.createElement(_ui.Card, {
      className: (0, _classnames.default)(_documentPanel.default.root, props.isCollapsed && _documentPanel.default.isCollapsed)
    }, /*#__PURE__*/_react.default.createElement(_components.LegacyLayerProvider, {
      zOffset: "paneHeader"
    }, /*#__PURE__*/_react.default.createElement(_ui.Layer, {
      className: _documentPanel.default.headerContainer
    }, /*#__PURE__*/_react.default.createElement(_header.DocumentPanelHeader, {
      activeViewId: props.activeViewId,
      idPrefix: props.idPrefix,
      isClosable: props.isClosable,
      isCollapsed: props.isCollapsed,
      isTimelineOpen: props.isTimelineOpen,
      markers: props.markers,
      menuItemGroups: props.menuItemGroups,
      menuItems: menuItems,
      onCloseView: props.onCloseView,
      onCollapse: props.onCollapse,
      onContextMenuAction: handleContextMenuAction,
      onExpand: props.onExpand,
      onSetActiveView: props.onSetActiveView,
      onSplitPane: props.onSplitPane,
      onTimelineOpen: props.onTimelineOpen,
      rootElement: props.rootElement,
      schemaType: props.schemaType,
      onSetFormInputFocus: props.onFormInputFocus,
      timelineMode: props.timelineMode,
      title: /*#__PURE__*/_react.default.createElement(_title.DocumentHeaderTitle, {
        documentType: props.documentType,
        paneTitle: props.paneTitle,
        value: props.value
      }),
      versionSelectRef: props.versionSelectRef,
      views: props.views,
      rev: revTime,
      isHistoryOpen: isHistoryOpen
    }))), /*#__PURE__*/_react.default.createElement(_ui.PortalProvider, {
      element: portalElement
    }, /*#__PURE__*/_react.default.createElement(_ui.BoundaryElementProvider, {
      element: documentViewerContainerElement
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _documentPanel.default.documentViewerContainer,
      ref: setDocumentViewerContainerElement
    }, /*#__PURE__*/_react.default.createElement(_components.ScrollContainer, {
      className: _documentPanel.default.documentScroller
    }, activeView.type === 'form' && /*#__PURE__*/_react.default.createElement(_views.FormView, {
      id: props.documentId,
      initialValue: props.initialValue,
      focusPath: props.formInputFocusPath,
      onFocus: props.onFormInputFocus,
      markers: props.markers,
      onChange: props.onChange,
      readOnly: revTime !== null,
      schemaType: props.schemaType,
      value: displayed,
      margins: margins,
      compareValue: props.compareValue
    }), activeView.type === 'component' && /*#__PURE__*/(0, _react.createElement)(activeView.component, {
      document: {
        draft: props.draft,
        displayed: displayed || props.value || props.initialValue,
        historical: displayed,
        published: props.published
      },
      documentId: props.documentId,
      options: activeView.options,
      schemaType: props.schemaType
    })), /*#__PURE__*/_react.default.createElement("div", {
      className: _documentPanel.default.portalContainer
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _documentPanel.default.portal,
      ref: portalRef
    }))))));
  }