"use strict";

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

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var _getSpacingAmount = function _getSpacingAmount(theme, regex, key) {
  var keys = Array.isArray(key) ? key : [key];
  var heightKeys = keys.filter(function (k) {
    return regex.test(k);
  }); // First one wins in case of conflict.

  if (!heightKeys[0]) {
    return;
  }

  var regexResult = regex.exec(heightKeys[0]);
  if (regexResult === null) return '0';

  var _regexResult = _slicedToArray(regexResult, 2),
      level = _regexResult[1];

  if (level === '0' || level === 'auto') {
    return level;
  }

  return level != null ? theme.spacing["s".concat(level)] : '0';
};

var _getSpacingTop = function _getSpacingTop(theme, key, mOrP) {
  return _getSpacingAmount(theme, mOrP === 'm' ? /^m[vt]?([0-9])$/ : /^p[vt]?([0-9])$/, key);
};

var _getSpacingRight = function _getSpacingRight(theme, key, mOrP) {
  return _getSpacingAmount(theme, mOrP === 'm' ? /^m[hr]?([0-9]|auto)$/ : /^p[hr]?([0-9])$/, key);
};

var _getSpacingBottom = function _getSpacingBottom(theme, key, mOrP) {
  return _getSpacingAmount(theme, mOrP === 'm' ? /^m[vb]?([0-9]|auto)$/ : /^p[bv]?([0-9])$/, key);
};

var _getSpacingLeft = function _getSpacingLeft(theme, key, mOrP) {
  return _getSpacingAmount(theme, mOrP === 'm' ? /^m[hl]?([0-9])$/ : /^p[hl]?([0-9])$/, key);
};

var getSpacingRules = function getSpacingRules(_ref) {
  var theme = _ref.theme,
      spacing = _ref.spacing;

  if (!spacing) {
    return {};
  }

  var result = {};

  var marginTop = _getSpacingTop(theme, spacing, 'm');

  if (marginTop) result.marginTop = marginTop;

  var marginRight = _getSpacingRight(theme, spacing, 'm');

  if (marginRight) result.marginRight = marginRight;

  var marginBottom = _getSpacingBottom(theme, spacing, 'm');

  if (marginBottom) result.marginBottom = marginBottom;

  var marginLeft = _getSpacingLeft(theme, spacing, 'm');

  if (marginLeft) result.marginLeft = marginLeft;

  var paddingTop = _getSpacingTop(theme, spacing, 'p');

  if (paddingTop) result.paddingTop = paddingTop;

  var paddingRight = _getSpacingRight(theme, spacing, 'p');

  if (paddingRight) result.paddingRight = paddingRight;

  var paddingBottom = _getSpacingBottom(theme, spacing, 'p');

  if (paddingBottom) result.paddingBottom = paddingBottom;

  var paddingLeft = _getSpacingLeft(theme, spacing, 'p');

  if (paddingLeft) result.paddingLeft = paddingLeft;
  return result;
};

exports.getSpacingRules = getSpacingRules;