import React from 'react';
export default ((children, space, axis, reverse, divider) => {
  let childrenArray = React.Children.toArray(children);
  const orientation = axis === 'X' ? 'vertical' : 'horizontal';

  if (divider) {
    divider = /*#__PURE__*/React.cloneElement(divider, { ...divider.props,
      orientation
    });
    childrenArray = childrenArray.reduce((r, a) => r.concat(a, divider), [divider]);
    childrenArray = childrenArray.slice(1, -1);
  }
  /*
  | Separate the trailing (not first) children from the children array
  */


  const trailingChildren = reverse === 'reverse' ? childrenArray.slice(0, -1) : childrenArray.slice(1);
  /*
  | Set margin prop based on axis
  */

  let spaceValue;

  if (typeof space === 'string') {
    switch (space) {
      case 'gutter':
        spaceValue = 0;
        break;

      case '2xs':
        spaceValue = 1;
        break;

      case 'xs':
        spaceValue = 2;
        break;

      case 'sm':
        spaceValue = 3;
        break;

      case 'md':
        spaceValue = 4;
        break;

      case 'lg':
        spaceValue = 6;
        break;

      case 'xl':
        spaceValue = 7;
        break;

      case '2xl':
        spaceValue = 8;
        break;

      default:
        spaceValue = 1;
        break;
    }
  } else {
    spaceValue = space;
  }

  const marginProp = { ...(axis === 'X' ? {
      ml: spaceValue
    } : {
      mt: spaceValue
    })
  };
  /*
  | Add the margiin to the children
  */

  /*
  | New children array with applied margin to trailing children
  */

  if (reverse === 'reverse') {
    const trailingChildrenWithSpacingReverse = trailingChildren.reverse().map((child, index) => {
      return /*#__PURE__*/React.cloneElement(child, {
        key: "reverse-spaced-child-".concat(index),
        ...child.props,
        ...marginProp
      }, child.props.children);
    });
    return [childrenArray[childrenArray.length - 1], trailingChildrenWithSpacingReverse];
  } else {
    const trailingChildrenWithSpacing = trailingChildren.map((child, index) => {
      return /*#__PURE__*/React.cloneElement(child, {
        key: "spaced-child-".concat(index),
        ...child.props,
        ...marginProp
      }, child.props.children);
    });
    return [childrenArray[0], trailingChildrenWithSpacing];
  }
});
//# sourceMappingURL=getSpacedChildren.js.map