"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MediaBadges = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _debounce = _interopRequireDefault(require("lodash/debounce"));
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
var _primitives = require("@atlaskit/primitives");
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); }
// eslint-disable-next-line @atlaskit/design-system/no-emotion-primitives -- to be migrated to @atlaskit/primitives/compiled – go/akcss

var MEDIA_BADGE_VISIBILITY_BREAKPOINT = 200;
var containerStyles = (0, _primitives.xcss)({
  display: 'flex',
  position: 'absolute',
  top: 'space.0',
  right: 'space.0',
  // eslint-disable-next-line @atlaskit/design-system/use-tokens-typography
  lineHeight: "var(--ds-space-200, 16px)",
  gap: 'space.025',
  zIndex: 'card',
  height: 'fit-content',
  width: 'fit-content',
  margin: 'space.075'
});

// The above styles are used for both editor and renderer, and in renderer the
// document body is the main scroll area. This means it overscrolls the primary
// toolbar, where the z-index is "2". We have to hack in our own z-index less
// than that to ensure our badge appears under the toolbar when scrolled.
var hackedZIndexStyles = (0, _primitives.xcss)({
  // @ts-ignore
  zIndex: '1'
});
var resizeOffsetStyles = (0, _primitives.xcss)({
  right: 'space.150'
});
var getBadgeVisible = function getBadgeVisible(width, height) {
  return width && width < MEDIA_BADGE_VISIBILITY_BREAKPOINT || height && height < MEDIA_BADGE_VISIBILITY_BREAKPOINT ? false : true;
};
var MediaBadges = exports.MediaBadges = function MediaBadges(_ref) {
  var children = _ref.children,
    mediaElement = _ref.mediaElement,
    mediaWidth = _ref.mediaWidth,
    mediaHeight = _ref.mediaHeight,
    extendedResizeOffset = _ref.extendedResizeOffset,
    _ref$useMinimumZIndex = _ref.useMinimumZIndex,
    useMinimumZIndex = _ref$useMinimumZIndex === void 0 ? false : _ref$useMinimumZIndex;
  var _useState = (0, _react.useState)(getBadgeVisible(mediaWidth, mediaHeight)),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    visible = _useState2[0],
    setVisible = _useState2[1];
  (0, _react.useEffect)(function () {
    var observer = new ResizeObserver((0, _debounce.default)(function (entries) {
      var _entries = (0, _slicedToArray2.default)(entries, 1),
        entry = _entries[0];
      var _entry$contentRect = entry.contentRect,
        width = _entry$contentRect.width,
        height = _entry$contentRect.height;
      setVisible(getBadgeVisible(width, height));
    }));
    if (mediaElement) {
      // Ignored via go/ees005
      // eslint-disable-next-line @atlaskit/editor/no-as-casting
      observer.observe(mediaElement);
    }
    return function () {
      observer.disconnect();
    };
  }, [mediaElement]);
  if (typeof children === 'function') {
    children = children({
      visible: visible
    });
  }

  // delete this block on cleanup of media-perf-uplift-mutation-fix
  if (!(0, _platformFeatureFlags.fg)('media-perf-uplift-mutation-fix')) {
    // becuase it is wrapped in a fragment, React.Children.count(children) will always be 1.
    // That makes this check a source of late mutations that we don't need.
    if (!mediaElement || _react.default.Children.count(children) === 0) {
      return null;
    }
  }
  return /*#__PURE__*/_react.default.createElement(_primitives.Box, {
    as: "div",
    testId: "media-badges",
    "data-media-badges": "true",
    contentEditable: false,
    xcss: [containerStyles, useMinimumZIndex && hackedZIndexStyles, extendedResizeOffset && resizeOffsetStyles]
  }, children);
};