import { createAction as t, createSlice as e } from "@reduxjs/toolkit"
import {
LAYOUT_VIEW as o,
LAYOUT_TYPE as n,
VIDEO_FEED_VIEW as a,
ROOM_ADD_VIEW as r,
MODAL_VIEW_LAYOUT_TYPES as s,
TOUR_STEPS as i,
ROOM_LIST_TAB as l,
UI_STATE_KEY as I,
UI_MODAL_STATE_KEY as p,
ROOM_INFO_SECTION as c,
UI_USER_PROFILE_STATE_KEY as u,
} from "./ui.constants"
import { UI_MODAL_INITIAL_STATE as m, uiModalReducer as d } from "./ui-modal.reducer"
import { USER_PROFILE_INITIAL_STATE as U, userProfileReducer as S } from "./ui-user-profile.reducer"
import { last as g } from "../lib/array"
export const uiRoomInfoGoToCmd = t("ui/room-info-go-to-cmd")
export const uiRoomInfoGoBackCmd = t("ui/room-info-go-back-cmd")
export const uiRoomInfoGoToMemberCmd = t("ui/room-info-go-to-member-cmd")
export const uiJoinDmCall = t("ui/join-dm-call")
export const uiDeclineDmCall = t("ui/decline-dm-call")
export const setUIPanels = t("ui/set-panels")
const x = (t) => "boolean" == typeof t,
T = {
rooms: !0,
chat: !0,
profile: !1,
roomInfo: !1,
identityProfile: !1,
call: !1,
callParticipants: !1,
feed: !1,
}
export const ROOM_INFO_SECTION_ROOT = { section: c.ROOT, sectionParams: {} }
export const ROOM_INFO_SECTION_STACK_DEFAULT = [ROOM_INFO_SECTION_ROOT]
export const UI_CONFIG = { logoutBannerVisibleTime: 1e3 }
const C = {
appActive: !0,
config: UI_CONFIG,
controls: !1,
menu: null,
layoutType: n.ROOM,
view: o.CHAT,
feedView: a.LIST,
roomAddView: r.NONE,
tour: "",
tourStepIndex: -1,
editing: !1,
roomListTab: l.JOINED,
panels: T,
[p]: m,
[u]: U,
roomInfoSectionsStack: ROOM_INFO_SECTION_STACK_DEFAULT,
incomingDmCallTransition: !1,
}
export const uiSlice = e({
name: I,
initialState: C,
reducers: {
setAppActive: (t, { payload: e }) => {
t.appActive = e
},
setUILayoutType: (t, { payload: e }) => {
;((t.layoutType = e), s.includes(e) && (t.panels.profile = !1))
},
setUIFeedView: (t, { payload: e }) => {
t.feedView = e
},
setUIRoomAddView: (t, { payload: e }) => {
t.roomAddView = e
},
setUIEditing: (t, { payload: e }) => {
t.editing = e
},
tourStart: (t, { payload: e }) => {
;((t.tour = e), (t.tourStepIndex = 0))
},
tourNextStep: (t) => {
;((t.tourStepIndex += 1),
t.tourStepIndex >= i[t.tour]?.length && ((t.tour = ""), (t.tourStepIndex = -1)))
},
tourPrevStep: (t) => {
;((t.tourStepIndex -= 1), t.tourStepIndex < 0 && ((t.tour = ""), (t.tourStepIndex = -1)))
},
tourFinish: (t, e) => {
;((t.tour = ""), (t.tourStepIndex = -1))
},
showUIControls: (t) => {
t.controls = !0
},
hideUIControls: (t) => {
t.controls = !1
},
toggleUIControls: (t, { payload: e }) => {
x(e) ? (t.controls = e) : (t.controls = !t.controls)
},
openUIPanel: (t, { payload: e }) => {
t.panels[e] = !0
},
closeUIPanel: (t, { payload: e }) => {
t.panels[e] = !1
},
resetUIPanels: (t) => ({ ...t, panels: T }),
toggleUIPanel: (t, { payload: { key: e, value: o } }) => {
x(o) ? (t.panels[e] = o) : (t.panels[e] = !t.panels[e])
},
setUIMenuOpen: (t, e) => {
t.menu = e.payload.id
},
setUIMenuClose: (t, e) => {
t.menu === e.payload.id && (t.menu = null)
},
toggleUIMenu: (t, { payload: { id: e } }) => {
t.menu === e ? (t.menu = null) : (t.menu = e)
},
setRoomListTab: (t, { payload: e }) => {
t.roomListTab = e
},
setRoomInfoSectionsStack: (t, e) => {
t.roomInfoSectionsStack = e.payload
},
setUIIncomingDmCallTransition: (t, { payload: e }) => {
t.incomingDmCallTransition = e
},
},
extraReducers: (t) => {
t.addCase(setUIPanels, (t, { payload: e }) => {
Object.keys(e).forEach((o) => {
const n = e[o]
t.panels[o] !== n && (t.panels[o] = n)
})
}).addDefaultCase((t, e) => {
const o = t[u]
;((t[u] = S(o, e)), (t[p] = d(t[p], e)))
})
},
})
export const getUIState = (t) => t[I]
export const getUILayoutType = (t) => getUIState(t).layoutType
export const getUIPanelState = (t) => getUIState(t).panels
export const getUIPanelRoomInfoOpen = (t) => getUIState(t).panels.roomInfo
export const getUIMenu = (t) => getUIState(t).menu
export const getUIAppActive = (t) => getUIState(t).appActive
export const getUIRoomAddView = (t) => getUIState(t).roomAddView
export const getUITour = (t) => getUIState(t).tour
export const getUITourStepIndex = (t) => getUIState(t).tourStepIndex
export const getUITourStepsCount = (t) => i[getUIState(t).tour]?.length || 0
export const makeGetUITourStepMatched = (t, e) => (o) =>
getUIState(o).tour === t && i[t]?.[getUITourStepIndex(o)] === e
export const getUIRoomListTab = (t) => getUIState(t).roomListTab
export const getRoomInfoSectionsStack = (t) => getUIState(t).roomInfoSectionsStack
export const getRoomInfoCurrentSectionData = (t) =>
g(getRoomInfoSectionsStack(t)) || ROOM_INFO_SECTION_ROOT
export const getRoomInfoCurrentSection = (t) => getRoomInfoCurrentSectionData(t).section
export const getRoomInfoCurrentSectionParams = (t) => getRoomInfoCurrentSectionData(t).sectionParams
export const getHasPrevRoomInfoSection = (t) => getUIState(t).roomInfoSectionsStack.length > 1
export const getUIIncomingDmCallTransition = (t) => getUIState(t).incomingDmCallTransition
export const getUICallExpanded = (t) => {
const { rooms: e, chat: o } = getUIPanelState(t)
return !e && !o
}
export const {
setAppActive: setAppActive,
setUILayoutType: setUILayoutType,
setUIFeedView: setUIFeedView,
setUIRoomAddView: setUIRoomAddView,
setUIEditing: setUIEditing,
tourStart: tourStart,
tourNextStep: tourNextStep,
tourPrevStep: tourPrevStep,
tourFinish: tourFinish,
showUIControls: showUIControls,
hideUIControls: hideUIControls,
toggleUIControls: toggleUIControls,
openUIPanel: openUIPanel,
closeUIPanel: closeUIPanel,
resetUIPanels: resetUIPanels,
toggleUIPanel: toggleUIPanel,
setUIMenuOpen: setUIMenuOpen,
setUIMenuClose: setUIMenuClose,
toggleUIMenu: toggleUIMenu,
setRoomListTab: setRoomListTab,
setRoomInfoSectionsStack: setRoomInfoSectionsStack,
setUIIncomingDmCallTransition: setUIIncomingDmCallTransition,
} = uiSlice.actions
export const getUIInitialState = uiSlice.getInitialState
export const uiReducer = uiSlice.reducer