import { showHome, openPath, initWatcher } from './router.js';
import { initCssModal } from './ui/css-modal.js';
import { initSidebarResize } from './ui/sidebar-resize.js';

const win = window.__TAURI__.window.getCurrentWindow();

document.getElementById('titlebar').addEventListener('mousedown', (e) => {
  if (e.target.closest('.titlebar-controls')) return;
  if (e.buttons === 1) win.startDragging();
});

document.getElementById('titlebar-minimize').addEventListener('click', () => win.minimize());
document.getElementById('titlebar-maximize').addEventListener('click', async () => {
  if (await win.isMaximized()) win.unmaximize();
  else win.maximize();
});
document.getElementById('titlebar-close').addEventListener('click', () => win.close());

document.getElementById('btn-open-file').addEventListener('click', async () => {
  const selected = await window.__TAURI__.dialog.open({
    multiple: false,
    filters: [{ name: 'Markdown', extensions: ['md'] }],
  });
  if (selected == null) return;
  await openPath(selected, 'file');
});

document.getElementById('btn-open-dir').addEventListener('click', async () => {
  const selected = await window.__TAURI__.dialog.open({ directory: true });
  if (selected == null) return;
  await openPath(selected, 'dir');
});

document.getElementById('btn-back').addEventListener('click', showHome);

await initCssModal();
initSidebarResize();
await initWatcher();
showHome();
