import { keychain } from "../core/vault";
/**
 * Main-window bridge: exposes the frozen `window.chjCore` object with exactly the
 * method names and semantics of the Electron 0.1.0 `corePreload.js`, implemented on
 * top of the CH-J API. The unchanged renderer (`src/ui/app.js`) uses it as before.
 */
import { core } from "../index";
import { bootInfo } from "./boot";
import { installWindowChrome } from "./windowChrome";
import { installActivityReporter } from "./activity";

const { app, config, vault, biometrics, profiles, availability, ssh, diagnostics, updates, modules, standby } = core;
const boot = bootInfo();

const chjCore = Object.freeze({
  platform: boot.platform,
  diagnostics: Object.freeze({
    resolve: diagnostics.resolve,
    start: diagnostics.start,
    cancel: diagnostics.cancel,
    get: diagnostics.get,
    history: diagnostics.history,
    removeHistory: diagnostics.removeHistory,
    export: diagnostics.export,
    copy: diagnostics.copy,
    import: diagnostics.import,
    onProgress: diagnostics.onProgress
  }),
  getInfo: app.getInfo,
  openLegalDocument: app.openLegalDocument,
  getConfig: config.get,
  updateConfig: config.update,
  getLanguageCatalogs: config.catalogs,
  getLanguages: config.languages,
  listPlugins: modules.list,
  getPluginState: modules.getState,
  checkPluginCatalog: modules.checkCatalog,
  installPlugin: modules.install,
  uninstallPlugin: modules.uninstall,
  openPlugin: modules.open,
  setModuleArea: modules.setArea,
  detachModule: modules.detach,
  collapseModule: modules.collapse,
  closeModule: modules.close,
  onModuleActivate: modules.onActivate,
  onPluginWindowState: modules.onWindowState,
  keychain,
  getStandbyHelper: standby.get,
  setStandbyHelper: standby.set,
  onGeneratorUnlockRequested: (callback: (event: unknown) => void) => subscribe("generator:unlockRequested", callback),
  onKeychainChanged: (callback: (event: unknown) => void) => subscribe("keychain:changed", callback),
  getVaultStatus: vault.status,
  createVault: vault.create,
  unlockVault: vault.unlock,
  lockVault: vault.lock,
  resetVault: vault.reset,
  getBiometricStatus: biometrics.status,
  enableBiometrics: biometrics.enable,
  disableBiometrics: biometrics.disable,
  unlockVaultWithBiometrics: biometrics.unlock,
  autoUnlockVaultWithBiometrics: biometrics.autoUnlock,
  authenticateSensitiveAction: biometrics.authenticate,
  onVaultLocked: (callback: (payload: unknown) => void) => subscribeVault(callback),
  listProfiles: profiles.list,
  exportProfiles: profiles.export,
  previewProfileImport: profiles.previewImport,
  commitProfileImport: profiles.commitImport,
  cancelProfileImport: profiles.cancelImport,
  saveProfile: profiles.save,
  deleteProfile: profiles.delete,
  selectPrivateKey: profiles.selectPrivateKey,
  listAvailability: availability.list,
  refreshAvailability: availability.refresh,
  onAvailabilityChanged: availability.onChanged,
  listSshSessions: ssh.list,
  connectSsh: ssh.connect,
  trustSshHostKey: ssh.trustHostKey,
  disconnectSsh: ssh.disconnect,
  sendTerminalInput: ssh.input,
  suggestTerminal: ssh.suggest,
  getTerminalDictionary: ssh.dictionary,
  saveTerminalDictionary: ssh.saveDictionary,
  resizeTerminal: ssh.resize,
  getUpdateState: updates.getState,
  checkForUpdates: updates.check,
  selectUpdateRelease: updates.select,
  downloadUpdate: updates.download,
  installUpdate: updates.install,
  revealUpdate: updates.reveal,
  onUpdateState: updates.onState,
  onSshData: ssh.onData,
  onSshState: ssh.onState,
  onSshError: ssh.onError
});

import { subscribe } from "../transport";
function subscribeVault(callback: (payload: unknown) => void) { return subscribe("vault:locked", callback); }

Object.defineProperty(window, "chjCore", { value: chjCore, writable: false, configurable: false, enumerable: true });
installWindowChrome(boot.platform, [".window-titlebar"], () => [document]);
installActivityReporter();
