// Nordbank — scenario 2 application flow ("batch upload").
//
// This is the same 6-step wizard as <Apply/> but the signed application is
// tagged scenario='batch' in the DB, and there is NO Sherpa checkout iframe
// step. After signing, the user lands directly on the account-setup screen
// (reused from apply.jsx) and then the portal, identical to scenario 1.
//
// All wizard step components are reused verbatim — only the orchestration
// changes. The scenario prop is threaded through ApplyWizard → Step6Sign so
// dbSaveApplication tags the row correctly.

function ApplyBatch() {
  useLang();
  const [app, dispatch] = useApplication();
  const user = useCurrentUser();

  // Mirror Apply's redirect: once signed + account created + logged in,
  // reset the wizard and bounce to /portal so reloads don't replay step 6.
  React.useEffect(() => {
    if (app.signed && app.accountCreated && user) {
      const handle = setTimeout(() => {
        dispatch({ type: 'RESET' });
        navigate('/portal');
      }, 50);
      return () => clearTimeout(handle);
    }
  }, [app.signed, app.accountCreated, user, dispatch]);

  // Step 7 (Sherpa iframe) is intentionally skipped in this scenario.
  if (app.signed && !app.accountCreated) {
    return <AccountSetup app={app} dispatch={dispatch} />;
  }
  if (app.signed && app.accountCreated) {
    // About to redirect via the effect above.
    return null;
  }
  return <ApplyWizard app={app} dispatch={dispatch} scenario="batch" />;
}

Object.assign(window, { ApplyBatch });
