import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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) { _defineProperty(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; }
import React, { useEffect, useMemo } from 'react';
import { MediaViewer as MediaViewerNextGen } from '../media-viewer';
import { isSameIdentifier } from '../utils';
import { isFileIdentifier } from '@atlaskit/media-client';
import { withMediaClient, withMediaClientAndSettings } from '@atlaskit/media-client-react';
import { fg } from '@atlaskit/platform-feature-flags';
var ensureCollectionName = function ensureCollectionName(identifier, collectionName) {
  return 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 (isSameIdentifier(item, selectedItemWithCollectionName)) {
      selectedIndex = index;
    }
    return ensureCollectionName(item, collectionName);
  });
  var itemsWithSelectedItem = selectedIndex === -1 ? [selectedItem].concat(_toConsumableArray(itemsWithCollectionName)) : itemsWithCollectionName;
  return {
    items: itemsWithSelectedItem,
    selectedItem: selectedItemWithCollectionName
  };
};

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

export var 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 = 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
   */
  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.createElement(MediaViewerNextGen, {
    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)
export var MediaViewerWithMediaClient = function MediaViewerWithMediaClient(props) {
  var ViewerComponent = React.useMemo(function () {
    if (fg('platform_media_video_captions')) {
      return withMediaClientAndSettings(MediaViewerBase);
    }
    return withMediaClient(MediaViewerBase);
  }, []);
  return /*#__PURE__*/React.createElement(ViewerComponent, props);
};