// See the Electron documentation for details on how to use preload scripts:
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts

import { contextBridge, ipcRenderer } from 'electron';

contextBridge.exposeInMainWorld('electronAPI', {
  setCookie: async (cookieString: string) => {
    const cookies = cookieString.split(';').map(cookie => {
      const [name, value] = cookie.trim().split('=');
      return {
        name: name.trim(),
        value: value.trim(),
        path: '/',
        secure: false,
        httpOnly: false,
        expirationDate: Math.floor(Date.now() / 1000) + 86400 // 24小时后过期
      };
    });

    // 为每个域名设置Cookie
    const domains = ['localhost', '1.12.73.211'];
    const ports = ['', ':8848'];

    for (const cookie of cookies) {
      for (const domain of domains) {
        for (const port of ports) {
          const url = `http://${domain}${port}`;
          await ipcRenderer.invoke('set-cookie', {
            ...cookie,
            url,
            domain
          });
        }
      }
    }
  }
});
