const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {
    // Desktop sources
    getSources: () => ipcRenderer.invoke('get-sources'),
    
    // Fullscreen control
    toggleFullscreen: (displayId) => ipcRenderer.send('toggle-fullscreen', displayId),
    onFullscreenChange: (callback) => {
        const listener = (event, value) => callback(value);
        ipcRenderer.on('fullscreen-change', listener);
        return () => ipcRenderer.removeListener('fullscreen-change', listener);
    },

    // Displays & Projection control
    getDisplays: () => ipcRenderer.invoke('get-displays'),
    startProjection: (data) => ipcRenderer.send('start-projection', data),
    stopProjection: () => ipcRenderer.send('stop-projection'),
    updateTransform: (transform) => ipcRenderer.send('update-transform', transform),
    onSyncTransform: (callback) => {
        const listener = (event, transform) => callback(transform);
        ipcRenderer.on('sync-transform', listener);
        return () => ipcRenderer.removeListener('sync-transform', listener);
    },
    onProjectionClosed: (callback) => {
        const listener = () => callback();
        ipcRenderer.on('projection-closed', listener);
        return () => ipcRenderer.removeListener('projection-closed', listener);
    },
    minimizeToBar: (displayId) => ipcRenderer.send('minimize-to-bar', displayId),
    setCapturedDisplay: (displayId) => ipcRenderer.send('set-captured-display', displayId),
    restoreFromBar: () => ipcRenderer.send('restore-from-bar'),
    moveMiniWindow: (coords) => ipcRenderer.send('move-mini-window', coords),
    minimizeToTaskbar: () => ipcRenderer.send('minimize-to-taskbar'),
    maximizeFromBar: () => ipcRenderer.send('maximize-from-bar'),
    onEnterMiniMode: (callback) => {
        const listener = () => callback();
        ipcRenderer.on('enter-mini-mode', listener);
        return () => ipcRenderer.removeListener('enter-mini-mode', listener);
    },
    onExitMiniMode: (callback) => {
        const listener = () => callback();
        ipcRenderer.on('exit-mini-mode', listener);
        return () => ipcRenderer.removeListener('exit-mini-mode', listener);
    },
    onWindowDisplayChanged: (callback) => {
        const listener = (event, value) => callback(value);
        ipcRenderer.on('window-display-changed', listener);
        return () => ipcRenderer.removeListener('window-display-changed', listener);
    },
    openExternal: (url) => ipcRenderer.send('open-external', url),
    onFullscreenDenied: (callback) => {
        const listener = (event, data) => callback(data);
        ipcRenderer.on('fullscreen-denied', listener);
        return () => ipcRenderer.removeListener('fullscreen-denied', listener);
    },
    onFullscreenLimitReached: (callback) => {
        const listener = (event, data) => callback(data);
        ipcRenderer.on('fullscreen-limit-reached', listener);
        return () => ipcRenderer.removeListener('fullscreen-limit-reached', listener);
    },
    activateLicense: (key) => ipcRenderer.invoke('activate-license', key),
    deactivateLicense: () => ipcRenderer.invoke('deactivate-license'),
    checkLicenseStatus: () => ipcRenderer.invoke('check-license-status')
});
