import type { BrowserWindow } from 'electron'
import type { ProjectJson } from './project'

export interface LoadPayload {
  path: string
  json: ProjectJson
  mode: 'edit' | 'publish'
}

let pending: LoadPayload | null = null

/** 渲染层尚未就绪时暂存待加载项目 */
export function setPendingLoad(p: LoadPayload | null): void {
  pending = p
}

/** 渲染层主动消费暂存项目（启动时） */
export function consumePendingLoad(): LoadPayload | null {
  const p = pending
  pending = null
  return p
}

/** 实时推送给渲染层并聚焦窗口（second-instance 场景） */
export function pushLoad(win: BrowserWindow, payload: LoadPayload): void {
  if (win.isDestroyed()) return
  win.webContents.send('app:loadProject', payload)
  if (win.isMinimized()) win.restore()
  win.show()
  win.focus()
}
