"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.withMediaAnalyticsContext = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireWildcard(require("react"));
var _analyticsNext = require("@atlaskit/analytics-next");
var _MediaAnalyticsContext = require("@atlaskit/analytics-namespaced-context/MediaAnalyticsContext");
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; }
/**
 * HOC for attaching MediaAnalyticsContext to a top-level React Component.
 *
 * Similar than "packages/analytics/analytics-next/src/hocs/withAnalyticsContext.tsx",
 * except that we restrict attributes put in this context using ContextPublicAttributes and ContextPrivateAttributes.
 *
 * As a constraint, the wrapped React Component's props must extend ContextStaticProps.
 *
 * @param {ContextPublicAttributes} attributes public attributes to put in context
 *
 * @see packages/analytics/analytics-next/src/hocs/withAnalyticsContext.tsx
 */
var withMediaAnalyticsContext = exports.withMediaAnalyticsContext = function withMediaAnalyticsContext(contextPublicAttributes) {
  return function (WrappedComponent) {
    // forwardRef() allows passing React refs to the wrapped component WithMediaAnalyticsContext
    var WithMediaAnalyticsContext = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
      var featureFlags = props.featureFlags;
      var contextData = (0, _react.useMemo)(function () {
        var contextPrivateAttributes = {
          featureFlags: featureFlags
        };
        return _objectSpread(_objectSpread({}, contextPublicAttributes), {}, (0, _defineProperty2.default)({}, _MediaAnalyticsContext.MEDIA_CONTEXT, _objectSpread({}, contextPrivateAttributes)));
      }, [featureFlags]);
      return /*#__PURE__*/_react.default.createElement(_analyticsNext.AnalyticsContext, {
        data: contextData
      }, /*#__PURE__*/_react.default.createElement(WrappedComponent, (0, _extends2.default)({}, props, {
        ref: ref
      })));
    });
    WithMediaAnalyticsContext.displayName = "WithMediaAnalyticsContext(".concat(
    // @ts-ignore displayName doesn't exist on type
    WrappedComponent.displayName || WrappedComponent.name, ")");
    return WithMediaAnalyticsContext;
  };
};