"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MediaViewerWithMediaClient = exports.MediaViewerBase = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _mediaViewer = require("../media-viewer");
var _utils = require("../utils");
var _mediaClient = require("@atlaskit/media-client");
var _mediaClientReact = require("@atlaskit/media-client-react");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
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); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var ensureCollectionName = function ensureCollectionName(identifier, collectionName) {
  return (0, _mediaClient.isFileIdentifier)(identifier) ? _objectSpread(_objectSpread({}, identifier), {}, {
    collectionName: identifier.collectionName || collectionName
  }) : identifier;
};
var normaliseItems = function normaliseItems(items, selectedItem, collectionName) {
  var selectedItemWithCollectionName = ensureCollectionName(selectedItem, collectionName);
  var selectedIndex = -1;
  var itemsWithCollectionName = items.map(function (item, index) {
    if ((0, _utils.isSameIdentifier)(item, selectedItemWithCollectionName)) {
      selectedIndex = index;
    }
    return ensureCollectionName(item, collectionName);
  });
  var itemsWithSelectedItem = selectedIndex === -1 ? [selectedItem].concat((0, _toConsumableArray2.default)(itemsWithCollectionName)) : itemsWithCollectionName;
  return {
    items: itemsWithSelectedItem,
    selectedItem: selectedItemWithCollectionName
  };
};

// TODO: This component will be removed in https://product-fabric.atlassian.net/browse/CXP-2722

var MediaViewerBase = exports.MediaViewerBase = function MediaViewerBase(_ref) {
  var featureFlags = _ref.featureFlags,
    onClose = _ref.onClose,
    selectedItem = _ref.selectedItem,
    collectionName = _ref.collectionName,
    items = _ref.items,
    extensions = _ref.extensions,
    contextId = _ref.contextId,
    viewerOptions = _ref.viewerOptions;
  var _useMemo = (0, _react.useMemo)(function () {
      return normaliseItems(items, selectedItem, collectionName);
    }, [items, selectedItem, collectionName]),
    normalisedItems = _useMemo.items,
    normalisedSelectedItem = _useMemo.selectedItem;

  /**
   * Sends a notification for when MediaViewer opens or closes. We do so by
   * posting a message to a window of any target origin (i.e. '*') so please
   * ensure that we are NOT including any sensitive data in the message.
   * Read more details here: https://product-fabric.atlassian.net/browse/MEX-2566
   */
  (0, _react.useEffect)(function () {
    var openingMsg = {
      source: 'media',
      event: 'mediaViewerOpened'
    };
    parent.postMessage(openingMsg, '*');
    return function () {
      var closingMsg = {
        source: 'media',
        event: 'mediaViewerClosed'
      };
      parent.postMessage(closingMsg, '*');
    };
  }, []);
  return /*#__PURE__*/_react.default.createElement(_mediaViewer.MediaViewer, {
    selectedItem: normalisedSelectedItem,
    onClose: onClose,
    items: normalisedItems,
    featureFlags: featureFlags,
    extensions: extensions,
    contextId: contextId,
    viewerOptions: viewerOptions
  });
};

// Can't export in a single line. Typescript struggles to recognize the component signature in the error boundary test file ./media-viewer-error-boundary.test.tsx
// export const MediaViewerWithMediaClient = withMediaClient(MediaViewerBase)
var MediaViewerWithMediaClient = exports.MediaViewerWithMediaClient = function MediaViewerWithMediaClient(props) {
  var ViewerComponent = _react.default.useMemo(function () {
    if ((0, _platformFeatureFlags.fg)('platform_media_video_captions')) {
      return (0, _mediaClientReact.withMediaClientAndSettings)(MediaViewerBase);
    }
    return (0, _mediaClientReact.withMediaClient)(MediaViewerBase);
  }, []);
  return /*#__PURE__*/_react.default.createElement(ViewerComponent, props);
};