import {DocumentVerifierSettings, SdkWizardStep, VerifierSettingsBase} from "./generated/zenid-types.generated.js"; import {SdkVerifierType} from "./generated/zenid-enums.generated.js"; export enum WizardEvent { RequiredStepsAdded= "RequiredStepsAdded", MovedToNext = "MovedToNext", SecondPageDemanded = "SecondPageDemanded", WizardStepsCompleted = "WizardStepsCompleted", } export class SdkWizardStepWithSettings { Step: SdkWizardStep; Settings: VerifierSettingsBase; constructor(step: SdkWizardStep, settings: VerifierSettingsBase = {}) { this.Step = step; this.Settings = settings; if (step.VerifierType !== SdkVerifierType.Document || !step.AcceptableInput || (settings as DocumentVerifierSettings)?.AcceptableInput) return; this.Settings = { ...settings, AcceptableInput: step.AcceptableInput } as DocumentVerifierSettings; } } export default SdkWizardStepWithSettings export class WizardLogic { private steps: SdkWizardStepWithSettings[] = []; private currentStepIndex: number = 0; private listeners: Record void>> = { [WizardEvent.RequiredStepsAdded]: [], [WizardEvent.MovedToNext]: [], [WizardEvent.SecondPageDemanded]: [], [WizardEvent.WizardStepsCompleted]: [], }; addEventListener(event: WizardEvent, listener: (wiz: this) => void): void { this.listeners[event].push(listener); } demandSecondPageOfDocument(actualStep: SdkWizardStepWithSettings, nextStep: SdkWizardStepWithSettings): void { if (!actualStep || actualStep.Step.VerifierType !== SdkVerifierType.Document) return; if (this.totalSteps() <= 1) { this.setRequiredSteps(actualStep, nextStep); return; } let getFirstPossibleDocument = (step: SdkWizardStepWithSettings) => { if (step.Settings) { return (step.Settings as DocumentVerifierSettings)?.AcceptableInput?.PossibleDocuments?.[0]; } return undefined; }; let currentStep = this.getCurrentMandatoryStep(); let currentDocumentFilter = getFirstPossibleDocument(currentStep!); let firstDocumentFilter = getFirstPossibleDocument(actualStep); if (!currentDocumentFilter || !firstDocumentFilter) return; if (currentDocumentFilter.DocumentCode === firstDocumentFilter.DocumentCode && currentDocumentFilter.Page === firstDocumentFilter.Page) { return; // already the correct step } this.steps.splice(this.currentStepIndex, 1, actualStep); this.steps.splice(this.currentStepIndex + 1, 0, nextStep); this.listeners[WizardEvent.SecondPageDemanded]?.forEach(listener => listener(this)); } getCurrentMandatoryStep(): SdkWizardStepWithSettings | undefined { return this.steps[this.currentStepIndex]; } getCurrentStepIndex(): number { return this.currentStepIndex; } getSteps(): SdkWizardStepWithSettings[] { return this.steps; } mandatoryStepsRequired(): boolean { return this.totalSteps() > this.currentStepIndex; } moveToNext(): boolean { const lastStep = this.currentStepIndex + 1 === this.totalSteps(); this.currentStepIndex = Math.min(this.currentStepIndex + 1, this.totalSteps()); const anyStepLeft = this.currentStepIndex < this.totalSteps(); if (anyStepLeft) this.listeners[WizardEvent.MovedToNext]?.forEach(listener => listener(this)); else if (lastStep) { this.listeners[WizardEvent.WizardStepsCompleted]?.forEach(listener => listener(this)); this.reset(); } return anyStepLeft; } reset(): void { this.steps = []; this.currentStepIndex = 0; } setRequiredSteps(...steps: SdkWizardStep[]): void; setRequiredSteps(...steps: SdkWizardStepWithSettings[]): void; setRequiredSteps(...steps: SdkWizardStep[] | SdkWizardStepWithSettings[]): void { if (steps.length === 0) return; const first = steps[0]; if (first instanceof SdkWizardStepWithSettings) this.steps = steps.map(s => s as SdkWizardStepWithSettings); else this.steps = steps.map(s => new SdkWizardStepWithSettings(s as SdkWizardStep)); this.currentStepIndex = 0; if (steps.length > 0) { this.listeners[WizardEvent.RequiredStepsAdded]?.forEach(listener => listener(this)); } } totalSteps(): number { return this.steps.length; } }