import { create } from 'zustand';

interface ObjectsState {
    id: Record<string, {
        selectedRegion: number | null;
        selectedDistrict: number | null;
        selectedCity: number | null;
        selectedYear: number | null;
        currentObjectId: string | null;
    }>;
}

export const useObjectsStore = create<ObjectsState>(() => ({
    id: {}
}));

export const initializeObjectsState = (
    id: string,
    selectedRegion: number | null,
    selectedDistrict: number | null,
    selectedCity: number | null,
    selectedYear: number | null,
) => useObjectsStore.setState((state) => {
    return {
        id: {
            ...state.id,
            [id]: {
                selectedRegion: selectedRegion,
                selectedDistrict: selectedDistrict,
                selectedCity: selectedCity,
                selectedYear: selectedYear,
                currentObjectId: null,
            }
        }
    }
})

export const getSelectedCity = (id: string) => useObjectsStore.getState().id[id].selectedCity
export const setSelectedCity = (id: string, city: number | null) => useObjectsStore.setState((state) => {
    return {
        id: {
            ...state.id,
            [id]: { ...state.id[id], selectedCity: city }
        }
    }
})

export const getSelectedRegion = (id: string) => useObjectsStore.getState().id[id].selectedRegion
export const setSelectedRegion = (id: string, region: number | null) => useObjectsStore.setState((state) => {
    return {
        id: {
            ...state.id,
            [id]: { ...state.id[id], selectedRegion: region }
        }
    }
})

export const getSelectedDistrict = (id: string) => useObjectsStore.getState().id[id].selectedDistrict
export const setSelectedDistrict = (id: string, district: number | null) => useObjectsStore.setState((state) => {
    return {
        id: {
            ...state.id,
            [id]: { ...state.id[id], selectedDistrict: district }
        }
    }
})


export const getSelectedYear = (id: string) => useObjectsStore.getState().id[id].selectedYear
export const setSelectedYear = (id: string, year: number | null) => useObjectsStore.setState((state) => {
    return {
        id: {
            ...state.id,
            [id]: { ...state.id[id], selectedYear: year }
        }
    }
})

export const getCurrentObjectId = (id: string) => useObjectsStore.getState().id[id].currentObjectId
export const setCurrentObjectId = (id: string, objectId: string | null) => useObjectsStore.setState((state) => {
    return {
        id: {
            ...state.id,
            [id]: { ...state.id[id], currentObjectId: objectId }
        }
    }
})