"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MediaViewer = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _reactIntlNext = require("react-intl-next");
var _mediaUi = require("@atlaskit/media-ui");
var _analyticsNext = require("@atlaskit/analytics-next");
var _analytics = require("./analytics");
var _modal = require("./analytics/events/screen/modal");
var _closed = require("./analytics/events/ui/closed");
var _list = require("./list");
var _content = require("./content");
var _styleWrappers = require("./styleWrappers");
var _perfMarks = require("perf-marks");
var _classnames = require("./classnames");
var _reactScrolllock = _interopRequireDefault(require("react-scrolllock"));
var _reactFocusLock = _interopRequireDefault(require("react-focus-lock"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var MediaViewerComponent = function MediaViewerComponent(_ref) {
  var featureFlags = _ref.featureFlags,
    items = _ref.items,
    extensions = _ref.extensions,
    contextId = _ref.contextId,
    innerRef = _ref.innerRef,
    _onClose = _ref.onClose,
    selectedItem = _ref.selectedItem,
    intl = _ref.intl,
    viewerOptions = _ref.viewerOptions;
  var _useState = (0, _react.useState)(false),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    isSidebarVisible = _useState2[0],
    setIsSidebarVisible = _useState2[1];
  var _useState3 = (0, _react.useState)(),
    _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
    selectedIdentifier = _useState4[0],
    setSelectedIdentifier = _useState4[1];
  var _useAnalyticsEvents = (0, _analyticsNext.useAnalyticsEvents)(),
    createAnalyticsEvent = _useAnalyticsEvents.createAnalyticsEvent;
  var createAnalyticsEventRef = (0, _react.useRef)(createAnalyticsEvent);
  createAnalyticsEventRef.current = createAnalyticsEvent;
  (0, _react.useLayoutEffect)(function () {
    (0, _analytics.fireAnalytics)((0, _modal.createModalEvent)(), createAnalyticsEventRef.current);
    (0, _perfMarks.start)('MediaViewer.SessionDuration');
  }, []);
  var defaultSelectedItem = selectedItem || items[0];
  var renderSidebar = function renderSidebar() {
    var sidebarSelectedIdentifier = selectedIdentifier || defaultSelectedItem;
    if (sidebarSelectedIdentifier && isSidebarVisible && extensions && extensions.sidebar) {
      return /*#__PURE__*/_react.default.createElement(_styleWrappers.SidebarWrapper, {
        "data-testid": "media-viewer-sidebar-content"
      }, extensions.sidebar.renderer(sidebarSelectedIdentifier, {
        close: function close() {
          return setIsSidebarVisible(!isSidebarVisible);
        }
      }));
    }
  };
  var content = /*#__PURE__*/_react.default.createElement("div", {
    ref: innerRef
  }, /*#__PURE__*/_react.default.createElement(_styleWrappers.Blanket, {
    "data-testid": "media-viewer-popup"
    // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
    ,
    className: _classnames.mediaViewerPopupClass
  }, /*#__PURE__*/_react.default.createElement(_mediaUi.Shortcut, {
    code: 'Escape',
    handler: function handler() {
      (0, _analytics.fireAnalytics)((0, _closed.createClosedEvent)('escKey'), createAnalyticsEventRef.current);
      _onClose && _onClose();
    }
  }), /*#__PURE__*/_react.default.createElement(_content.Content, {
    isSidebarVisible: isSidebarVisible,
    onClose: function onClose(_e, analyticsEvent) {
      var _analyticsEvent$paylo;
      if ((analyticsEvent === null || analyticsEvent === void 0 || (_analyticsEvent$paylo = analyticsEvent.payload) === null || _analyticsEvent$paylo === void 0 ? void 0 : _analyticsEvent$paylo.actionSubject) === 'button') {
        (0, _analytics.fireAnalytics)((0, _closed.createClosedEvent)('button'), createAnalyticsEventRef.current);
      }
      _onClose && _onClose();
    }
  }, /*#__PURE__*/_react.default.createElement(_list.List, {
    defaultSelectedItem: defaultSelectedItem || items[0],
    items: items,
    onClose: _onClose,
    extensions: extensions,
    onNavigationChange: setSelectedIdentifier,
    onSidebarButtonClick: function onSidebarButtonClick() {
      return setIsSidebarVisible(!isSidebarVisible);
    },
    isSidebarVisible: isSidebarVisible,
    contextId: contextId,
    featureFlags: featureFlags,
    viewerOptions: viewerOptions
  })), renderSidebar()));
  return intl ? content : /*#__PURE__*/_react.default.createElement(_reactIntlNext.IntlProvider, {
    locale: "en"
  }, content);
};
var MediaViewerWithRef = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  return /*#__PURE__*/_react.default.createElement(MediaViewerComponent, (0, _extends2.default)({}, props, {
    innerRef: ref
  }));
});
var MediaViewerWithScrollLock = function MediaViewerWithScrollLock(props) {
  return /*#__PURE__*/_react.default.createElement(_reactFocusLock.default, {
    autoFocus: true
  }, /*#__PURE__*/_react.default.createElement(_reactScrolllock.default, null), /*#__PURE__*/_react.default.createElement(MediaViewerWithRef, props));
};
var MediaViewer = exports.MediaViewer = (0, _reactIntlNext.injectIntl)(MediaViewerWithScrollLock, {
  enforceContext: false
});