import React from 'react';
import { locales, formatAmount } from '@pisell/utils';
import Decimal from 'decimal.js';
import { showPaymentResultToastSuccess, showPaymentResultToastFailed } from "../components/PaymentResultToast";

// 支付结果Toast工具类的配置参数接口

// 支付结果Toast显示参数接口

// 格式化货币金额
export var formatCurrency = function formatCurrency(amount) {
  var amountSymbol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '$';
  return formatAmount(amount, 2, amountSymbol);
};

// 根据支付状态和参数计算标题
export var calculateTitle = function calculateTitle(status, failureReason) {
  switch (status) {
    case 'paid':
    case 'partially_paid':
      return locales.getText('payment.result.success');
    case 'failed':
      var reason = failureReason ? " - ".concat(failureReason) : '';
      return "".concat(locales.getText('payment.result.failed')).concat(reason);
    case 'wallet-pass-redemption-failed':
      return locales.getText('pisell2.checkout.wallet-pass.redemption-failed') || 'Wallet pass redeem failed';
    default:
      return locales.getText('payment.result.success');
  }
};

// 根据支付状态和金额计算副标题
export var calculateSubtitle = function calculateSubtitle(status, amount, gapAmount, getData) {
  // 生成CSS类名的辅助函数
  var getSubtitleClassName = function getSubtitleClassName(colorClass) {
    return "payment-result-toast-subtitle-text ".concat(colorClass);
  };
  var text;
  var colorClass;
  var amountSymbol = (getData === null || getData === void 0 ? void 0 : getData('shop_symbol')) || '$';
  switch (status) {
    case 'paid':
      if (gapAmount) {
        // 找零金额不为0，显示找零金额
        text = "".concat(locales.getText('payment.result.changeDue') || 'Change Due', " ").concat(formatCurrency(Math.abs(gapAmount), amountSymbol));
        colorClass = 'partially-paid'; // 橙色
      } else {
        text = "".concat(locales.getText('payment.result.paid') || 'Paid', " ").concat(formatCurrency(amount, amountSymbol));
        colorClass = 'paid'; // 绿色
      }
      break;
    case 'partially_paid':
      var displayAmount = new Decimal(amount).minus(gapAmount !== null && gapAmount !== void 0 ? gapAmount : 0).toNumber();
      text = "".concat(locales.getText('payment.result.partiallyPaid') || 'Partially Paid', " ").concat(formatCurrency(displayAmount, amountSymbol));
      colorClass = 'partially-paid'; // 橙色
      break;
    case 'failed':
      text = "".concat(locales.getText('payment.result.amountDue') || 'Amount Due', " ").concat(formatCurrency(amount, amountSymbol));
      colorClass = 'amount-due'; // 红色
      break;
    case 'wallet-pass-redemption-failed':
      var successAmount = gapAmount !== null && gapAmount !== void 0 ? gapAmount : amount;
      text = "".concat(locales.getText('payment.result.partiallyPaid') || 'Partially Paid', " ").concat(formatCurrency(successAmount, amountSymbol));
      colorClass = 'partially-paid'; // 绿色
      break;
    default:
      return null;
  }
  // 返回带样式的JSX元素，包含完整的结构和样式
  return /*#__PURE__*/React.createElement("div", {
    className: "payment-result-toast-subtitle"
  }, /*#__PURE__*/React.createElement("h1", {
    className: getSubtitleClassName(colorClass)
  }, text));
};

// 根据支付状态计算StatusClassName
export var calculateStatusClassName = function calculateStatusClassName(status) {
  switch (status) {
    case 'paid':
    case 'partially_paid':
      return 'success';
    case 'failed':
      return 'failed';
    case 'wallet-pass-redemption-failed':
      return 'wallet-pass-failed';
    default:
      return 'success';
  }
};

// 根据支付状态计算自动关闭配置
export var calculateAutoCloseConfig = function calculateAutoCloseConfig(status, hasChangeDue) {
  switch (status) {
    case 'paid':
      return {
        autoClose: true,
        autoCloseDelay: hasChangeDue ? 30 : 5,
        // 有找零30秒，无找零5秒
        maskClosable: true
      };
    case 'partially_paid':
      return {
        autoClose: true,
        autoCloseDelay: 5,
        maskClosable: true
      };
    case 'failed':
    case 'wallet-pass-redemption-failed':
      return {
        autoClose: false,
        autoCloseDelay: 0,
        maskClosable: false
      };
    default:
      return {
        autoClose: false,
        autoCloseDelay: 5,
        maskClosable: false
      };
  }
};

// 封装toast显示逻辑
export var displayPaymentResultToast = function displayPaymentResultToast(params, config) {
  var paymentStatus = params.paymentStatus,
    orderTotalAmount = params.orderTotalAmount,
    gapAmount = params.gapAmount,
    failureReason = params.failureReason,
    autoCloseConfig = params.autoCloseConfig,
    actionButtons = params.actionButtons;
  var getData = config.getData,
    onToastClose = config.onToastClose;
  if (paymentStatus === 'paid') {
    // 成功支付的 toast
    showPaymentResultToastSuccess({
      title: calculateTitle(paymentStatus),
      subtitle: calculateSubtitle(paymentStatus, orderTotalAmount, gapAmount, getData),
      autoClose: (autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
      autoCloseDelay: (autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoCloseDelay) || 3000,
      showCountdownProgress: true,
      actionButtons: actionButtons || [],
      onClose: onToastClose
    });
  } else if (paymentStatus === 'partially_paid') {
    showPaymentResultToastSuccess({
      title: calculateTitle(paymentStatus),
      subtitle: calculateSubtitle(paymentStatus, orderTotalAmount, gapAmount, getData),
      autoClose: (autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
      actionButtons: actionButtons || [],
      onClose: onToastClose
    });
  } else if (paymentStatus === 'failed' || paymentStatus === 'error') {
    // 失败支付的 toast
    showPaymentResultToastFailed({
      title: calculateTitle(paymentStatus, failureReason),
      subtitle: calculateSubtitle(paymentStatus, orderTotalAmount / 100, gapAmount, getData),
      autoClose: (autoCloseConfig === null || autoCloseConfig === void 0 ? void 0 : autoCloseConfig.autoClose) || false,
      actionButtons: actionButtons || [],
      onClose: onToastClose
    });
  }
};

// 显示成功Toast的包装方法
export var createShowSuccessToast = function createShowSuccessToast(config, getActionButtons) {
  return function (result) {
    var _result$response;
    // 获取支付状态和相关信息
    var responseData = (_result$response = result.response) === null || _result$response === void 0 ? void 0 : _result$response.data;
    // 获取支付状态
    var paymentStatus = responseData === null || responseData === void 0 ? void 0 : responseData.payment_status;
    // 获取订单总金额
    var orderTotalAmount = Number((responseData === null || responseData === void 0 ? void 0 : responseData.total_amount) || 0);
    var gapAmount = Number((responseData === null || responseData === void 0 ? void 0 : responseData.amount_gap) || 0);
    // 计算自动关闭配置
    var autoCloseConfig = calculateAutoCloseConfig(paymentStatus, !!gapAmount);

    // 获取按钮配置
    var actionButtons = getActionButtons ? getActionButtons(paymentStatus) : [];

    // 调用封装的toast显示方法
    displayPaymentResultToast({
      paymentStatus: paymentStatus,
      orderTotalAmount: orderTotalAmount,
      gapAmount: gapAmount,
      autoCloseConfig: autoCloseConfig,
      actionButtons: actionButtons
    }, config);
  };
};