import _typeof from "@babel/runtime/helpers/typeof";
import _inherits from "@babel/runtime/helpers/inherits";
import _setPrototypeOf from "@babel/runtime/helpers/setPrototypeOf";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["only"];

function _wrapRegExp() { _wrapRegExp = function _wrapRegExp(re, groups) { return new BabelRegExp(re, void 0, groups); }; var _super = RegExp.prototype, _groups = new WeakMap(); function BabelRegExp(re, flags, groups) { var _this = new RegExp(re, flags); return _groups.set(_this, groups || _groups.get(re)), _setPrototypeOf(_this, BabelRegExp.prototype); } function buildGroups(result, re) { var g = _groups.get(re); return Object.keys(g).reduce(function (groups, name) { return groups[name] = result[g[name]], groups; }, Object.create(null)); } return _inherits(BabelRegExp, RegExp), BabelRegExp.prototype.exec = function (str) { var result = _super.exec.call(this, str); return result && (result.groups = buildGroups(result, this)), result; }, BabelRegExp.prototype[Symbol.replace] = function (str, substitution) { if ("string" == typeof substitution) { var groups = _groups.get(this); return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) { return "$" + groups[name]; })); } if ("function" == typeof substitution) { var _this = this; return _super[Symbol.replace].call(this, str, function () { var args = arguments; return "object" != _typeof(args[args.length - 1]) && (args = [].slice.call(args)).push(buildGroups(args, _this)), substitution.apply(this, args); }); } return _super[Symbol.replace].call(this, str, substitution); }, _wrapRegExp.apply(this, arguments); }

import PropTypes from "prop-types";
import useTheme from "../styles/useTheme";
import useMediaQuery from "../styles/useMediaQuery";

var useHidden = function useHidden(_ref) {
  var only = _ref.only,
      props = _objectWithoutProperties(_ref, _excluded);

  var theme = useTheme();

  if (only) {
    return useMediaQuery(theme.breakpoints.only(only));
  }

  var hideMedias = Object.entries(props).map(function (_ref2) {
    var _ref3 = _slicedToArray(_ref2, 2),
        key = _ref3[0],
        value = _ref3[1];

    if (!value) return false;

    var _exec = /*#__PURE__*/_wrapRegExp(/([A-Za-z]*)(Up|Down)/, {
      breakpoint: 1,
      dir: 2
    }).exec(key),
        groups = _exec.groups;

    if (!groups) return false;
    var breakpoint = groups.breakpoint,
        dir = groups.dir;
    var media = theme.breakpoints[dir.toLowerCase()](breakpoint);
    return useMediaQuery(media);
  });
  return hideMedias.some(Boolean);
};

var Hidden = function Hidden(props) {
  var children = props.children,
      only = props.only,
      xsUp = props.xsUp,
      smUp = props.smUp,
      mdUp = props.mdUp,
      lgUp = props.lgUp,
      xlUp = props.xlUp,
      xsDown = props.xsDown,
      smDown = props.smDown,
      mdDown = props.mdDown,
      lgDown = props.lgDown,
      xlDown = props.xlDown;
  var isHidden = useHidden({
    only: only,
    xsUp: xsUp,
    smUp: smUp,
    mdUp: mdUp,
    lgUp: lgUp,
    xlUp: xlUp,
    xsDown: xsDown,
    smDown: smDown,
    mdDown: mdDown,
    lgDown: lgDown,
    xlDown: xlDown
  });

  if (isHidden) {
    return null;
  }

  return children;
};

Hidden.propTypes = {
  only: PropTypes.oneOf(["xs", "sm", "md", "lg", "xl"]),
  xsUp: PropTypes.bool,
  smUp: PropTypes.bool,
  mdUp: PropTypes.bool,
  lgUp: PropTypes.bool,
  xlUp: PropTypes.bool,
  xsDown: PropTypes.bool,
  smDown: PropTypes.bool,
  mdDown: PropTypes.bool,
  lgDown: PropTypes.bool,
  xlDown: PropTypes.bool
};
Hidden.defaultProps = {
  xsUp: false,
  smUp: false,
  mdUp: false,
  lgUp: false,
  xlUp: false,
  xsDown: false,
  smDown: false,
  mdDown: false,
  lgDown: false,
  xlDown: false
};
export default Hidden;