import {GuideTour, Subscription, UserProps} from "@/lib/types/user";
import {SessionProps} from "@/lib/types/session";

export function getCurrentSubscription(user: UserProps | null, type: "quill-sense" | "use-your-keys" | "quill-trial"): Subscription | null {
    if (!user || !user.subscription || user.subscription.length === 0) {
        return null;
    }
    return user.subscription.find((sub: Subscription): boolean => {
        return sub.subType === type && sub.status;
    }) || null;
}

export function getWritingLevel(level: number): string {
    switch (level) {
        case 1:
            return 'Débutant';
        case 2:
            return 'Intermédiaire';
        case 3:
            return 'Avancé';
        default:
            return 'Débutant';
    }
}

export function guideTourDone(guide: GuideTour[], tour: string): boolean {
    if (!guide || !tour) return false;
    return guide.find((guide: GuideTour): boolean => guide[tour]) === undefined;
}

export function setNewGuideTour(session: SessionProps, tour: string): SessionProps {
    if (!session.user) return session;
    const newGuideTour: GuideTour[] = [
        ...(session.user.guideTour ?? []),
        {[tour]: true}
    ];
    return {
        ...session,
        user: {
            ...session.user,
            guideTour: newGuideTour
        }
    };
}
