// 与主程序 src/publicJs/w2ui 保持同步的模型库本地副本，这里只做类型适配。
// @ts-expect-error w2ui 本地 JS 暂无配套 d.ts，运行时由 Vite 正常打包。
import * as w2uiLocal from "./w2ui/w2ui.es6.js";

type W2EventHandler = (event: unknown) => void;
type W2EventHost = {
  on(eventName: string, handler: W2EventHandler): W2EventHost;
};

type W2Popup = {
  open(options: {
    title?: string;
    width?: number;
    height?: number;
    modal?: boolean;
    blockPage?: boolean;
    body?: string;
    actions?: Record<string, () => void | Promise<void>>;
  }): {
    self: W2EventHost;
  };
  close(): void;
};

type W2Utils = {
  alert(options: {
    box?: string | HTMLElement;
    title?: string;
    text?: string;
  }): unknown;
  confirm(options: {
    box?: string | HTMLElement;
    title?: string;
    text?: string;
    btn_yes?: {
      text?: string;
      class?: string;
      style?: string;
      attrs?: string;
    };
    btn_no?: {
      text?: string;
      class?: string;
      style?: string;
      attrs?: string;
    };
  }): W2ConfirmPromise;
};

type W2ConfirmPromise = {
  yes(callback: (event: unknown) => void): W2ConfirmPromise;
  no(callback: (event: unknown) => void): W2ConfirmPromise;
  close(callback: (event: unknown) => void): W2ConfirmPromise;
};

type W2LayoutOptions = {
  name: string;
  padding?: number;
  panels: Array<{
    type: "left" | "right" | "top" | "bottom" | "main" | "preview";
    size?: number | string;
    minSize?: number;
    maxSize?: number | false;
    resizable?: boolean;
    overflow?: string;
    html?: string;
  }>;
};

type W2Layout = {
  render(target: HTMLElement): void;
  destroy(): void;
};

const local = w2uiLocal as Record<string, unknown>;

export const w2popup = local.w2popup as W2Popup;
export const w2layout = local.w2layout as new (options: W2LayoutOptions) => W2Layout;
export const w2ui = local.w2ui as Record<string, { destroy?: () => void } | undefined>;
export const w2utils = local.w2utils as W2Utils;
export const w2alert = local.w2alert as (message: string, title?: string) => unknown;
export const w2confirm = local.w2confirm as (
  message: string | { msg: string; title?: string; yes?: string; no?: string },
  title?: string,
  callback?: (action: string) => void
) => unknown;
