"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StepScope = exports.JourneyScope = exports.Step = exports.Journey = void 0; const config_1 = require("./config"); /** * Represents an active journey tracking session */ class Journey { constructor(name) { this.metadata = {}; this.steps = []; this._completed = false; this._failed = false; this.id = (0, config_1.generateEventId)(); this.name = name; this.startedAt = new Date(); } /** * Set user context for this journey */ setUser(id, email, data) { this.user = { id, email, data }; return this; } /** * Set metadata for this journey */ setMetadata(key, value) { this.metadata[key] = value; return this; } /** * Start a new step in this journey */ startStep(name, category) { // Complete any existing step if (this.currentStep && this.currentStep.status === 'in_progress') { this.currentStep.status = 'completed'; this.currentStep.endedAt = new Date(); } const step = { name, category, startedAt: new Date(), status: 'in_progress', data: {}, }; this.steps.push(step); this.currentStep = step; return new Step(step, this); } /** * Mark the journey as completed */ complete() { if (this.currentStep && this.currentStep.status === 'in_progress') { this.currentStep.status = 'completed'; this.currentStep.endedAt = new Date(); } this._completed = true; } /** * Mark the journey as failed */ fail() { if (this.currentStep && this.currentStep.status === 'in_progress') { this.currentStep.status = 'failed'; this.currentStep.endedAt = new Date(); } this._failed = true; } /** * Get the journey context for an event */ getContext() { return { journeyId: this.id, name: this.name, currentStep: this.currentStep?.name, startedAt: this.startedAt, metadata: this.metadata, }; } /** * Get the user context for this journey */ getUser() { return this.user; } /** * Check if the journey is complete */ get isComplete() { return this._completed || this._failed; } /** * Get journey ID */ get journeyId() { return this.id; } } exports.Journey = Journey; /** * Represents a step within a journey */ class Step { constructor(step, journey) { this.step = step; this.journey = journey; } /** * Set data for this step */ setData(key, value) { this.step.data[key] = value; return this; } /** * Mark the step as completed */ complete() { this.step.status = 'completed'; this.step.endedAt = new Date(); } /** * Mark the step as failed */ fail() { this.step.status = 'failed'; this.step.endedAt = new Date(); } /** * Get the step name */ get name() { return this.step.name; } /** * Get the parent journey */ getJourney() { return this.journey; } } exports.Step = Step; /** * Journey scope that auto-completes on disposal */ class JourneyScope { constructor(journey, onComplete) { this.journey = journey; this.onComplete = onComplete; } /** * Get the underlying journey */ getJourney() { return this.journey; } /** * Dispose of the journey scope */ [Symbol.dispose]() { if (!this.journey.isComplete) { this.journey.complete(); } this.onComplete?.(); } } exports.JourneyScope = JourneyScope; /** * Step scope that auto-completes on disposal */ class StepScope { constructor(step) { this.step = step; } /** * Get the underlying step */ getStep() { return this.step; } /** * Set data on the step */ setData(key, value) { this.step.setData(key, value); return this; } /** * Dispose of the step scope */ [Symbol.dispose]() { if (this.step['step'].status === 'in_progress') { this.step.complete(); } } } exports.StepScope = StepScope; //# sourceMappingURL=journey.js.map