"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _styles = require("@material-ui/core/styles");

var _propTypes = _interopRequireDefault(require("prop-types"));

var _classnames = _interopRequireDefault(require("classnames"));

var _appendCssRules = _interopRequireDefault(require("./append-css-rules"));

var _excluded = ["children", "className", "classes", "fontSizeFactor"],
    _excluded2 = ["extraCSSRules"];

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

var UiLayout = /*#__PURE__*/function (_AppendCSSRules) {
  (0, _inherits2["default"])(UiLayout, _AppendCSSRules);

  var _super = _createSuper(UiLayout);

  function UiLayout(props) {
    var _this;

    (0, _classCallCheck2["default"])(this, UiLayout);
    _this = _super.call(this, props);
    _this.classesSheet = document.createElement('style');
    return _this;
  }

  (0, _createClass2["default"])(UiLayout, [{
    key: "computeStyle",
    value: function computeStyle(fontSizeFactor) {
      var getFontSize = function getFontSize(element) {
        return parseFloat(getComputedStyle(element).fontSize);
      };

      var rootFontSize = getFontSize(document.documentElement);
      var bodyFontSize = getFontSize(document.body);
      var effectiveFontSize = Math.max(rootFontSize, bodyFontSize);
      return fontSizeFactor !== 1 ? {
        fontSize: "".concat(effectiveFontSize * fontSizeFactor, "px")
      } : null;
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          children = _this$props.children,
          className = _this$props.className,
          classes = _this$props.classes,
          fontSizeFactor = _this$props.fontSizeFactor,
          rest = (0, _objectWithoutProperties2["default"])(_this$props, _excluded);
      var finalClass = (0, _classnames["default"])(className, classes.extraCSSRules, classes.uiLayoutContainer);
      var extraCSSRules = rest.extraCSSRules,
          restProps = (0, _objectWithoutProperties2["default"])(rest, _excluded2);
      var style = this.computeStyle(fontSizeFactor);
      return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({
        className: finalClass
      }, restProps, style && {
        style: style
      }), children);
    }
  }]);
  return UiLayout;
}(_appendCssRules["default"]);

(0, _defineProperty2["default"])(UiLayout, "propTypes", {
  classes: _propTypes["default"].object,
  className: _propTypes["default"].string,
  children: _propTypes["default"].array,
  extraCSSRules: _propTypes["default"].shape({
    names: _propTypes["default"].arrayOf(_propTypes["default"].string),
    rules: _propTypes["default"].string
  }),
  fontSizeFactor: _propTypes["default"].number
});
(0, _defineProperty2["default"])(UiLayout, "defaultProps", {
  extraCSSRules: {},
  fontSizeFactor: 1
});
var styles = {
  extraCSSRules: {},
  // need this because some browsers set their own style on table
  uiLayoutContainer: {
    '& table, th, td': {
      fontSize: 'inherit'
      /* Ensure table elements inherit font size */

    }
  }
};
var Styled = (0, _styles.withStyles)(styles)(UiLayout);
var _default = Styled;
exports["default"] = _default;
//# sourceMappingURL=ui-layout.js.map