import { createSlice, createSelector } from '@reduxjs/toolkit'
import type { PayloadAction } from '@reduxjs/toolkit'

export interface IUTM {
    [key: string]: string
}

const initialState: IUTM = {}

const utmSlice = createSlice({
    name: 'utm',
    initialState,
    reducers: {
        update(state, action: PayloadAction<IUTM>) {
            return { ...state, ...action.payload }
        },
    },
    extraReducers: (builder) => builder.addCase('reset', () => initialState)
})

export const { actions } = utmSlice
export const selectUTM = createSelector(
    (state: { utm: IUTM }) => state.utm,
    (utm) => utm
)
export default utmSlice.reducer
