/**
 * 保存聊天記錄到本地緩存（與 $appKit.send / runtime-bridge 統一前綴）
 */
import { STORAGE } from '@/core/constants/storage-keys'

export function setChat(data, send) {
  const uid = send === 'send' ? data.to_id : data.form.id
  const key = STORAGE.chatMsgPrefix + uid
  uni.getStorage({
    key,
    success(res) {
      const arr = res.data.slice(-96)
      arr.push(data)
      uni.setStorageSync(key, arr)
    },
    fail() {
      uni.setStorageSync(key, [data])
    }
  })
  return data
}
