var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
import React from "react";
import { isElement } from "react-is";
import Flex from "../../layout/Flex";
import { Stepper } from "..";
function FlowStepper({ activeIndex, header, renderStepHeader, footer, renderStepFooter, extraProps, extraContainerProps, extraStepperProps, extraStepperContainerProps, extraChildrenContainerProps, renderChildren, children, }) {
    const { steps, innerContents, stepFooter, stepHeader } = React.Children.toArray(children).reduce((acc, child, idx) => {
        var _a;
        const index = (_a = (isElement(child) && child.props.index)) !== null && _a !== void 0 ? _a : idx;
        const label = isElement(child) && child.props.label;
        const hidden = isElement(child) && child.props.hidden;
        const stepHeader = isElement(child) && child.props.header;
        const stepFooter = isElement(child) && child.props.footer;
        if (label && !hidden) {
            acc.steps[index] = label;
        }
        if (index === activeIndex) {
            acc.innerContents = child;
            acc.stepFooter = stepFooter;
            acc.stepHeader = stepHeader;
        }
        return acc;
    }, {
        steps: [],
        innerContents: null,
        stepHeader: null,
        stepFooter: null,
    });
    const renderArgs = Object.assign(Object.assign({}, extraProps), { activeIndex, stepsLength: steps.length });
    function getSectionContents(renderFunc, stepSection, renderStepFunc) {
        return stepSection
            ? renderStepFunc
                ? renderStepFunc(Object.assign(Object.assign({}, renderArgs), { children: stepSection }))
                : stepSection
            : renderFunc && renderFunc(renderArgs);
    }
    return (React.createElement(Flex, Object.assign({ flex: 1, flexDirection: "column" }, extraContainerProps),
        getSectionContents(header, stepHeader, renderStepHeader),
        React.createElement(Flex, Object.assign({ my: 8, justifyContent: "center" }, extraStepperContainerProps),
            React.createElement(Stepper, Object.assign({ activeIndex: activeIndex, steps: steps, flex: 1 }, extraStepperProps))),
        React.createElement(Flex, Object.assign({ flex: 1, flexDirection: "column", position: "relative" }, extraChildrenContainerProps), renderChildren
            ? renderChildren(Object.assign(Object.assign({}, renderArgs), { children: innerContents }))
            : innerContents),
        getSectionContents(footer, stepFooter, renderStepFooter)));
}
function Step({ children }) {
    return React.createElement(React.Fragment, null, children);
}
FlowStepper.Step = Step;
export default FlowStepper;
/**
 * This is a FlowStepper where each child must have an `itemKey: string` prop
 * and the active step is defined by the `activeKey: string` prop.
 *
 * This allows for usages with a lot of steps where dealing with indices could be
 * painful and error prone (for instance inserting/removing a step somewhere would shift
 * the indices of the following steps and navigation would be impacted).
 *
 * By using string identifiers (`itemKey`) for each step, it's more "human readable"
 * and less error prone to setup a navigation logic between steps.
 */
function FlowStepperIndexed(props) {
    const { activeKey, children } = props, otherProps = __rest(props, ["activeKey", "children"]);
    const activeIndex = React.Children.toArray(children).findIndex(child => {
        const res = isElement(child) && child.props.itemKey === activeKey;
        return res;
    });
    return (React.createElement(FlowStepper, Object.assign({}, otherProps, { activeIndex: activeIndex }), children));
}
function IndexedStep({ children }) {
    return React.createElement(React.Fragment, null, children);
}
FlowStepperIndexed.Step = IndexedStep;
FlowStepper.Indexed = FlowStepperIndexed;
//# sourceMappingURL=index.js.map