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; }
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable @typescript-eslint/no-unused-expressions */
/* eslint-disable no-useless-escape */
/* eslint-disable no-restricted-globals */
import { locales } from '@pisell/utils';
import { InputNumber, Button, message } from 'antd';
import { Decimal } from 'decimal.js';
import React, { useMemo, useState } from 'react';
import { useMount } from 'ahooks';
import Alert from "./component/alert";
import Titlebar from "./component/header/titlebar";
import { getAmonunt } from "./manufacturer";
import { limitDecimals } from "./helper";
import { useDispatch, useContextData, updateComponent, updateState } from "./store";
import payLog from "./utils/payLog";

/**
 * @title: 支付金额信息配置页
 * @description: 配置支付金额与手续费
 * @return React.Element
 * @Author: hongbing.wang
 * @Date: 2024-08-12 11:35
 */
export default (function (_ref) {
  var _ref$className = _ref.className,
    className = _ref$className === void 0 ? '' : _ref$className,
    onChange = _ref.onChange,
    onClose = _ref.onClose,
    _ref$formatAmount = _ref.formatAmount,
    formatAmount = _ref$formatAmount === void 0 ? function (number, precision, symbol) {
      return "".concat(symbol).concat(number);
    } : _ref$formatAmount,
    isMobile = _ref.isMobile,
    net = _ref.net,
    client = _ref.client;
  var dispatch = useDispatch();
  var _useState = useState(true),
    _useState2 = _slicedToArray(_useState, 2),
    status = _useState2[0],
    setStatus = _useState2[1];
  var store = useContextData();
  var _getAmonunt = getAmonunt(store),
    name = _getAmonunt.name,
    symbol = _getAmonunt.symbol,
    money = _getAmonunt.money,
    lastMoney = _getAmonunt.amount,
    surRate = _getAmonunt.surRate,
    surMoney = _getAmonunt.surMoney,
    useOriginSurcharge = _getAmonunt.useOriginSurcharge;
  var _useState3 = useState(surRate),
    _useState4 = _slicedToArray(_useState3, 2),
    curSurRate = _useState4[0],
    setSurRate = _useState4[1];
  var _useState5 = useState(surMoney),
    _useState6 = _slicedToArray(_useState5, 2),
    curSurMoney = _useState6[0],
    setSurMoney = _useState6[1];
  var _useState7 = useState(lastMoney),
    _useState8 = _slicedToArray(_useState7, 2),
    currentMoney = _useState8[0],
    setCurrentMoney = _useState8[1];
  var _useState9 = useState(curSurRate),
    _useState10 = _slicedToArray(_useState9, 2),
    tempSurRate = _useState10[0],
    setTempSurRate = _useState10[1];
  var _useState11 = useState(curSurMoney),
    _useState12 = _slicedToArray(_useState11, 2),
    tempSurMoney = _useState12[0],
    setTempSurMoney = _useState12[1];
  var surRateMoney = useMemo(function () {
    var surRateDeci = new Decimal(curSurRate);
    var moneyDeci = new Decimal(currentMoney);
    return moneyDeci.mul(surRateDeci.div(100)).toFixed(2, Decimal.ROUND_HALF_UP);
  }, [currentMoney, curSurRate]);

  /**
   * @title: 总手续费
   * @description: 总手续费 = 比例手续费 + 固定手续费
   * @return  总手续费
   * @Author: hongbing.wang
   * @Date: 2024-08-19 09:51
   */
  var totalSurMoney = useMemo(function () {
    var surRateDeci = new Decimal(curSurRate);
    var moneyDeci = new Decimal(currentMoney);
    var surMoneyDeci = new Decimal(curSurMoney);
    return moneyDeci.mul(surRateDeci.div(100)).add(surMoneyDeci).toFixed(2, Decimal.ROUND_HALF_UP);
  }, [currentMoney, curSurRate, curSurMoney]);

  /**
   * @title: 总金额
   * @description: 总金额 = 总手续费 + 订单金额
   * @return 总金额
   * @Author: hongbing.wang
   * @Date: 2024-08-19 09:52
   */
  var total = useMemo(function () {
    var totalSurDeci = new Decimal(totalSurMoney);
    var moneyDeci = new Decimal(currentMoney);
    return totalSurDeci.add(moneyDeci).toFixed(2, Decimal.ROUND_HALF_UP);
  }, [currentMoney, totalSurMoney]);
  var subtracted = useMemo(function () {
    var moneyDeci = new Decimal(money);
    var currentMoneyDeci = new Decimal(currentMoney);
    return moneyDeci.sub(currentMoneyDeci).toFixed(2, Decimal.ROUND_HALF_UP);
  }, [currentMoney, money]);
  useMount(function () {
    payLog.log('金额页面展示', {
      amount: currentMoney,
      surcharge: totalSurMoney,
      total: total,
      surRate: curSurRate
    });
  });

  /**
   * @title:提交
   * @description: 1. 修改金额页面提交 前往下一步, 修改收费费金额 前往修改金额页
   * @return {*}
   * @Author: hongbing.wang
   * @Date: 2024-08-19 09:50
   */
  var onSubmit = function onSubmit() {
    if (!status) {
      setStatus(true);
      setSurRate(tempSurRate);
      setSurMoney(tempSurMoney);
      return;
    }
    if (!net) {
      message.open({
        key: 'net-warning',
        type: 'warning',
        content: locales.getText('page.eftpos-pay.text.not.connected')
      });
      return;
    }
    var rate = new Decimal(curSurRate).div(100).toFixed(4, Decimal.ROUND_HALF_UP);
    var pay = {
      sourceAmount: "".concat(money),
      amount: "".concat(currentMoney),
      surcharge: "".concat(totalSurMoney),
      total: "".concat(total),
      surPercentRate: "".concat(rate),
      surRate: "".concat(curSurRate),
      surMoney: "".concat(curSurMoney),
      surRateMoney: "".concat(surRateMoney),
      totalUnit: formatAmount(total, 2, symbol)
    };
    payLog.log('金额页-确认提交', {
      pay: pay
    });
    dispatch(updateState({
      pay: pay
    }));
    dispatch(updateComponent(''));
    onChange && onChange('page', 'pay');
  };

  /**
   * @title: 取消
   * @description: 跳出 支付页 返回支付失败
   * @Author: hongbing.wang
   * @Date: 2024-08-19 09:52
   */
  var onCancel = function onCancel() {
    if (!status) {
      setStatus(true);
      return;
    }
    payLog.log('金额页-点击取消');
    onClose();
  };

  /**
   * @title: 回到编辑页
   * @description:
   * @Author: hongbing.wang
   * @Date: 2024-08-19 09:53
   */
  var onEdit = function onEdit() {
    payLog.log('金额页-点击编辑');
    setStatus(false);
    setTempSurRate(curSurRate);
    setTempSurMoney(curSurMoney);
  };

  /**
   * 1. 金额编辑页  status :true
   * 2. 手续费编辑页 status :false
   * isMobile 确认关闭按钮 出现在哪里
   */
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-amount ".concat(className)
  }, isMobile ? null : /*#__PURE__*/React.createElement(Titlebar, {
    type: 'left-title-close',
    onClose: onClose
  }, status ? name : locales.getText('page.eftpos-pay.text.surcharge.edit.title')), status ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-amount-body"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-cash",
    "data-money": formatAmount(money, 2, symbol)
  }, locales.getText('page.eftpos-pay.text.amount.to.pay')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-money"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-money-title"
  }, locales.getText('page.eftpos-pay.text.input.title')(name)), /*#__PURE__*/React.createElement(InputNumber, {
    type: "number",
    addonBefore: symbol || '$',
    max: money,
    precision: 2,
    min: 0,
    style: {
      width: '100%'
    },
    value: currentMoney,
    formatter: limitDecimals,
    parser: limitDecimals,
    onChange: function onChange(event) {
      if (event !== null && event !== undefined) {
        setCurrentMoney(event || 0);
      } else {
        setCurrentMoney(0);
      }
    }
  }), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-money-tip"
  }, locales.getText('page.eftpos-pay.text.amount.cash.left.pay.paid')(formatAmount("".concat(subtracted), 2, symbol)))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-surcharge"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-details"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-total"
  }, locales.getText('page.eftpos-pay.text.surcharge.edit.actual.pay'), ":", formatAmount("".concat(total), 2, symbol)), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-money"
  }, locales.getText('page.eftpos-pay.text.surcharge.edit.inc.surcharge'), ": ", formatAmount("".concat(totalSurMoney), 2, symbol), " (", locales.getText('page.eftpos-pay.text.surcharge.edit.rate'), ":", curSurRate, "% + ", formatAmount("".concat(curSurMoney), 2, symbol), ")")), /*#__PURE__*/React.createElement(Button, {
    type: "link",
    onClick: onEdit,
    disabled: useOriginSurcharge
  }, locales.getText('page.eftpos-pay.text.surcharge.edit.surcharge')))) : /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-amount-body"
  }, /*#__PURE__*/React.createElement(Alert, null, locales.getText('page.eftpos-pay.text.surcharge.percentage-fixed')), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-amount-form"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-amount-item"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-amount-item-title"
  }, locales.getText('page.eftpos-pay.text.surcharge.percentage')), /*#__PURE__*/React.createElement(InputNumber, {
    type: "number",
    value: tempSurRate,
    min: 0,
    max: 100,
    precision: 2,
    addonAfter: "%",
    style: {
      width: '100%'
    },
    onChange: function onChange(e) {
      setTempSurRate(e || 0);
    }
  })), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-amount-item"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-amount-item-title"
  }, locales.getText('page.eftpos-pay.text.surcharge.fixed')), /*#__PURE__*/React.createElement(InputNumber, {
    type: "number",
    value: tempSurMoney,
    min: 0,
    max: 100000,
    precision: 2,
    style: {
      width: '100%'
    },
    addonBefore: symbol || '$',
    onChange: function onChange(e) {
      setTempSurMoney(e || 0);
    }
  })))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__eft-pay-step-item-btns"
  }, /*#__PURE__*/React.createElement(Button, {
    block: true,
    className: "pisell-lowcode__eft-pay-step-item-btn",
    onClick: onCancel
  }, locales.getText('page.eftpos-pay.text.cancel')), /*#__PURE__*/React.createElement(Button, {
    block: true,
    className: "pisell-lowcode__eft-pay-step-item-btn",
    type: "primary",
    onClick: onSubmit,
    disabled: currentMoney <= 0 && status
  }, locales.getText('page.eftpos-pay.text.sure'))));
});