function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import React from 'react';
import { formatAmount } from '@pisell/utils';
/**
 * 格式化货币金额
 */
export var formatCurrency = function formatCurrency(amount) {
  var amountSymbol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '$';
  return formatAmount(amount, 2, amountSymbol);
};
function isObject(value) {
  return !!value && _typeof(value) === 'object';
}
function getFirstPresentValue(values) {
  return values.find(function (value) {
    return value !== undefined && value !== null && value !== '';
  });
}
function getLegacyPayload(value) {
  if ((value === null || value === void 0 ? void 0 : value.payload) !== undefined) return value.payload;
  if ((value === null || value === void 0 ? void 0 : value.type) === 'callback' && isObject(value.data)) return value.data;
  return value;
}
function getLegacyOrderId(value, payload) {
  var _value$data, _value$data2, _value$response, _value$data3, _payload$response, _payload$data, _payload$data2;
  return getFirstPresentValue([value === null || value === void 0 ? void 0 : value.orderId, value === null || value === void 0 ? void 0 : value.order_id, value === null || value === void 0 || (_value$data = value.data) === null || _value$data === void 0 ? void 0 : _value$data.orderId, value === null || value === void 0 || (_value$data2 = value.data) === null || _value$data2 === void 0 ? void 0 : _value$data2.order_id, value === null || value === void 0 || (_value$response = value.response) === null || _value$response === void 0 || (_value$response = _value$response.data) === null || _value$response === void 0 ? void 0 : _value$response.order_id, value === null || value === void 0 || (_value$data3 = value.data) === null || _value$data3 === void 0 || (_value$data3 = _value$data3.response) === null || _value$data3 === void 0 || (_value$data3 = _value$data3.data) === null || _value$data3 === void 0 ? void 0 : _value$data3.order_id, payload === null || payload === void 0 ? void 0 : payload.orderId, payload === null || payload === void 0 ? void 0 : payload.order_id, payload === null || payload === void 0 || (_payload$response = payload.response) === null || _payload$response === void 0 || (_payload$response = _payload$response.data) === null || _payload$response === void 0 ? void 0 : _payload$response.order_id, payload === null || payload === void 0 || (_payload$data = payload.data) === null || _payload$data === void 0 ? void 0 : _payload$data.order_id, payload === null || payload === void 0 || (_payload$data2 = payload.data) === null || _payload$data2 === void 0 || (_payload$data2 = _payload$data2.response) === null || _payload$data2 === void 0 || (_payload$data2 = _payload$data2.data) === null || _payload$data2 === void 0 ? void 0 : _payload$data2.order_id]);
}
function getLegacyStatus(value, payload) {
  var _value$data4, _payload$toastInfo, _payload$response2;
  return (value === null || value === void 0 ? void 0 : value.status) || (value === null || value === void 0 || (_value$data4 = value.data) === null || _value$data4 === void 0 ? void 0 : _value$data4.status) || (payload === null || payload === void 0 ? void 0 : payload.status) || (payload === null || payload === void 0 || (_payload$toastInfo = payload.toastInfo) === null || _payload$toastInfo === void 0 ? void 0 : _payload$toastInfo.status) || (payload === null || payload === void 0 || (_payload$response2 = payload.response) === null || _payload$response2 === void 0 || (_payload$response2 = _payload$response2.data) === null || _payload$response2 === void 0 ? void 0 : _payload$response2.payment_status);
}
function getLegacyReason(value, payload) {
  var _value$data5, _value$data6;
  var reason = (value === null || value === void 0 ? void 0 : value.reason) || (value === null || value === void 0 || (_value$data5 = value.data) === null || _value$data5 === void 0 ? void 0 : _value$data5.reason) || (payload === null || payload === void 0 ? void 0 : payload.reason);
  if (reason) return reason;
  var source = (value === null || value === void 0 ? void 0 : value.source) || (value === null || value === void 0 || (_value$data6 = value.data) === null || _value$data6 === void 0 ? void 0 : _value$data6.source) || (payload === null || payload === void 0 ? void 0 : payload.source);
  if (source === 'back') return 'back';
  if ((value === null || value === void 0 ? void 0 : value.type) === 'close' || (value === null || value === void 0 ? void 0 : value.type) === 'modal-close') return value !== null && value !== void 0 && value.reset ? 'paid' : 'cancel';
  if (getLegacyStatus(value, payload) === 'cancelled') return 'cancel';
  return undefined;
}
function getDefaultCloseCaller(params) {
  if (params.type === 'toast-close') return false;
  if (params.type === 'error') return false;
  if (params.reason === 'back') return false;
  if (params.status === 'failed') return false;
  if (params.type === 'modal-close') return params.reset === true;
  return params.status === 'success' || params.status === 'paid';
}
function getDefaultRefreshOrder(params) {
  if (params.type === 'toast-close') return false;
  if (params.type === 'error') return false;
  if (params.reason === 'back') return false;
  if (params.status === 'failed') return false;
  return params.reset === true || params.status === 'success' || params.status === 'paid' || params.status === 'partially_paid';
}
export function createPaymentModalCallbackEvent(event) {
  var _event$shouldRefreshO, _event$shouldCloseCal, _event$data, _event$source;
  var shouldRefreshOrder = (_event$shouldRefreshO = event.shouldRefreshOrder) !== null && _event$shouldRefreshO !== void 0 ? _event$shouldRefreshO : getDefaultRefreshOrder({
    type: event.type,
    status: event.status,
    reason: event.reason,
    reset: event.reset
  });
  var shouldCloseCaller = (_event$shouldCloseCal = event.shouldCloseCaller) !== null && _event$shouldCloseCal !== void 0 ? _event$shouldCloseCal : getDefaultCloseCaller({
    type: event.type,
    status: event.status,
    reason: event.reason,
    reset: event.reset
  });
  return _objectSpread(_objectSpread({}, event), {}, {
    shouldRefreshOrder: shouldRefreshOrder,
    shouldCloseCaller: shouldCloseCaller,
    data: (_event$data = event.data) !== null && _event$data !== void 0 ? _event$data : event.payload,
    isCallbackFromToast: event.type === 'toast-close' || event.isCallbackFromToast,
    source: (_event$source = event.source) !== null && _event$source !== void 0 ? _event$source : event.reason
  });
}
export function normalizePaymentModalCallbackEvent(value) {
  var _value$data7, _value$reset, _value$payload2;
  if (!isObject(value)) {
    return createPaymentModalCallbackEvent({
      isChangePaymentNotIncludeVoucher: false,
      type: 'modal-close',
      reason: 'cancel',
      status: 'cancelled',
      payload: value,
      reset: false
    });
  }
  var payload = getLegacyPayload(value);
  if (value.type === 'toast-close' || value.isCallbackFromToast || value !== null && value !== void 0 && (_value$data7 = value.data) !== null && _value$data7 !== void 0 && _value$data7.isCallbackFromToast) {
    return createPaymentModalCallbackEvent(_objectSpread(_objectSpread({}, value), {}, {
      type: 'toast-close',
      status: getLegacyStatus(value, payload),
      reason: value.reason || (payload === null || payload === void 0 ? void 0 : payload.reason),
      orderId: getLegacyOrderId(value, payload),
      payload: payload,
      reset: value.reset,
      shouldRefreshOrder: false,
      shouldCloseCaller: false
    }));
  }
  if (value.type === 'error') {
    var _value$payload;
    return createPaymentModalCallbackEvent(_objectSpread(_objectSpread({}, value), {}, {
      type: 'error',
      orderId: getLegacyOrderId(value, payload),
      payload: (_value$payload = value.payload) !== null && _value$payload !== void 0 ? _value$payload : value.data,
      shouldRefreshOrder: false,
      shouldCloseCaller: false
    }));
  }
  var reason = getLegacyReason(value, payload);
  var status = getLegacyStatus(value, payload);
  var reset = (_value$reset = value.reset) !== null && _value$reset !== void 0 ? _value$reset : payload === null || payload === void 0 ? void 0 : payload.reset;
  var type = value.type === 'modal-close' || value.type === 'close' || reason === 'back' ? 'modal-close' : 'payment-result';
  return createPaymentModalCallbackEvent(_objectSpread(_objectSpread({}, value), {}, {
    type: type,
    status: status,
    reason: reason,
    orderId: getLegacyOrderId(value, payload),
    reset: reset,
    payload: (_value$payload2 = value.payload) !== null && _value$payload2 !== void 0 ? _value$payload2 : payload
  }));
}
export function getPaymentModalEventOrderId(event) {
  var _normalizedEvent$payl, _normalizedEvent$payl2, _normalizedEvent$payl3, _normalizedEvent$payl4, _normalizedEvent$payl5;
  var normalizedEvent = normalizePaymentModalCallbackEvent(event);
  return getFirstPresentValue([normalizedEvent.orderId, (_normalizedEvent$payl = normalizedEvent.payload) === null || _normalizedEvent$payl === void 0 ? void 0 : _normalizedEvent$payl.order_id, (_normalizedEvent$payl2 = normalizedEvent.payload) === null || _normalizedEvent$payl2 === void 0 ? void 0 : _normalizedEvent$payl2.orderId, (_normalizedEvent$payl3 = normalizedEvent.payload) === null || _normalizedEvent$payl3 === void 0 || (_normalizedEvent$payl3 = _normalizedEvent$payl3.response) === null || _normalizedEvent$payl3 === void 0 || (_normalizedEvent$payl3 = _normalizedEvent$payl3.data) === null || _normalizedEvent$payl3 === void 0 ? void 0 : _normalizedEvent$payl3.order_id, (_normalizedEvent$payl4 = normalizedEvent.payload) === null || _normalizedEvent$payl4 === void 0 || (_normalizedEvent$payl4 = _normalizedEvent$payl4.data) === null || _normalizedEvent$payl4 === void 0 ? void 0 : _normalizedEvent$payl4.order_id, (_normalizedEvent$payl5 = normalizedEvent.payload) === null || _normalizedEvent$payl5 === void 0 || (_normalizedEvent$payl5 = _normalizedEvent$payl5.data) === null || _normalizedEvent$payl5 === void 0 || (_normalizedEvent$payl5 = _normalizedEvent$payl5.response) === null || _normalizedEvent$payl5 === void 0 || (_normalizedEvent$payl5 = _normalizedEvent$payl5.data) === null || _normalizedEvent$payl5 === void 0 ? void 0 : _normalizedEvent$payl5.order_id]);
}

/**
 * 根据支付状态计算标题
 */
export var calculateTitle = function calculateTitle(status, failureReason) {
  var getText = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {
    return '';
  };
  switch (status) {
    case 'paid':
    case 'partially_paid':
      return getText('payment.result.success');
    case 'failed':
      var reason = failureReason ? " - ".concat(failureReason) : '';
      return "".concat(getText('payment.result.failed')).concat(reason);
    case 'wallet-pass-redemption-failed':
      return getText('pisell2.checkout.wallet-pass.redemption-failed') || 'Wallet pass redeem failed';
    default:
      return getText('payment.result.success');
  }
};

/**
 * 生成副标题CSS类名的辅助函数
 */
export var getSubtitleClassName = function getSubtitleClassName(colorClass) {
  return "payment-result-toast-subtitle-text ".concat(colorClass);
};

/**
 * 根据支付状态和金额计算副标题
 */
export var calculateSubtitle = function calculateSubtitle(status, amount, gapAmount) {
  var getText = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : function () {
    return '';
  };
  var amountSymbol = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : '$';
  var text;
  var colorClass;
  switch (status) {
    case 'paid':
      if (gapAmount) {
        // 找零金额不为0，显示找零金额
        text = "".concat(getText('payment.result.changeDue') || 'Change Due', " ").concat(formatCurrency(Math.abs(gapAmount), amountSymbol));
        colorClass = 'partially-paid'; // 橙色
      } else {
        text = "".concat(getText('payment.result.paid') || 'Paid', " ").concat(formatCurrency(amount, amountSymbol));
        colorClass = 'paid'; // 绿色
      }
      break;
    case 'partially_paid':
      var displayAmount = gapAmount !== null && gapAmount !== void 0 ? gapAmount : amount;
      text = "".concat(getText('payment.result.partiallyPaid') || 'Partially Paid', " ").concat(formatCurrency(displayAmount, amountSymbol));
      colorClass = 'partially-paid'; // 橙色
      break;
    case 'failed':
      text = "".concat(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(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) {
  var autoCloseDelay = 30;
  switch (status) {
    case 'paid':
      return {
        autoClose: true,
        autoCloseDelay: autoCloseDelay,
        maskClosable: true
      };
    case 'partially_paid':
      return {
        autoClose: true,
        autoCloseDelay: autoCloseDelay,
        maskClosable: true
      };
    case 'failed':
    case 'wallet-pass-redemption-failed':
      return {
        autoClose: false,
        autoCloseDelay: autoCloseDelay,
        maskClosable: false
      };
    default:
      return {
        autoClose: false,
        autoCloseDelay: autoCloseDelay,
        maskClosable: false
      };
  }
};