import electron from 'electron';
import type { BrowserWindow as BrowserWindowType } from 'electron';
import { join } from 'node:path';

import { resolveWindowIconPath } from '@main/windows/appIcon';

const { app, BrowserWindow, Menu, shell } = electron;

export function createMainWindow(): BrowserWindowType {
  Menu.setApplicationMenu(null);

  const window = new BrowserWindow({
    width: 1440,
    height: 960,
    minWidth: 1120,
    minHeight: 720,
    title: 'aryx',
    icon: resolveWindowIconPath({
      appPath: app.getAppPath(),
      platform: process.platform,
    }),
    backgroundColor: '#09090b',
    titleBarStyle: 'hidden',
    ...(process.platform === 'darwin' && {
      trafficLightPosition: { x: 16, y: 22 },
    }),
    titleBarOverlay: {
      color: '#09090b',
      symbolColor: '#a1a1aa',
      height: 32,
    },
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      contextIsolation: true,
      nodeIntegration: false,
    },
  });

  const rendererUrl = process.env.ELECTRON_RENDERER_URL;

  if (rendererUrl) {
    void window.loadURL(rendererUrl);
  } else {
    void window.loadFile(join(__dirname, '../../dist/renderer/index.html'));
  }

  window.webContents.setWindowOpenHandler(({ url }) => {
    void shell.openExternal(url);
    return { action: 'deny' };
  });

  return window;
}
