import { persist, createJSONStorage } from "zustand/middleware";
import { create } from "zustand";

export enum LOGIN_ACTIONS {
    PLAN_SUBSCRIPTION = "plan-subscription",
    ORGANIZATION_CREATE = "organization-create",
    APPOINTMENT_CREATE = "appointment-create",
    NONE = "none",
}

export interface ILoginActionStore {
    action: LOGIN_ACTIONS;
    url: string;
    setUrl: (url: string) => void;
    setLoginAction: (action: LOGIN_ACTIONS) => void;
    clear: () => void;
}

const useLoginActionStore = create<ILoginActionStore>()(
    persist(
        (set) => ({
            action: LOGIN_ACTIONS.NONE,
            url: "/",
            setLoginAction: (action: LOGIN_ACTIONS) => set(() => ({ action: action })),
            setUrl: (url: string) => set(() => ({ url: url })),
            clear: () => set(() => ({ action: LOGIN_ACTIONS.NONE, url: "/" })),
        }),
        {
            name: "site-login-action",
            storage: createJSONStorage(() => localStorage),
        }
    )
);

export { useLoginActionStore };
