'use client';
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// packages/ui/src/crud/components/EntityWorkflow.tsx
/**
 * @fileoverview EntityWorkflow component
 * @description Renders a multi-step entity workflow with stepper navigation.
 * Uses useEntityWorkflow for orchestration and EntityFormRenderer for each step's form.
 *
 * @example
 * ```tsx
 * <EntityWorkflow
 *   workflow={onboardingWorkflow}
 *   onComplete={async (data) => {
 *     await api.createAccount(data);
 *     navigate('/dashboard');
 *   }}
 * />
 * ```
 *
 * @version 0.0.1
 * @since 0.0.1
 * @author AMBROISE PARK Consulting
 */
import { Button, Stack, Stepper } from '@donotdev/components';
import { useTranslation } from '@donotdev/core';
import { isCrudModuleAvailable, useEntityWorkflow } from '../crudImports';
import { EntityFormRenderer } from './EntityFormRenderer';
/**
 * EntityWorkflow — renders a multi-step entity workflow.
 *
 * Default mode: auto-renders Stepper + form + navigation buttons.
 * Custom mode: pass a render prop as children for full control.
 */
export function EntityWorkflow({ workflow: config, onComplete, onStepChange, defaultValues, viewerRole, children, className, }) {
    // Safe guard: isCrudModuleAvailable is a module-level constant (immutable after load).
    // eslint-disable-next-line react-hooks/rules-of-hooks
    if (!isCrudModuleAvailable)
        return null;
    const { t } = useTranslation('crud');
    const wf = useEntityWorkflow(config, {
        onComplete,
        onStepChange,
        defaultValues,
    });
    // Custom render mode
    if (children) {
        return _jsx(_Fragment, { children: children(wf) });
    }
    const { currentStep, currentStepIndex, visibleSteps, isFirst, isLast, isSubmitting, stepData, } = wf;
    // Build stepper steps (for the Stepper header)
    const stepperSteps = visibleSteps.map((step, index) => ({
        number: index + 1,
        title: step.title,
        content: null, // Content is rendered separately below
    }));
    return (_jsxs(Stack, { className: className, gap: "large", children: [_jsx(Stepper, { steps: stepperSteps, activeStep: currentStepIndex, showStepNumbers: true, onStepChange: (index) => {
                    // Only allow navigating to completed steps (not jumping forward)
                    if (index <= currentStepIndex) {
                        wf.goToStep(index);
                    }
                } }), currentStep.custom ? (
            // Custom render step (e.g., review/summary)
            currentStep.custom(wf.allData)) : currentStep.entity ? (
            // Entity form step
            _jsx(EntityFormRenderer, { entity: currentStep.entity, operation: "create", viewerRole: viewerRole, defaultValues: {
                    ...stepData[currentStep.id],
                    ...(!isLast && { status: 'draft' }),
                }, onSubmit: async (data) => {
                    await wf.goNext(data);
                }, submitText: isLast
                    ? t('workflow.submit', { defaultValue: 'Submit' })
                    : t('workflow.next', { defaultValue: 'Next' }), cancelText: isFirst
                    ? null
                    : t('workflow.previous', { defaultValue: 'Previous' }), onCancel: isFirst ? undefined : wf.goPrevious, loading: isSubmitting, warnOnUnsavedChanges: false, hideVisibilityInfo: true })) : null, currentStep.allowSkip && !isLast && (_jsx(Button, { variant: "ghost", onClick: wf.skipStep, className: "dndev-w-full", children: t('workflow.skip', { defaultValue: 'Skip this step' }) }))] }));
}
