"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.updateUser = exports.unpackAndTransform = exports.setUser = exports.setStoreClient = exports.setSelectedUser = exports.setLangSource = exports.setDeleteModal = exports.setConfig = exports.setActiveModal = exports.setActiveFile = exports.refreshTokens = exports.logout = exports.default = exports.chatSlice = void 0;
var _toolkit = require("@reduxjs/toolkit");
var _LOCAL_STORAGE = require("../helpers/constants/LOCAL_STORAGE");
var _useLocalStorage = require("../hooks/useLocalStorage");
const unpackAndTransform = input => {
  return {
    description: '',
    token: input?.token || '',
    profileImage: input?.profileImage || '',
    _id: input?._id || '',
    walletAddress: input?.defaultWallet?.walletAddress || '',
    xmppPassword: input?.xmppPassword || '',
    refreshToken: input?.refreshToken || '',
    firstName: input?.firstName || '',
    lastName: input?.lastName || '',
    defaultWallet: {
      walletAddress: input?.defaultWallet?.walletAddress || ''
    },
    email: input?.email || '',
    username: input?.username || '',
    appId: input?.appId || '',
    homeScreen: input?.homeScreen || '',
    registrationChannelType: input?.registrationChannelType || '',
    updatedAt: input?.updatedAt || '',
    authMethod: input?.authMethod || '',
    resetPasswordExpires: input?.resetPasswordExpires || '',
    resetPasswordToken: input?.resetPasswordToken || '',
    xmppUsername: input?.xmppUsername || '',
    roles: input?.roles || [],
    tags: input?.tags || [],
    __v: input?.__v || 0,
    isProfileOpen: input?.isProfileOpen || false,
    isAssetsOpen: input?.isAssetsOpen || false,
    isAgreeWithTerms: input?.isAgreeWithTerms || false
  };
};
exports.unpackAndTransform = unpackAndTransform;
const initialState = {
  user: {
    description: '',
    token: '',
    profileImage: '',
    _id: '',
    walletAddress: '',
    xmppPassword: '',
    refreshToken: '',
    firstName: '',
    lastName: '',
    defaultWallet: {
      walletAddress: ''
    },
    email: '',
    username: '',
    appId: '',
    homeScreen: '',
    registrationChannelType: '',
    updatedAt: '',
    authMethod: '',
    resetPasswordExpires: '',
    resetPasswordToken: '',
    xmppUsername: '',
    roles: [],
    tags: [],
    __v: 0,
    isProfileOpen: true,
    isAssetsOpen: true,
    isAgreeWithTerms: false
  },
  deleteModal: {
    isDeleteModal: false,
    roomJid: '',
    messageId: ''
  },
  config: {
    colors: {
      primary: '#0052CD',
      secondary: '#F3F6FC'
    }
  }
};

// Reducers extracted as a named const so the slice can be annotated
// with Slice<State, typeof reducers, Name>. Without this, tsc inlines
// immer's internal WritableNonArrayDraft type in the emitted .d.ts and
// declaration emission fails with TS4023.
const reducers = {
  setUser: (state, action) => {
    state.user = unpackAndTransform(action.payload);
    (0, _useLocalStorage.asyncLocalStorage)(_LOCAL_STORAGE.localStorageConstants.ETHORA_USER).set(unpackAndTransform(action.payload));
  },
  updateUser(state, action) {
    const {
      updates
    } = action.payload;
    const user = state.user;
    if (user) {
      state.user = {
        ...user,
        ...updates
      };
    }
  },
  setConfig: (state, action) => {
    // Cast away immer's WritableDraft — IConfig contains callable
    // fields (eventHandlers, customComponent) that confuse the draft
    // type inference but are inert at write-time.
    state.config = action.payload;
  },
  setActiveModal: (state, action) => {
    state.activeModal = action.payload;
  },
  setActiveFile: (state, action) => {
    state.activeFile = action.payload;
  },
  setDeleteModal: (state, action) => {
    state.deleteModal = action.payload;
  },
  setStoreClient: (state, action) => {
    state.client = action.payload;
  },
  setSelectedUser: (state, action) => {
    state.selectedUser = action.payload;
  },
  refreshTokens: (state, action) => {
    state.user.refreshToken = action.payload.refreshToken;
    state.user.token = action.payload.token;
    // AsyncStorage is async; fire-and-forget so the reducer stays sync.
    (0, _useLocalStorage.asyncLocalStorage)(_LOCAL_STORAGE.localStorageConstants.ETHORA_USER).set(state.user);
  },
  logout: state => {
    state.user = unpackAndTransform();
    state.config = undefined;
    state.client = undefined;
    state.langSource = undefined;
    (0, _useLocalStorage.asyncLocalStorage)(_LOCAL_STORAGE.localStorageConstants.ETHORA_USER).remove();
  },
  setLangSource: (state, action) => {
    state.langSource = action.payload;
  }
};
const chatSlice = exports.chatSlice = (0, _toolkit.createSlice)({
  name: 'chat',
  initialState,
  reducers
});
const {
  setUser,
  setConfig,
  refreshTokens,
  logout,
  setActiveModal,
  setDeleteModal,
  setSelectedUser,
  updateUser,
  setActiveFile,
  setStoreClient,
  setLangSource
} = chatSlice.actions;
exports.setLangSource = setLangSource;
exports.setStoreClient = setStoreClient;
exports.setActiveFile = setActiveFile;
exports.updateUser = updateUser;
exports.setSelectedUser = setSelectedUser;
exports.setDeleteModal = setDeleteModal;
exports.setActiveModal = setActiveModal;
exports.logout = logout;
exports.refreshTokens = refreshTokens;
exports.setConfig = setConfig;
exports.setUser = setUser;
var _default = exports.default = chatSlice.reducer;
//# sourceMappingURL=chatSettingsSlice.js.map