var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var _a;
import { createSlice } from '@reduxjs/toolkit';
import { getCachesFromEntries } from 'src/lib/common/helpers/cache';
import { getTimeFormatted } from 'src/lib/utils';
export var initMarkerPos = { top: -9999999, left: -9999999 };
var initialState = {};
export var cacheSlice = createSlice({
    name: 'cache',
    initialState: initialState,
    reducers: {
        setCache: function (state, action) {
            var _a;
            var key = action.payload.key;
            var cacheId = action.payload.id;
            var cache = action.payload.cache;
            var geocode = action.payload.geocode;
            var cachesOnKey = state[key];
            var newCache = {
                createdAt: getTimeFormatted(new Date()),
                cache: cache,
                geocode: __assign({}, geocode),
            };
            state[key] = __assign(__assign({}, cachesOnKey), (_a = {}, _a[cacheId] = newCache, _a));
        },
        deleteCache: function (state, action) {
            var key = action.payload.key;
            var id = action.payload.id;
            var cachesOnKey = state[key];
            if (cachesOnKey) {
                var newCaches = getCachesFromEntries(Object.entries(cachesOnKey).filter(function (cache) {
                    var cacheId = cache[0];
                    return cacheId !== id;
                }));
                state[key] = newCaches;
            }
        },
        setCachesOnKey: function (state, action) {
            var key = action.payload.key;
            state[key] = action.payload.caches;
        },
    },
});
export var setCache = (_a = cacheSlice.actions, _a.setCache), deleteCache = _a.deleteCache, setCachesOnKey = _a.setCachesOnKey;
export default cacheSlice.reducer;
