import { __assign, __rest } from "tslib";
import * as React from 'react';
import classNames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray';
import Item from './Item';
export var LastIndexContext = React.createContext(0);
var Space = function (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 = __rest(props, ["size", "align", "className", "children", "direction", "prefixCls", "directionConfig", "split"]);
    var childNodes = toArray(children);
    if (childNodes.length === 0) {
        return null;
    }
    var mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align;
    var cn = classNames("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 (React.createElement(Item, { className: itemClassName, key: itemClassName + "-" + i, direction: direction, size: size, index: i, marginDirection: marginDirection, split: split }, child));
    });
    return (React.createElement("div", { className: "lp-space" },
        React.createElement("div", __assign({ className: cn }, otherProps),
            React.createElement(LastIndexContext.Provider, { value: latestIndex }, nodes))));
};
export default Space;
