"use strict";
const electron = require("electron");
const preload = require("@electron-toolkit/preload");
electron.contextBridge.exposeInMainWorld("electronAPI", {
  fetchData: (url) => electron.ipcRenderer.invoke("fetch-data", url)
});
const api = {
  // 检查API服务器状态
  checkApiStatus: () => electron.ipcRenderer.invoke("check-api-status"),
  // 向主进程发送消息
  send: (channel, data) => {
    const validChannels = ["toMain", "api-request"];
    if (validChannels.includes(channel)) {
      electron.ipcRenderer.send(channel, data);
    }
  },
  // 从主进程接收消息
  receive: (channel, func) => {
    const validChannels = ["fromMain", "api-response"];
    if (validChannels.includes(channel)) {
      electron.ipcRenderer.removeAllListeners(channel);
      electron.ipcRenderer.on(channel, (_, ...args) => func(...args));
    }
  },
  // 系统状态相关 API
  getSystemStatus: () => electron.ipcRenderer.invoke("api-call", { endpoint: "/status", method: "get" }),
  initializeSystem: (config) => electron.ipcRenderer.invoke("api-call", { endpoint: "/initialize", method: "post", data: config }),
  // 灯光控制 API
  openLight: () => electron.ipcRenderer.invoke("api-call", { endpoint: "/light/open", method: "post" }),
  closeLight: () => electron.ipcRenderer.invoke("api-call", { endpoint: "/light/close", method: "post" }),
  // 流程控制 API
  startProcess: (config) => electron.ipcRenderer.invoke("api-call", { endpoint: "/start", method: "post", data: config }),
  stopProcess: () => electron.ipcRenderer.invoke("api-call", { endpoint: "/stop", method: "post" }),
  // 清砂相关 API
  cleanSand: (options) => electron.ipcRenderer.invoke("api-call", { endpoint: "/clean", method: "post", data: options }),
  getCleanStatus: () => electron.ipcRenderer.invoke("api-call", { endpoint: "/clean/status", method: "get" }),
  stopCleanProcess: () => electron.ipcRenderer.invoke("api-call", { endpoint: "/clean/stop", method: "post" }),
  // 电子秤相关 API
  getScalePorts: () => electron.ipcRenderer.invoke("api-call", { endpoint: "/scale/ports", method: "get" }),
  getScaleStatus: () => electron.ipcRenderer.invoke("api-call", { endpoint: "/scale/status", method: "get" }),
  connectScale: (port) => electron.ipcRenderer.invoke("api-call", { endpoint: `/scale/connect/${port}`, method: "post" }),
  disconnectScale: () => electron.ipcRenderer.invoke("api-call", { endpoint: "/scale/disconnect", method: "post" }),
  calibrateZero: () => electron.ipcRenderer.invoke("api-call", { endpoint: "/scale/calibrate/zero", method: "post" }),
  calibrateGain: (calibrationWeight, slaveAddress = 1) => electron.ipcRenderer.invoke("api-call", {
    endpoint: "/scale/calibrate/gain",
    method: "post",
    data: { calibration_weight: calibrationWeight, slave_address: slaveAddress }
  }),
  getWeight: () => electron.ipcRenderer.invoke("api-call", { endpoint: "/scale/weight", method: "get" }),
  // 图片相关 API
  getDirectoryImages: (directory) => electron.ipcRenderer.invoke("api-call", {
    endpoint: "/images/list",
    method: "get",
    params: { directory }
  }),
  getImageUrl: (imagePath) => electron.ipcRenderer.invoke("get-image-url", { imagePath }),
  // 讯飞星火大模型 API
  chatWithXfyun: (userMessage) => electron.ipcRenderer.invoke("chat-with-xfyun", { userMessage }),
  // 测试服务器连接
  testServerConnection: () => electron.ipcRenderer.invoke("test-server-connection")
};
if (process.contextIsolated) {
  try {
    electron.contextBridge.exposeInMainWorld("electron", preload.electronAPI);
    electron.contextBridge.exposeInMainWorld("api", api);
  } catch (error) {
    console.error(error);
  }
} else {
  window.electron = preload.electronAPI;
  window.api = api;
}
