"use strict";

import AsyncStorage from '@react-native-async-storage/async-storage';
// -------------------------------------------------------------------
// Lightweight RN persistence layer. Mirrors what redux-persist gives
// the web component:
//
//   - persist chatSettingStore.user (sanitized) and rooms state
//   - blacklist transient fields (modals, activeRoomJID, etc.)
//   - cap each room's messages to the most recent MESSAGE_LIMIT (100)
//   - debounced writes (200ms) on relevant action types
//
// Not encrypted. We rely on AsyncStorage's per-app sandbox.
// -------------------------------------------------------------------

const KEY_CHAT = '@ethora/persist:chatSettingStore';
const KEY_ROOMS = '@ethora/persist:rooms';

// Per-room message cap on disk. Mirror this value in `roomsSlice`'s
// in-memory cap (see `enforceMessageCap`) so the runtime and persisted
// shapes stay aligned.
export const MESSAGE_LIMIT = 100;
const sanitizeUser = user => {
  if (!user) {
    return null;
  }
  // Drop secrets we don't want at rest — destructure to omit, then
  // explicitly zero them in the persisted shape.
  const {
    token: _token,
    refreshToken: _refreshToken,
    xmppPassword: _xmppPassword,
    ...rest
  } = user;
  return {
    ...rest,
    token: '',
    refreshToken: '',
    xmppPassword: ''
  };
};
const sanitizeMessages = messages => {
  if (!Array.isArray(messages)) {
    return [];
  }
  if (messages.length <= MESSAGE_LIMIT) {
    return messages;
  }
  return messages.slice(-MESSAGE_LIMIT);
};
const sanitizeRooms = rooms => {
  if (!rooms || typeof rooms !== 'object') {
    return {};
  }
  const out = {};
  for (const [jid, room] of Object.entries(rooms)) {
    if (!jid || typeof jid !== 'string' || !jid.includes('@')) {
      continue;
    }
    if (!room || typeof room !== 'object' || Array.isArray(room)) {
      continue;
    }
    out[jid] = {
      ...room,
      messages: sanitizeMessages(room?.messages || []),
      composing: false,
      composingList: [],
      isLoading: false,
      historyPreloadState: 'idle'
    };
  }
  return out;
};
let writeTimer = null;

/**
 * Middleware that debounces writes of the persisted slices to AsyncStorage.
 * Only triggers on actions that actually mutate persisted state.
 */
export const persistenceMiddleware = storeAPI => next => action => {
  const result = next(action);
  const type = action?.type || '';
  if (!type.startsWith('roomMessages/') && !type.startsWith('chat/setUser') && !type.startsWith('chat/updateUser') && !type.startsWith('chat/refreshTokens') && !type.startsWith('chat/logout')) {
    return result;
  }
  if (writeTimer) {
    clearTimeout(writeTimer);
  }
  writeTimer = setTimeout(() => {
    try {
      const state = storeAPI.getState();
      const chatPayload = {
        user: sanitizeUser(state.chatSettingStore?.user)
      };
      const roomsPayload = {
        rooms: sanitizeRooms(state.rooms?.rooms || {})
      };
      AsyncStorage.multiSet([[KEY_CHAT, JSON.stringify(chatPayload)], [KEY_ROOMS, JSON.stringify(roomsPayload)]]).catch(e => console.warn('persist write failed', e));
    } catch (e) {
      console.warn('persist serialize failed', e);
    }
  }, 200);
  return result;
};

/**
 * Read persisted slices from AsyncStorage. Called once at store creation;
 * we DON'T hydrate synchronously (AsyncStorage is async) — instead, the
 * caller dispatches rehydrate actions when the read resolves.
 */
export async function readPersistedState() {
  try {
    const [chatRaw, roomsRaw] = await AsyncStorage.multiGet([KEY_CHAT, KEY_ROOMS]);
    const chat = chatRaw[1] ? JSON.parse(chatRaw[1]) : null;
    const rooms = roomsRaw[1] ? JSON.parse(roomsRaw[1]) : null;
    return {
      chat,
      rooms
    };
  } catch (e) {
    console.warn('persist read failed', e);
    return {
      chat: null,
      rooms: null
    };
  }
}
export async function clearPersistedState() {
  try {
    await AsyncStorage.multiRemove([KEY_CHAT, KEY_ROOMS]);
  } catch (e) {
    console.warn('persist clear failed', e);
  }
}
export const PERSIST_KEYS = {
  KEY_CHAT,
  KEY_ROOMS
};
//# sourceMappingURL=persistence.js.map