import { Buffer } from 'buffer';
import * as SecureStore from 'expo-secure-store';
import type { SecretStore } from '@chat-app/shared/auth';

// SecretStore contract uses Uint8Array values; SecureStore only takes
// strings, so we base64 at the boundary. iOS Keychain max value size
// is generous (a few MB); private keys are 32 bytes so we are well
// within limits.
export const secretStore: SecretStore = {
  async getSecret(key) {
    const v = await SecureStore.getItemAsync(key);
    return v ? new Uint8Array(Buffer.from(v, 'base64')) : null;
  },
  async setSecret(key, value) {
    await SecureStore.setItemAsync(key, Buffer.from(value).toString('base64'));
  },
  async removeSecret(key) {
    await SecureStore.deleteItemAsync(key);
  },
};
