import React, { useEffect, useState } from 'react';
import { CheckIcon } from '@heroicons/react/24/solid';
import { PHXCard } from '../Card';
export const PHXStep = ({ className, steps, nextLabel, backLabel, formMode, onSubmit, triggerStep, }) => {
    var _a, _b;
    const [currentStep, setCurrentStep] = useState(1);
    const [listSteps, setListSteps] = useState([]);
    const handleClickToStep = (stepItem) => {
        var _a, _b;
        const isNextStep = ((_a = triggerStep === null || triggerStep === void 0 ? void 0 : triggerStep.find((itemChild) => itemChild.step === currentStep)) === null || _a === void 0 ? void 0 : _a.isPass) ||
            ((_b = triggerStep === null || triggerStep === void 0 ? void 0 : triggerStep.find((itemChild) => itemChild.step === stepItem.step)) === null || _b === void 0 ? void 0 : _b.isPass);
        if (!isNextStep)
            return;
        if (stepItem.step > currentStep) {
            handleNextStep();
        }
        else {
            handleBackStep(stepItem);
        }
    };
    const handleBackStep = (stepItem) => {
        var _a;
        const funcCallBackStep = (_a = triggerStep === null || triggerStep === void 0 ? void 0 : triggerStep.find((func) => func.step === currentStep)) === null || _a === void 0 ? void 0 : _a.onBack;
        if (funcCallBackStep && formMode !== 'readOnly')
            funcCallBackStep();
        if (stepItem && stepItem.step) {
            setCurrentStep(stepItem.step);
        }
        else {
            setCurrentStep((currentStep) => currentStep - 1);
        }
    };
    const handleNextStep = () => {
        var _a;
        const funcCallNextStep = (_a = triggerStep === null || triggerStep === void 0 ? void 0 : triggerStep.find((func) => func.step === currentStep)) === null || _a === void 0 ? void 0 : _a.onNext;
        if (funcCallNextStep && formMode !== 'readOnly')
            funcCallNextStep();
        if (currentStep === (listSteps === null || listSteps === void 0 ? void 0 : listSteps.length)) {
            onSubmit();
            return;
        }
        else {
            listSteps === null || listSteps === void 0 ? void 0 : listSteps.forEach((item) => {
                if (item.step === currentStep + 1 && item.status !== 'complete')
                    item.status = 'current';
                if (item.step === currentStep)
                    item.status = 'complete';
            });
        }
        setCurrentStep((currentStep) => currentStep + 1);
    };
    useEffect(() => {
        if (steps) {
            let listNew = steps === null || steps === void 0 ? void 0 : steps.map((item, index) => {
                if (index === 0) {
                    return {
                        ...item,
                        status: 'current',
                    };
                }
                else {
                    return {
                        ...item,
                        status: 'upcoming',
                    };
                }
            });
            if (formMode !== 'addNew')
                listNew = steps === null || steps === void 0 ? void 0 : steps.map((item) => ({ ...item, status: 'complete' }));
            setListSteps(listNew);
        }
    }, []);
    return (React.createElement("div", { className: className },
        React.createElement(PHXCard, { primaryFooterAction: {
                content: currentStep === (listSteps === null || listSteps === void 0 ? void 0 : listSteps.length) ? (formMode !== 'readOnly' ? 'Submit' : '') : nextLabel,
                submit: false,
                onClick: handleNextStep,
                disabled: ((_a = triggerStep === null || triggerStep === void 0 ? void 0 : triggerStep.find((stepChild) => stepChild.step === currentStep)) === null || _a === void 0 ? void 0 : _a.isPass) === false,
            }, secondaryFooterActions: [{ content: currentStep !== 1 ? backLabel : '', onClick: handleBackStep }] },
            React.createElement("nav", { "aria-label": 'Progress' },
                React.createElement("ol", { role: 'list', className: 'divide-y divide-gray-300 rounded-md border border-gray-300 md:flex md:divide-y-0' }, listSteps &&
                    listSteps.map((stepItem, stepIdx) => (React.createElement("li", { key: stepIdx, className: 'relative cursor-pointer md:flex md:flex-1' },
                        React.createElement("button", { type: 'button', onClick: () => handleClickToStep(stepItem) },
                            stepItem.status === 'complete' ? (React.createElement("div", { className: 'group flex w-full items-center' },
                                React.createElement("span", { className: 'flex items-center px-6 py-3 text-sm font-medium' },
                                    React.createElement("span", { className: 'flex h-10 w-10 flex-shrink-0 items-center justify-center rounded-full bg-indigo-600 group-hover:bg-indigo-800' },
                                        React.createElement(CheckIcon, { className: 'h-6 w-6 text-white', "aria-hidden": 'true' })),
                                    React.createElement("span", { className: 'ml-4 text-sm font-medium text-gray-900' }, stepItem.title)))) : stepItem.status === 'current' ? (React.createElement("div", { className: 'flex items-center px-6 py-3 text-sm font-medium', "aria-current": 'step' },
                                React.createElement("span", { className: 'flex h-10 w-10 flex-shrink-0 items-center justify-center rounded-full border-2 border-indigo-600' },
                                    React.createElement("span", { className: 'text-indigo-600' }, stepItem.step)),
                                React.createElement("span", { className: 'ml-4 text-sm font-medium text-indigo-600' }, stepItem.title))) : (React.createElement("div", { className: 'group flex items-center' },
                                React.createElement("span", { className: 'flex items-center px-6 py-3 text-sm font-medium' },
                                    React.createElement("span", { className: 'flex h-10 w-10 flex-shrink-0 items-center justify-center rounded-full border-2 border-gray-300 group-hover:border-gray-400' },
                                        React.createElement("span", { className: 'text-gray-500 group-hover:text-gray-900' }, stepItem.step)),
                                    React.createElement("span", { className: 'ml-4 text-sm font-medium text-gray-500 group-hover:text-gray-900' }, stepItem.title)))),
                            stepIdx !== (listSteps === null || listSteps === void 0 ? void 0 : listSteps.length) - 1 ? (React.createElement(React.Fragment, null,
                                React.createElement("div", { className: 'absolute right-0 top-0 hidden h-full w-5 md:block', "aria-hidden": 'true' },
                                    React.createElement("svg", { className: 'h-full w-full text-gray-300', viewBox: '0 0 22 80', fill: 'none', preserveAspectRatio: 'none' },
                                        React.createElement("path", { d: 'M0 -2L20 40L0 82', vectorEffect: 'non-scaling-stroke', stroke: 'currentcolor', strokeLinejoin: 'round' }))))) : null)))))), (_b = listSteps === null || listSteps === void 0 ? void 0 : listSteps.find((item) => item.step === currentStep)) === null || _b === void 0 ? void 0 :
            _b.component)));
};
//# sourceMappingURL=Step.js.map