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) => {
        const index = (isElement(child) && child.props.index) ?? 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 = { ...extraProps, activeIndex, stepsLength: steps.length };
    function getSectionContents(renderFunc, stepSection, renderStepFunc) {
        return stepSection
            ? renderStepFunc
                ? renderStepFunc({ ...renderArgs, children: stepSection })
                : stepSection
            : renderFunc && renderFunc(renderArgs);
    }
    return (React.createElement(Flex, { flex: 1, flexDirection: "column", ...extraContainerProps },
        getSectionContents(header, stepHeader, renderStepHeader),
        React.createElement(Flex, { my: 8, justifyContent: "center", ...extraStepperContainerProps },
            React.createElement(Stepper, { activeIndex: activeIndex, steps: steps, flex: 1, ...extraStepperProps })),
        React.createElement(Flex, { flex: 1, flexDirection: "column", position: "relative", ...extraChildrenContainerProps }, renderChildren
            ? renderChildren({ ...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, ...otherProps } = props;
    const activeIndex = React.Children.toArray(children).findIndex(child => {
        const res = isElement(child) && child.props.itemKey === activeKey;
        return res;
    });
    return (React.createElement(FlowStepper, { ...otherProps, activeIndex: activeIndex }, children));
}
function IndexedStep({ children }) {
    return React.createElement(React.Fragment, null, children);
}
FlowStepperIndexed.Step = IndexedStep;
FlowStepper.Indexed = FlowStepperIndexed;
//# sourceMappingURL=index.js.map