"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMediaQuery = useMediaQuery;

var _reactNative = require("react-native");

var _lodash = _interopRequireDefault(require("lodash.isnil"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function useMediaQuery(query) {
  let dims = (0, _reactNative.useWindowDimensions)();
  const height = dims === null || dims === void 0 ? void 0 : dims.height;
  const width = dims === null || dims === void 0 ? void 0 : dims.width;
  return iterateQuery(query, height, width);
}

function iterateQuery(query, height, width) {
  let key;
  let val;
  let queryResults = [];

  if (Array.isArray(query)) {
    query.forEach(subQuery => {
      key = Object.keys(subQuery)[0];
      val = Object.values(subQuery)[0];
      queryResults.push(calculateQuery(key, val, height, width));
    });
  } else {
    key = Object.keys(query)[0];
    val = Object.values(query)[0];
    queryResults.push(calculateQuery(key, val, height, width));
  }

  return queryResults;
}

function calculateQuery(key, val, height, width) {
  let retval;

  if ((0, _lodash.default)(width) || (0, _lodash.default)(height) || (0, _lodash.default)(val)) {
    return;
  }

  switch (key) {
    case 'maxWidth':
      retval = !(0, _lodash.default)(val) ? width <= val : undefined;
      break;

    case 'minWidth':
      retval = !(0, _lodash.default)(val) ? width >= val : undefined;
      break;

    case 'maxHeight':
      retval = !(0, _lodash.default)(val) ? height <= val : undefined;
      break;

    case 'minHeight':
      retval = !(0, _lodash.default)(val) ? height >= val : undefined;
      break;

    case 'orientation':
      if (!(0, _lodash.default)(val)) {
        if (width > height) {
          retval = val === 'landscape';
        } else {
          retval = val === 'portrait';
        }
      }

      break;

    default:
      break;
  }

  return retval;
}
//# sourceMappingURL=useMediaQuery.js.map