"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getEffectiveSpaceValue = exports.getSpacingRules = exports.getSpacingLeft = exports.getSpacingBottom = exports.getSpacingRight = exports.getSpacingTop = void 0;

var _lodash = require("lodash");

var _breakpoints = require("../../breakpoints");

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);
};

exports.getSpacingTop = getSpacingTop;

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

exports.getSpacingRight = getSpacingRight;

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

exports.getSpacingBottom = getSpacingBottom;

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

exports.getSpacingLeft = getSpacingLeft;

var getSpacingOptions = function getSpacingOptions(theme, spacing) {
  if (!spacing) {
    return '';
  } // 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');
  var rules = [];

  if (marginTop) {
    rules.push("margin-top: ".concat(marginTop, ";"));
  }

  if (marginRight) {
    rules.push("margin-right: ".concat(marginRight, ";"));
  }

  if (marginBottom) {
    rules.push("margin-bottom: ".concat(marginBottom, ";"));
  }

  if (marginLeft) {
    rules.push("margin-left: ".concat(marginLeft, ";"));
  }

  if (paddingTop) {
    rules.push("padding-top: ".concat(paddingTop, ";"));
  }

  if (paddingRight) {
    rules.push("padding-right: ".concat(paddingRight, ";"));
  }

  if (paddingBottom) {
    rules.push("padding-bottom: ".concat(paddingBottom, ";"));
  }

  if (paddingLeft) {
    rules.push("padding-left: ".concat(paddingLeft, ";"));
  }

  return rules.join('\n');
}; // For embedding in a template string.


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

  if (!spacing) {
    return '';
  }

  if (typeof spacing === 'string' || Array.isArray(spacing)) {
    return getSpacingOptions(theme, spacing);
  }

  var rules = [];

  if (spacing.sm != null) {
    rules.push("@media (max-width: ".concat(_breakpoints.bpMedium, ") {").concat(getSpacingOptions(theme, spacing.sm), "}"));
  }

  if (spacing.md != null || spacing.sm != null) {
    rules.push("@media (min-width: ".concat(_breakpoints.bpMedium, ") {").concat(getSpacingOptions(theme, spacing.md || spacing.sm), "}"));
  }

  if (spacing.lg != null || spacing.md != null || spacing.sm != null) {
    rules.push("@media (min-width: ".concat(_breakpoints.bpLarge, ") {").concat(getSpacingOptions(theme, spacing.lg || spacing.md || spacing.sm), "}"));
  }

  if (spacing.xl != null || spacing.lg != null || spacing.md != null || spacing.sm != null) {
    rules.push("@media (min-width: ".concat(_breakpoints.bpXLarge, ") {").concat(getSpacingOptions(theme, spacing.xl || spacing.lg || spacing.md || spacing.sm), "}"));
  }

  return rules.join('\n');
};

exports.getSpacingRules = getSpacingRules;

var getEffectiveBreakPoint = function getEffectiveBreakPoint(spacing, currentBreakpoint) {
  var _candidateBreakPoints;

  var currentIndex = _breakpoints.breakPoints.findIndex(function (bp) {
    return bp === currentBreakpoint;
  });

  var candidateBreakPoints = (0, _lodash.compact)(_breakpoints.breakPoints.slice(0, currentIndex + 1).filter(function (bp) {
    return !!spacing[bp];
  }).reverse());
  return (_candidateBreakPoints = candidateBreakPoints[0]) !== null && _candidateBreakPoints !== void 0 ? _candidateBreakPoints : 'sm';
}; // Gets the space value that's active in the current breakpoint.


var getEffectiveSpaceValue = function getEffectiveSpaceValue(spacing, currentBreakpoint) {
  if (spacing == null || typeof spacing === 'string' || Array.isArray(spacing)) {
    return spacing;
  }

  return spacing[getEffectiveBreakPoint(spacing, currentBreakpoint)];
};

exports.getEffectiveSpaceValue = getEffectiveSpaceValue;