"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.LastIndexContext = void 0;

var _tslib = require("tslib");

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

var _classnames = _interopRequireDefault(require("classnames"));

var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray"));

var _Item = _interopRequireDefault(require("./Item"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 LastIndexContext = /*#__PURE__*/React.createContext(0);
exports.LastIndexContext = LastIndexContext;

var Space = function Space(props) {
  var _a;

  var _b = props.size,
      size = _b === void 0 ? 'small' : _b,
      align = props.align,
      className = props.className,
      children = props.children,
      _c = props.direction,
      direction = _c === void 0 ? 'horizontal' : _c,
      _d = props.prefixCls,
      prefixCls = _d === void 0 ? "next-" : _d,
      directionConfig = props.directionConfig,
      split = props.split,
      otherProps = (0, _tslib.__rest)(props, ["size", "align", "className", "children", "direction", "prefixCls", "directionConfig", "split"]);
  var childNodes = (0, _toArray["default"])(children);

  if (childNodes.length === 0) {
    return null;
  }

  var mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align;
  var cn = (0, _classnames["default"])("lp-space", "" + prefixCls + direction, (_a = {}, _a[prefixCls + "rtl"] = directionConfig === 'rtl', _a[prefixCls + "align-" + mergedAlign] = mergedAlign, _a), className);
  var itemClassName = prefixCls + "item";
  var marginDirection = directionConfig === 'rtl' ? 'marginLeft' : 'marginRight'; // Calculate latest one

  var latestIndex = 0;
  var nodes = childNodes.map(function (child, i) {
    if (child !== null && child !== undefined) {
      latestIndex = i;
    }

    return /*#__PURE__*/React.createElement(_Item["default"], {
      className: itemClassName,
      key: itemClassName + "-" + i,
      direction: direction,
      size: size,
      index: i,
      marginDirection: marginDirection,
      split: split
    }, child);
  });
  return /*#__PURE__*/React.createElement("div", {
    className: "lp-space"
  }, /*#__PURE__*/React.createElement("div", (0, _tslib.__assign)({
    className: cn
  }, otherProps), /*#__PURE__*/React.createElement(LastIndexContext.Provider, {
    value: latestIndex
  }, nodes)));
};

var _default = Space;
exports["default"] = _default;