"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.WidthProvider = exports.WidthContext = exports.WidthConsumer = void 0;
exports.createWidthContext = createWidthContext;
exports.getBreakpoint = getBreakpoint;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@emotion/react");
var _rafSchd = _interopRequireDefault(require("raf-schd"));
var _widthDetector = require("@atlaskit/width-detector");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/** @jsx jsx */

var styles = (0, _react2.css)({
  position: 'relative',
  width: '100%'
});
var SCROLLBAR_WIDTH = 30;
function getBreakpoint() {
  var width = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  var MAX_S = 1266;
  var MAX_M = 2146;
  if (width >= MAX_S && width < MAX_M) {
    return 'M';
  } else if (width >= MAX_M) {
    return 'L';
  }
  return 'S';
}
function createWidthContext() {
  var width = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  return {
    width: width,
    breakpoint: getBreakpoint(width)
  };
}
var WidthContext = exports.WidthContext = /*#__PURE__*/_react.default.createContext(createWidthContext());
var Provider = WidthContext.Provider,
  Consumer = exports.WidthConsumer = WidthContext.Consumer;
var WidthProvider = exports.WidthProvider = function WidthProvider(_ref) {
  var _document$body$offset, _document$body;
  var className = _ref.className,
    shouldCheckExistingValue = _ref.shouldCheckExistingValue,
    children = _ref.children;
  var existingContextValue = _react.default.useContext(WidthContext);
  var _React$useState = _react.default.useState(typeof document !== 'undefined' ? (_document$body$offset = (_document$body = document.body) === null || _document$body === void 0 ? void 0 : _document$body.offsetWidth) !== null && _document$body$offset !== void 0 ? _document$body$offset : 0 : 0),
    _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
    width = _React$useState2[0],
    setWidth = _React$useState2[1];
  var providerValue = _react.default.useMemo(function () {
    return createWidthContext(width);
  }, [width]);
  var updateWidth = (0, _rafSchd.default)(function (nextWidth) {
    // Ignore changes that are less than SCROLLBAR_WIDTH, otherwise it can cause infinite re-scaling
    if (Math.abs(width - nextWidth) < SCROLLBAR_WIDTH) {
      return;
    }
    setWidth(nextWidth);
  });
  var skipWidthDetection = shouldCheckExistingValue && existingContextValue.width > 0;
  return (0, _react2.jsx)("div", {
    css: styles,
    className: className
  }, !skipWidthDetection && (0, _react2.jsx)(_react.Fragment, null, (0, _react2.jsx)(_widthDetector.WidthObserver, {
    setWidth: updateWidth,
    offscreen: true
  }), (0, _react2.jsx)(Provider, {
    value: providerValue
  }, children)), skipWidthDetection && children);
};