import { UIContext, ComponentContent } from '@kit.ArkUI';

// 1. 定义包裹组件 (保持不变)
@Component
struct ToastDeleteImage {
  build() {
    Stack() {
      Image($r('app.media.ic_toast_delete_account'))
        .width(91)
        .height(91)
        .objectFit(ImageFit.Contain)
    }
    .width(120)
    .height(120)
    .backgroundColor('#00000000')
    .borderRadius(16)
  }
}

// 2. 包装 Builder (跨模块调用的核心)
@Builder
function toastDeleteBuilder() {
  ToastDeleteImage()
}

export class ToastDeleteUtils {
  private static content: ComponentContent<Object> | null = null;

  static showDeleteToast(uiContext: UIContext, duration: number = 2000): void {
    // 如果已有弹窗，先清理
    if (ToastDeleteUtils.content) {
      uiContext.getPromptAction().closeCustomDialog(ToastDeleteUtils.content);
    }

    // 使用 wrapBuilder 确保 UI 上下文正确
    ToastDeleteUtils.content = new ComponentContent(uiContext, wrapBuilder(toastDeleteBuilder));

    const prompt = uiContext.getPromptAction();

    // 参数1: content 实例 参数2: 配置对象 (BaseDialogOptions)
    prompt.openCustomDialog(ToastDeleteUtils.content, {
      autoCancel: true,
      alignment: DialogAlignment.Center,
      maskColor: '#00000000'
    }).then(() => {
      setTimeout(() => {
        if (ToastDeleteUtils.content) {
          prompt.closeCustomDialog(ToastDeleteUtils.content);
          ToastDeleteUtils.content = null;
        }
      }, duration);
    });
  }
}
