import { create } from 'zustand';
import { UserInterface } from '../types';
import { persist } from "zustand/middleware";

type ZustandState = {
    user: UserInterface;
    updateUser: (userData: Partial<UserInterface>) => void;
    clearUser: () => void;
};

const defaultParams: UserInterface = {
    cardBank: [],
    city_name: "",
    created_at: "",
    dollarBalance: "",
    first_name: "",
    id: "",
    last_name: "",
    mobile_number: "",
    national_code: "",
    profilePic: "",
    province_name: "",
    score: 0,
    shopName: "",
    updated_at: "",
    walletBalance: 0,
    active: true,
    _id: "",
    address: "",
    birthDate: "",
    cell_number: "",
}
export const useUserStore = create<ZustandState>()(
    persist(
        (set) => ({
            user: defaultParams,
            updateUser: (userData: Partial<UserInterface>) =>
                set((state) => ({
                    ...state,
                    user: {
                        ...state.user,
                        ...userData,
                    },
                })),
            clearUser: () =>
                set((state) => ({
                    ...state,
                    user: defaultParams,
                })),
        }),
        {
            name: 'user-store',
            getStorage: () => localStorage
        }
    )
);