function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { Skeleton } from 'antd';
import classNames from 'classnames';
import React, { useMemo, useState } from 'react';
import { Iconfont } from '@pisell/materials';
import { formatAmount, locales } from '@pisell/utils';
import useEngineContext from "../../../../hooks/useEngineContext";
import { SubTotal } from "../../../../index";
import "./index.less";
var EMPTY_AMOUNT_VIEW = {
  totalLabel: '',
  totalAmountText: '',
  isRefundAmount: false,
  arrearsText: '',
  subTotalList: [],
  orderTotalList: [],
  hasOrderTotal: false
};
export function AmountDetails(_ref) {
  var _context$appHelper, _amountContext$sales, _amountContext$sales2, _amountContext$summar;
  var _ref$defaultExpanded = _ref.defaultExpanded,
    defaultExpanded = _ref$defaultExpanded === void 0 ? false : _ref$defaultExpanded,
    amountContext = _ref.context;
  var context = useEngineContext();
  var _context$appHelper$ut = (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.utils,
    getData = _context$appHelper$ut.getData;
  var _useState = useState(defaultExpanded),
    _useState2 = _slicedToArray(_useState, 2),
    showDetail = _useState2[0],
    setShowDetail = _useState2[1];
  var amountView = useMemo(function () {
    var _ref2, _amountContext$sales$, _amountContext$sales$2, _amountContext$sales$3, _ref3, _ref4, _tempOrder$payments$l, _tempOrder$payments, _tempOrder$payment_st, _getData;
    /** React 快照可能滞后于 OS；优先读顶层 tempOrder，否则回退 live getTempOrder。 */
    var tempOrder = (_ref2 = (_amountContext$sales$ = amountContext.sales.tempOrder) !== null && _amountContext$sales$ !== void 0 ? _amountContext$sales$ : (_amountContext$sales$2 = amountContext.sales.bookingTicket) === null || _amountContext$sales$2 === void 0 || (_amountContext$sales$3 = _amountContext$sales$2.getTempOrder) === null || _amountContext$sales$3 === void 0 ? void 0 : _amountContext$sales$3.call(_amountContext$sales$2)) !== null && _ref2 !== void 0 ? _ref2 : null;
    var summary = (_ref3 = (_ref4 = amountContext.summary.summary) !== null && _ref4 !== void 0 ? _ref4 : tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.summary) !== null && _ref3 !== void 0 ? _ref3 : null;
    if (!summary) {
      return EMPTY_AMOUNT_VIEW;
    }
    var symbol = context.appHelper.utils.getSymbolic();
    var translationOriginal = context.appHelper.utils.translationOriginal;
    var hasPayment = ((_tempOrder$payments$l = tempOrder === null || tempOrder === void 0 || (_tempOrder$payments = tempOrder.payments) === null || _tempOrder$payments === void 0 ? void 0 : _tempOrder$payments.length) !== null && _tempOrder$payments$l !== void 0 ? _tempOrder$payments$l : 0) > 0;
    var hasOrderPaidAmount = Number(summary.order_paid_amount) > 0;
    var isFullyPaid = String((_tempOrder$payment_st = tempOrder === null || tempOrder === void 0 ? void 0 : tempOrder.payment_status) !== null && _tempOrder$payment_st !== void 0 ? _tempOrder$payment_st : '').toLowerCase() === 'paid';
    var isRefundAmount = Number(summary.amount_gap) < 0;
    var displayAmount = hasOrderPaidAmount && !isFullyPaid ? summary.amount_gap : summary.total_amount;
    var totalLabelKey = isRefundAmount ? 'pisell2.sale-detail.refund' : isFullyPaid ? 'pisell2.sale-detail.fully-paid' : 'pisell2.sale-detail.to-pay';
    var totalLabel = locales.getText(totalLabelKey);
    var parsedTaxRate = Number((_getData = getData('tax_rate')) !== null && _getData !== void 0 ? _getData : 10);
    var surcharges = summary.surcharges ? summary.surcharges : [];
    var surchargeList = surcharges.filter(function (surcharge) {
      return surcharge.name;
    }).map(function (surcharge) {
      return {
        key: "custom_surcharge_".concat(surcharge.surcharge_id),
        value: surcharge.amount,
        label: translationOriginal(surcharge.name)
      };
    });
    var subTotalList = [{
      key: 'sub_total',
      value: summary.product_amount
    }].concat(_toConsumableArray(surchargeList), [{
      key: 'tax',
      value: summary.tax_fee,
      tax: {
        is_price_include_tax: getData('is_price_include_tax'),
        rate: Number.isNaN(parsedTaxRate) ? 0.1 : parsedTaxRate / 100,
        title: getData('tax_title') || 'GST'
      }
    }, {
      key: 'discount',
      value: summary.discount_amount
    }, {
      key: 'expect_amount',
      value: summary.expect_amount,
      label: locales.getText('pisell2.sale-detail.total')
    }, {
      hidden: !Number(summary.rounding_amount),
      key: 'custom_roundingAmount',
      value: summary.rounding_amount,
      label: locales.getText('pisell2.sale-detail.rounding')
    }]);
    var orderTotalList = [{
      hidden: !hasPayment,
      key: 'custom_orderPaidAmount',
      value: summary.order_paid_amount,
      label: locales.getText('pisell2.text.amount-paid-on-order')
    }, {
      hidden: !hasPayment,
      key: 'custom_actualPaidAmount',
      value: summary.customer_paid_amount,
      label: locales.getText('pisell2.text.amount-actually-paid-by-the-customer')
    }, {
      hidden: !hasPayment || !Number(summary.total_refund_amount),
      key: 'custom_totalRefundAmount',
      value: summary.total_refund_amount,
      label: locales.getText('pisell2.text.refunded-amount')
    }];
    return {
      totalLabel: totalLabel,
      totalAmountText: formatAmount(displayAmount, 2, symbol),
      isRefundAmount: isRefundAmount,
      arrearsText: '',
      subTotalList: subTotalList,
      orderTotalList: orderTotalList,
      hasOrderTotal: hasPayment
    };
  }, [context, (_amountContext$sales = amountContext.sales) === null || _amountContext$sales === void 0 ? void 0 : _amountContext$sales.tempOrder, (_amountContext$sales2 = amountContext.sales) === null || _amountContext$sales2 === void 0 ? void 0 : _amountContext$sales2.bookingTicket, (_amountContext$summar = amountContext.summary) === null || _amountContext$summar === void 0 ? void 0 : _amountContext$summar.summary]);
  return /*#__PURE__*/React.createElement("section", {
    className: classNames('pisell-sale-detail-amount-details', {
      'pisell-sale-detail-amount-details--open': showDetail
    })
  }, /*#__PURE__*/React.createElement(Skeleton, {
    title: false,
    round: true,
    loading: false,
    active: true
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-sale-detail-amount-details__summary"
  }, /*#__PURE__*/React.createElement("span", {
    className: "pisell-sale-detail-amount-details__summary-left"
  }, /*#__PURE__*/React.createElement("span", {
    className: "pisell-sale-detail-amount-details__summary-total"
  }, /*#__PURE__*/React.createElement("span", {
    className: classNames('pisell-sale-detail-amount-details__summary-label', {
      'pisell-sale-detail-amount-details__summary-label--refund': amountView.isRefundAmount
    })
  }, amountView.totalLabel), ": ", amountView.totalAmountText), /*#__PURE__*/React.createElement("span", {
    className: "pisell-sale-detail-amount-details__summary-arrears"
  }, amountView.arrearsText)), /*#__PURE__*/React.createElement("span", {
    className: "pisell-sale-detail-amount-details__details-trigger",
    onClick: function onClick() {
      return setShowDetail(!showDetail);
    }
  }, locales.getText('pisell2.sale-detail.details'), ' ', /*#__PURE__*/React.createElement("span", {
    className: classNames('pisell-sale-detail-amount-details__details-icon', {
      'pisell-sale-detail-amount-details__details-icon--open': showDetail
    })
  }, /*#__PURE__*/React.createElement(Iconfont, {
    type: "pisell2-chevron-down"
  })))), /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-sale-detail-amount-details__details', {
      'pisell-sale-detail-amount-details__details--open': showDetail
    })
  }, /*#__PURE__*/React.createElement(SubTotal, {
    mode: "column1",
    list: amountView.subTotalList,
    style: {
      padding: 0
    }
  }), amountView.hasOrderTotal ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-sale-detail-amount-details__divider"
  }) : null, amountView.hasOrderTotal ? /*#__PURE__*/React.createElement(SubTotal, {
    mode: "column1",
    list: amountView.orderTotalList,
    style: {
      padding: ' 0 0 16px'
    }
  }) : null)));
}