import { createAsyncThunk } from '@reduxjs/toolkit' import type { ChannelConfig } from 'api/api.types' import type { Config } from 'config.types' import type { ThunkAPI } from 'domains/redux/redux.types' type InitializeConfigReturn = ChannelConfig & { connectWhenInView: Config['connectWhenInView'] } export const initializeConfig = createAsyncThunk< InitializeConfigReturn, void, ThunkAPI >( 'initializeConfig', async (_, { extra: { api, config }, rejectWithValue }) => { try { const { features, preChat, agentParticipant, defaultContentLocale, defaultUserLocale, userParticipant, startChatIcon, } = await api.getConfig() const { connectWhenInView } = config return { features, preChat, agentParticipant, userParticipant, startChatIcon, defaultUserLocale: config?.context?.userLocale || defaultUserLocale, defaultContentLocale: config?.context?.contentLocale || defaultContentLocale, connectWhenInView, } } catch (error: any) { return rejectWithValue(error) } }, ) export const resetConfig = createAsyncThunk( 'resetConfig', async (_, { extra: { config } }) => { return config }, )