"use strict";

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

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

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) 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]|auto)$/ : /^p[hl]?([0-9])$/, key);
}; // For embedding in a template string.


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

  if (!spacing) {
    return undefined;
  } // Set spacing for all breakpoints.


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

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

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

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

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

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

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

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

  return [marginTop ? "margin-top: ".concat(marginTop, ";\n") : '', marginRight ? "margin-right: ".concat(marginRight, ";\n") : '', marginBottom ? "margin-bottom: ".concat(marginBottom, ";\n") : '', marginLeft ? "margin-left: ".concat(marginLeft, ";\n") : '', paddingTop ? "padding-top: ".concat(paddingTop, ";\n") : '', paddingRight ? "padding-right: ".concat(paddingRight, ";\n") : '', paddingBottom ? "padding-bottom: ".concat(paddingBottom, ";\n") : '', paddingLeft ? "padding-left: ".concat(paddingLeft, ";\n") : ''].join('');
};

exports.getSpacingRules = getSpacingRules;