import { apiClient } from '@/api/client';
import type { Platform } from './types';

export const electronPlatform: Platform = {
  kind: 'electron',

  window: {
    minimize: () => window.electronWindow.minimize(),
    maximize: () => window.electronWindow.maximize(),
    fullscreen: () => window.electronWindow.fullscreen(),
    close: () => window.electronWindow.close(),
    onMaximizeChange: (cb) => window.electronWindow.onMaximizeChange(cb),
    get platform() {
      const p = window.electronWindow?.platform;
      return p === 'darwin' || p === 'win32' || p === 'linux' ? p : 'linux';
    },
  },

  huddle: {
    isSupported: true,
    open: (args) => window.electronWindow.openHuddle(args),
    close: () => window.electronWindow.closeHuddle(),
    getScreenSources: () => window.electronHuddle.getScreenSources(),
  },

  screenRecord: {
    isSupported: true,
    start: () => window.electronScreen.startRecordingWindow(),
    stop: () => window.electronScreenRecord.stop(),
    cancel: () => window.electronScreen.stopRecordingWindow(),
    onStopRequested: (cb) => window.electronScreen.onStopRequested(cb),
    getScreenSources: () => window.electronScreen.getScreenSources(),
  },

  autoplay: {
    play: (payload) => window.electronAutoplay.play(payload),
    dismiss: () => window.electronAutoplay.dismiss(),
    navigate: (d) => window.electronAutoplay.navigate(d),
    onPlay: (cb) => window.electronAutoplay.onPlay(cb),
    onStop: (cb) => window.electronAutoplay.onStop(cb),
    onNavigate: (cb) => window.electronAutoplay.onNavigate(cb),
  },

  link: {
    fetchMetadata: (url) => apiClient.getLinkMetadata(url).catch(() => null),
    openExternal: (url) => window.electronLink.openExternal(url),
  },

  attachment: {
    download: (url, filename) =>
      window.electronAttachment.download(url, filename),
  },

  app: {
    setDockBadge: (count) => window.electronApp.setDockBadge(count),
    getVersion: () => window.electronApp.getVersion(),
  },

  deepLink: {
    getPending: () => window.electronDeepLink.getPending(),
    onNavigate: (cb) => window.electronDeepLink.onNavigate(cb),
  },
};
