"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireWildcard(require("react"));

var _Box = require("../Box");

var _Flex = require("../Flex");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __jsx = _react["default"].createElement;

// TODO: Reduce complexity by deprecating isInline and isReversed prop
var Stack = function Stack(_ref) {
  var direction = _ref.direction,
      _ref$isInline = _ref.isInline,
      isInline = _ref$isInline === void 0 ? false : _ref$isInline,
      _ref$isReversed = _ref.isReversed,
      isReversed = _ref$isReversed === void 0 ? false : _ref$isReversed,
      _ref$spacing = _ref.spacing,
      spacing = _ref$spacing === void 0 ? 'spacing-xs' : _ref$spacing,
      children = _ref.children,
      align = _ref.align,
      justify = _ref.justify,
      shouldWrapChildren = _ref.shouldWrapChildren,
      rest = (0, _objectWithoutProperties2["default"])(_ref, ["direction", "isInline", "isReversed", "spacing", "children", "align", "justify", "shouldWrapChildren"]);

  var _isReversed = isReversed || direction && direction.toString().endsWith('reverse');

  var _isInline = isInline || direction && direction.toString().startsWith('row');

  var _direction;

  if (_isInline) {
    _direction = 'row';
  }

  if (_isReversed) {
    _direction = isInline ? 'row-reverse' : 'column-reverse';
  }

  if (direction) {
    _direction = direction;
  }

  if (!_isInline && !_isReversed && !direction) {
    _direction = 'column';
  }

  var validChildrenArray = _react.Children.toArray(children).filter(_react.isValidElement);

  var stackSpacingProps = _direction === 'row' ? {
    mr: 'spacing'
  } : {
    mb: 'spacing'
  };
  return __jsx(_Flex.Flex, (0, _extends2["default"])({
    align: align,
    justify: justify,
    direction: _direction
  }, stackSpacingProps, rest), validChildrenArray.map(function (child, index) {
    var isLastChild = validChildrenArray.length === index + 1;
    var spacingProps = _isInline ? (0, _defineProperty2["default"])({}, _isReversed ? 'ml' : 'mr', isLastChild ? null : spacing) : (0, _defineProperty2["default"])({}, _isReversed ? 'mt' : 'mb', isLastChild ? null : spacing);

    if (shouldWrapChildren) {
      return __jsx(_Box.Box, (0, _extends2["default"])({
        d: "inline-block"
      }, spacingProps, {
        key: "stack-box-wrapper-".concat(index)
      }), child);
    }

    return /*#__PURE__*/(0, _react.cloneElement)(child, spacingProps);
  }));
};

exports.Stack = Stack;
//# sourceMappingURL=Stack.js.map