import { create } from "zustand";

export type LocationItem = {
    url: string;
    title: string;
    icon?: React.ReactNode;
};

interface ILocationStore {
    path: LocationItem[];
    add: (item: LocationItem) => void;
    last: () => LocationItem | undefined;
    clear: () => void;
}

export type LocationState = ILocationStore;

const useLocationStore = create<LocationState>()((set, get) => ({
    path: [],
    add: (item: LocationItem) => set((state) => ({ path: [...state.path, item] })),
    last: () => {
        const path = get().path;
        return path.length - 1 >= 0 ? path[path.length - 1] : undefined; // Retorna el último elemento o `undefined` si el array está vacío
    },
    clear: () =>
        set(() => ({
            path: [],
        })),
}));

export { useLocationStore };
