text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.DocumentPane = DocumentPane;
  
  var _ui = require("@sanity/ui");
  
  var PathUtils = _interopRequireWildcard(require("@sanity/util/paths"));
  
  var _classnames = _interopRequireDefault(require("classnames"));
  
  var _default = _interopRequireDefault(require("part:@sanity/components/snackbar/default"));
  
  var _react = _interopRequireWildcard(require("react"));
  
  var _components = require("@sanity/base/components");
  
  var _ChangeConnectorRoot = require("@sanity/base/lib/change-indicators/overlay/ChangeConnectorRoot");
  
  var _presence = require("part:@sanity/base/datastore/presence");
  
  var _PaneRouterContext = require("../../contexts/PaneRouterContext");
  
  var _features = require("../../features");
  
  var _changesPanel = require("./changesPanel");
  
  var _documentHistory = require("./documentHistory");
  
  var _documentPanel = require("./documentPanel");
  
  var _documentOperationResults = require("./documentOperationResults");
  
  var _inspectDialog = require("./inspectDialog");
  
  var _keyboardShortcuts = require("./keyboardShortcuts");
  
  var _statusBar = require("./statusBar");
  
  var _timeline = require("./timeline");
  
  var _usePreviewUrl = require("./usePreviewUrl");
  
  var _documentPane = _interopRequireDefault(require("./documentPane.css"));
  
  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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
  
  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; }
  
  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; }
  
  var EMPTY_ARRAY = []; // eslint-disable-next-line complexity
  
  function DocumentPane(props) {
    var connectionState = props.connectionState,
        documentId = props.documentId,
        documentIdRaw = props.documentIdRaw,
        documentType = props.documentType,
        draft = props.draft,
        initialValue = props.initialValue,
        isSelected = props.isSelected,
        isCollapsed = props.isCollapsed,
        isClosable = props.isClosable,
        markers = props.markers,
        _props$menuItemGroups = props.menuItemGroups,
        menuItemGroups = _props$menuItemGroups === void 0 ? EMPTY_ARRAY : _props$menuItemGroups,
        onChange = props.onChange,
        onCollapse = props.onCollapse,
        onExpand = props.onExpand,
        paneKey = props.paneKey,
        published = props.published,
        paneTitle = props.title,
        schemaType = props.schemaType,
        value = props.value,
        compareValue = props.compareValue,
        _props$views = props.views,
        views = _props$views === void 0 ? [] : _props$views;
    var rootRef = (0, _react.useRef)(null);
    var features = (0, _features.useDeskToolFeatures)();
  
    var _useDocumentHistory = (0, _documentHistory.useDocumentHistory)(),
        historyController = _useDocumentHistory.historyController,
        setTimelineMode = _useDocumentHistory.setTimelineMode,
        timelineMode = _useDocumentHistory.timelineMode,
        open = _useDocumentHistory.open;
  
    var historyState = historyController.selectionState;
  
    var _useState = (0, _react.useState)(false),
        _useState2 = _slicedToArray(_useState, 2),
        showValidationTooltip = _useState2[0],
        setShowValidationTooltip = _useState2[1];
  
    var paneRouter = (0, _PaneRouterContext.usePaneRouter)();
    var activeViewId = paneRouter.params.view || views[0] && views[0].id;
  
    var _React$useState = _react.default.useState(() => paneRouter.params.path ? PathUtils.fromString(paneRouter.params.path) : []),
        _React$useState2 = _slicedToArray(_React$useState, 2),
        formInputFocusPath = _React$useState2[0],
        setFocusPath = _React$useState2[1];
  
    var isInspectOpen = paneRouter.params.inspect === 'on';
    var handleFocus = (0, _react.useCallback)(nextFocusPath => {
      setFocusPath(PathUtils.pathFor(nextFocusPath));
      (0, _presence.setLocation)([{
        type: 'document',
        documentId,
        path: nextFocusPath,
        lastActiveAt: new Date().toISOString()
      }]);
    }, [documentId]);
    var toggleInspect = (0, _react.useCallback)(function () {
      var toggle = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : !isInspectOpen;
  
      var _paneRouter$params = paneRouter.params,
          oldInspect = _paneRouter$params.inspect,
          params = _objectWithoutProperties(_paneRouter$params, ["inspect"]);
  
      if (toggle) {
        paneRouter.setParams(_objectSpread({
          inspect: 'on'
        }, params));
      } else {
        paneRouter.setParams(params);
      }
    }, [isInspectOpen, paneRouter]);
    var previewUrl = (0, _usePreviewUrl.usePreviewUrl)(value);
    var handleKeyUp = (0, _react.useCallback)(event => {
      if (event.key === 'Escape' && showValidationTooltip) {
        setShowValidationTooltip(false);
      }
  
      if ((0, _keyboardShortcuts.isInspectHotkey)(event)) {
        toggleInspect();
      }
  
      if ((0, _keyboardShortcuts.isPreviewHotkey)(event)) {
        if (previewUrl) {
          window.open(previewUrl);
        }
      }
    }, [previewUrl, showValidationTooltip, toggleInspect]);
    var handleInspectClose = (0, _react.useCallback)(() => toggleInspect(false), [toggleInspect]);
    var handleSetActiveView = (0, _react.useCallback)(id => paneRouter.setView(id), [paneRouter]);
    var handleClosePane = (0, _react.useCallback)(() => paneRouter.closeCurrent(), [paneRouter]);
    var handleSplitPane = (0, _react.useCallback)(() => paneRouter.duplicateCurrent(), [paneRouter]);
    var changesSinceSelectRef = (0, _react.useRef)(null);
    var versionSelectRef = (0, _react.useRef)(null);
    var handleTimelineClose = (0, _react.useCallback)(() => {
      setTimelineMode('closed');
    }, [setTimelineMode]);
    var handleTimelineSince = (0, _react.useCallback)(() => {
      setTimelineMode(timelineMode === 'since' ? 'closed' : 'since');
    }, [timelineMode, setTimelineMode]);
    var handleTimelineRev = (0, _react.useCallback)(() => {
      setTimelineMode(timelineMode === 'rev' ? 'closed' : 'rev');
    }, [timelineMode, setTimelineMode]);
    var isChangesOpen = historyController.changesPanelActive();
    var isTimelineOpen = timelineMode !== 'closed';
    var zOffsets = (0, _components.useZIndex)();
    return /*#__PURE__*/_react.default.createElement(_components.LegacyLayerProvider, {
      zOffset: "pane"
    }, /*#__PURE__*/_react.default.createElement(_keyboardShortcuts.DocumentActionShortcuts, {
      id: documentIdRaw,
      type: documentType,
      onKeyUp: handleKeyUp,
      className: (0, _classnames.default)([_documentPane.default.root, isCollapsed && _documentPane.default.isCollapsed, isSelected ? _documentPane.default.isActive : _documentPane.default.isDisabled]),
      rootRef: rootRef
    }, /*#__PURE__*/_react.default.createElement(_ui.DialogProvider, {
      position: ['fixed', 'absolute'],
      zOffset: zOffsets.portal
    }, /*#__PURE__*/_react.default.createElement(_ChangeConnectorRoot.ChangeConnectorRoot, {
      onSetFocus: handleFocus,
      onOpenReviewChanges: open,
      isReviewChangesOpen: isChangesOpen,
      className: _documentPane.default.documentAndChangesContainer
    }, /*#__PURE__*/_react.default.createElement("div", {
      className: _documentPane.default.documentContainer
    }, /*#__PURE__*/_react.default.createElement(_documentPanel.DocumentPanel, {
      activeViewId: activeViewId,
      documentId: documentId,
      documentType: documentType,
      draft: draft,
      idPrefix: paneKey,
      formInputFocusPath: formInputFocusPath,
      onFormInputFocus: handleFocus,
      initialValue: initialValue,
      isClosable: isClosable,
      isCollapsed: isCollapsed,
      isHistoryOpen: isChangesOpen,
      isTimelineOpen: isTimelineOpen,
      markers: markers,
      menuItemGroups: menuItemGroups,
      onChange: onChange,
      onCloseView: handleClosePane,
      onCollapse: onCollapse,
      onExpand: onExpand,
      onSetActiveView: handleSetActiveView,
      onSplitPane: handleSplitPane,
      onTimelineOpen: handleTimelineRev,
      paneTitle: paneTitle,
      published: published,
      rootElement: rootRef.current,
      schemaType: schemaType,
      timelineMode: timelineMode,
      toggleInspect: toggleInspect,
      value: value,
      compareValue: isChangesOpen ? historyController.sinceAttributes() : compareValue,
      versionSelectRef: versionSelectRef,
      views: views
    })), features.reviewChanges && !isCollapsed && isChangesOpen && /*#__PURE__*/_react.default.createElement("div", {
      className: _documentPane.default.changesContainer
    }, /*#__PURE__*/_react.default.createElement(_changesPanel.ChangesPanel, {
      changesSinceSelectRef: changesSinceSelectRef,
      documentId: documentId,
      isTimelineOpen: isTimelineOpen,
      loading: historyState === 'loading',
      onTimelineOpen: handleTimelineSince,
      schemaType: schemaType,
      since: historyController.sinceTime,
      timelineMode: timelineMode
    })))), /*#__PURE__*/_react.default.createElement(_components.LegacyLayerProvider, {
      zOffset: "paneFooter"
    }, /*#__PURE__*/_react.default.createElement(_ui.Layer, {
      className: _documentPane.default.footerContainer
    }, /*#__PURE__*/_react.default.createElement(_statusBar.DocumentStatusBar, {
      id: documentId,
      type: documentType,
      lastUpdated: value && value._updatedAt
    }))), connectionState === 'reconnecting' && /*#__PURE__*/_react.default.createElement(_default.default, {
      kind: "warning",
      isPersisted: true,
      title: "Connection lost. Reconnecting\u2026"
    }), /*#__PURE__*/_react.default.createElement(_documentOperationResults.DocumentOperationResults, {
      id: documentId,
      type: documentType
    }), /*#__PURE__*/_react.default.createElement(_components.LegacyLayerProvider, {
      zOffset: "paneHeader"
    }, /*#__PURE__*/_react.default.createElement(_timeline.TimelinePopover, {
      onClose: handleTimelineClose,
      open: isTimelineOpen,
      placement: "bottom",
      targetElement: timelineMode === 'rev' ? versionSelectRef.current : changesSinceSelectRef.current
    })), /*#__PURE__*/_react.default.createElement(_components.LegacyLayerProvider, {
      zOffset: "fullscreen"
    }, isInspectOpen && /*#__PURE__*/_react.default.createElement(_inspectDialog.InspectDialog, {
      idPrefix: paneKey,
      onClose: handleInspectClose,
      value: value
    }))));
  }