/**
 * Electron platform implementation.
 * Delegates to the window.* APIs exposed by preload.cjs.
 */
import SearchDatabase from '@discord-clone/shared/src/utils/SearchDatabase';

const searchDB = new SearchDatabase(
  window.searchStorage,
  {
    encryptData: (data, key) => window.cryptoAPI.encryptData(data, key),
    decryptData: (ct, key, iv, tag) => window.cryptoAPI.decryptData(ct, key, iv, tag),
  }
);

const electronPlatform = {
  crypto: {
    generateKeys: () => window.cryptoAPI.generateKeys(),
    randomBytes: (size) => window.cryptoAPI.randomBytes(size),
    sha256: (data) => window.cryptoAPI.sha256(data),
    signMessage: (privateKey, message) => window.cryptoAPI.signMessage(privateKey, message),
    verifySignature: (publicKey, message, signature) => window.cryptoAPI.verifySignature(publicKey, message, signature),
    deriveAuthKeys: (password, salt) => window.cryptoAPI.deriveAuthKeys(password, salt),
    encryptData: (data, key) => window.cryptoAPI.encryptData(data, key),
    decryptData: (encryptedData, key, iv, tag, options) => window.cryptoAPI.decryptData(encryptedData, key, iv, tag, options),
    decryptBatch: (items) => window.cryptoAPI.decryptBatch(items),
    verifyBatch: (items) => window.cryptoAPI.verifyBatch(items),
    publicEncrypt: (publicKey, data) => window.cryptoAPI.publicEncrypt(publicKey, data),
    privateDecrypt: (privateKey, encryptedHex) => window.cryptoAPI.privateDecrypt(privateKey, encryptedHex),
  },
  session: {
    save: (data) => window.sessionPersistence.save(data),
    load: () => window.sessionPersistence.load(),
    clear: () => window.sessionPersistence.clear(),
  },
  settings: {
    get: (key) => window.appSettings.get(key),
    set: (key, value) => window.appSettings.set(key, value),
  },
  idle: {
    getSystemIdleTime: () => window.idleAPI.getSystemIdleTime(),
    onIdleStateChanged: (callback) => window.idleAPI.onIdleStateChanged(callback),
    removeIdleStateListener: () => window.idleAPI.removeIdleStateListener(),
  },
  links: {
    openExternal: (url) => window.cryptoAPI.openExternal(url),
    fetchMetadata: (url) => window.cryptoAPI.fetchMetadata(url),
  },
  screenCapture: {
    getScreenSources: () => window.cryptoAPI.getScreenSources(),
  },
  windowControls: {
    minimize: () => window.windowControls.minimize(),
    maximize: () => window.windowControls.maximize(),
    close: () => window.windowControls.close(),
    flashFrame: () => window.windowControls.flashFrame(),
  },
  recording: {
    getDefaultFolder: () => window.recordingAPI.getDefaultFolder(),
    pickFolder: () => window.recordingAPI.pickFolder(),
    validateFolder: (dir) => window.recordingAPI.validateFolder(dir),
    openFolder: (dir) => window.recordingAPI.openFolder(dir),
    openSessionFolder: (payload) => window.recordingAPI.openSessionFolder(payload),
    startSession: (payload) => window.recordingAPI.startSession(payload),
    openTrack: (payload) => window.recordingAPI.openTrack(payload),
    append: (payload) => window.recordingAPI.append(payload),
    closeTrack: (payload) => window.recordingAPI.closeTrack(payload),
    finalize: (payload) => window.recordingAPI.finalize(payload),
    listRecoverable: (payload) => window.recordingAPI.listRecoverable(payload),
    recoverSession: (payload) => window.recordingAPI.recoverSession(payload),
  },
  updates: {
    checkUpdate: () => window.updateAPI.checkFlatpakUpdate(),
  },
  systemBars: null,
  searchDB,
  features: {
    hasWindowControls: true,
    hasScreenCapture: true,
    hasNativeUpdates: true,
    hasSearch: true,
    hasSystemBars: false,
    hasRecording: true,
  },
};

export default electronPlatform;
