import { invoke } from "@tauri-apps/api/core";
import { openUrl as opener } from "@tauri-apps/plugin-opener";
import type { Shortcut } from "../store/types";

/**
 * Tauri 命令的薄封装层，集中处理 invoke 名称与错误打印，方便前端调用。
 */

export async function loadState(): Promise<string> {
  return invoke<string>("load_state");
}

export async function saveState(json: string): Promise<void> {
  await invoke("save_state", { json });
}

/** 执行一个快捷方式：url 走 opener，其他类型走 Rust runner */
export async function runShortcut(s: Shortcut): Promise<void> {
  if (s.type === "url") {
    await opener(s.target);
    return;
  }
  await invoke("run_shortcut", { shortcut: s });
}

export async function setAlwaysOnTop(on: boolean): Promise<void> {
  await invoke("set_always_on_top", { on });
}

export async function setWindowSize(width: number, height: number): Promise<void> {
  await invoke("set_window_size", { width: Math.round(width), height: Math.round(height) });
}

export async function openDataDir(): Promise<string> {
  return invoke<string>("open_data_dir");
}

export async function notifyUser(title: string, body: string): Promise<void> {
  await invoke("notify_user", { title, body });
}

/** 钉子模式：窗口点击穿透，仅热区可点 */
export async function setPinMode(on: boolean): Promise<void> {
  await invoke("set_pin_mode", { on });
}

/** 上报钉子按钮热区（逻辑像素） */
export async function updatePinHotzone(
  x: number,
  y: number,
  width: number,
  height: number,
  active: boolean
): Promise<void> {
  await invoke("update_pin_hotzone", { x, y, width, height, active });
}
