/**
 * Dialog 适配层 - 替代微信小程序 @vant/weapp/dialog
 * 将 Vant Dialog 调用适配为 uni.showModal
 */

function Dialog(options) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: options.title || '提示',
      content: options.message || options.msg || '',
      showCancel: options.showCancelButton !== false,
      confirmText: options.confirmButtonText || '确定',
      cancelText: options.cancelButtonText || '取消',
      success(res) {
        if (res.confirm) {
          resolve({ confirm: true, cancel: false });
        } else {
          reject({ confirm: false, cancel: true });
        }
      },
      fail() {
        reject({ confirm: false, cancel: true });
      }
    });
  });
}

Dialog.alert = function(options) {
  if (typeof options === 'string') {
    options = { message: options };
  }
  return new Promise((resolve) => {
    uni.showModal({
      title: options.title || '提示',
      content: options.message || '',
      showCancel: false,
      confirmText: options.confirmButtonText || '确定',
      success() {
        resolve({ confirm: true, cancel: false });
      },
      fail() {
        resolve({ confirm: true, cancel: false });
      }
    });
  });
};

Dialog.confirm = function(options) {
  if (typeof options === 'string') {
    options = { message: options };
  }
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: options.title || '确认',
      content: options.message || '',
      showCancel: true,
      confirmText: options.confirmButtonText || '确定',
      cancelText: options.cancelButtonText || '取消',
      success(res) {
        if (res.confirm) {
          resolve({ confirm: true, cancel: false });
        } else {
          reject({ confirm: false, cancel: true });
        }
      },
      fail() {
        reject({ confirm: false, cancel: true });
      }
    });
  });
};

Dialog.close = function() {
  // uni-app 没有 close 方法，空操作
};

Dialog.setDefaultOptions = function() {};

Dialog.resetDefaultOptions = function() {};

export default Dialog;
