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 React, { useMemo, useState } from 'react';
import { locales } from '@pisell/utils';
import { DiscountKeyboard } from "../../../../pro/priceKeyboard";
import useEngineContext from "../../../../hooks/useEngineContext";
import { t } from "../i18n";
var formatMoney = function formatMoney(value, currencySymbol) {
  return "".concat(currencySymbol).concat(Number(value || 0).toFixed(2));
};
var clampDueAmount = function clampDueAmount(value, maxValue) {
  return Math.min(Math.max(Number(value) || 0, 0), Math.max(Number(maxValue) || 0, 0));
};
var resolveDiscountedDueAmount = function resolveDiscountedDueAmount(autoDueAmount, discountAmount) {
  return clampDueAmount(Number(autoDueAmount || 0) - Number(discountAmount || 0), autoDueAmount);
};
var buildAmountDueHint = function buildAmountDueHint(taxText) {
  return t('pisell2.adjust-time.amount-due-hint').replace('{tax}', taxText);
};
var buildAmountDueLabel = function buildAmountDueLabel(ticketCount) {
  if (ticketCount <= 1) return t('pisell2.adjust-time.amount-due');
  return t('pisell2.adjust-time.amount-due-with-ticket-count').replace('{count}', String(ticketCount)).replace('{tickets}', t('pisell2.adjust-time.ticket-count').replace('{count}', String(ticketCount)));
};

/**
 * Amount Due 区域：点击金额弹出数字键盘，支持最大值校验。
 */
export var AdjustTimeAmountPanel = function AdjustTimeAmountPanel(_ref) {
  var currencySymbol = _ref.currencySymbol,
    newTotal = _ref.newTotal,
    paidAmount = _ref.paidAmount,
    dueAmount = _ref.dueAmount,
    autoDueAmount = _ref.autoDueAmount,
    _ref$ticketCount = _ref.ticketCount,
    ticketCount = _ref$ticketCount === void 0 ? 1 : _ref$ticketCount,
    unitDueAmount = _ref.unitDueAmount,
    priceInputState = _ref.priceInputState,
    onDueAmountChange = _ref.onDueAmountChange,
    onPriceInputStateChange = _ref.onPriceInputStateChange,
    onAuto = _ref.onAuto;
  var context = useEngineContext();
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    keyboardOpen = _useState2[0],
    setKeyboardOpen = _useState2[1];
  var amountDueHint = useMemo(function () {
    var _context$appHelper, _getData;
    var getData = (_context$appHelper = context.appHelper) === null || _context$appHelper === void 0 || (_context$appHelper = _context$appHelper.utils) === null || _context$appHelper === void 0 ? void 0 : _context$appHelper.getData;
    var parsedTaxRate = Number((_getData = getData === null || getData === void 0 ? void 0 : getData('tax_rate')) !== null && _getData !== void 0 ? _getData : 10);
    var taxRate = Number.isNaN(parsedTaxRate) ? 0.1 : parsedTaxRate / 100;
    var taxTitle = (getData === null || getData === void 0 ? void 0 : getData('tax_title')) || 'GST';
    var includedText = getData !== null && getData !== void 0 && getData('is_price_include_tax') ? locales.getText('pisell2.subtotal.included') : locales.getText('pisell2.subtotal.not-contain');
    var taxText = "".concat(taxTitle, " ").concat((taxRate * 100).toFixed(2), "% ").concat(includedText);
    return buildAmountDueHint(taxText);
  }, [context]);
  return /*#__PURE__*/React.createElement("section", null, /*#__PURE__*/React.createElement("div", {
    className: "pisell-adjust-time__price-section-title"
  }, t('pisell2.adjust-time.amount-due'), ticketCount > 1 ? /*#__PURE__*/React.createElement("em", {
    className: "pisell-adjust-time__ticket-count"
  }, t('pisell2.adjust-time.ticket-count').replace('{count}', String(ticketCount))) : null, ' ', /*#__PURE__*/React.createElement("span", {
    style: {
      color: 'var(--muted)'
    }
  }, amountDueHint)), /*#__PURE__*/React.createElement("div", {
    className: "pisell-adjust-time__price-grid",
    style: {
      marginTop: 8
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-adjust-time__price-stat"
  }, t('pisell2.adjust-time.new-total'), " ", /*#__PURE__*/React.createElement("strong", null, formatMoney(newTotal, currencySymbol))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-adjust-time__price-stat"
  }, t('pisell2.adjust-time.paid'), " ", /*#__PURE__*/React.createElement("strong", null, formatMoney(paidAmount, currencySymbol))), /*#__PURE__*/React.createElement("div", {
    className: "pisell-adjust-time__amount-editor"
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-adjust-time__amount-label"
  }, buildAmountDueLabel(ticketCount)), /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "pisell-adjust-time__amount-input",
    "aria-label": t('pisell2.adjust-time.amount-due-label'),
    onClick: function onClick() {
      return setKeyboardOpen(true);
    }
  }, /*#__PURE__*/React.createElement("span", {
    className: "pisell-adjust-time__amount-input-text"
  }, formatMoney(dueAmount, currencySymbol))), /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "pisell-adjust-time__auto-btn",
    onClick: onAuto
  }, t('pisell2.adjust-time.auto')), ticketCount > 1 ? /*#__PURE__*/React.createElement("div", {
    className: "pisell-adjust-time__amount-multiplier"
  }, "".concat(formatMoney(unitDueAmount !== null && unitDueAmount !== void 0 ? unitDueAmount : dueAmount / ticketCount, currencySymbol), " x ").concat(ticketCount, " = ").concat(formatMoney(dueAmount, currencySymbol))) : null)), /*#__PURE__*/React.createElement(DiscountKeyboard, {
    containerProps: {
      renderType: 'modal',
      open: keyboardOpen,
      onCancel: function onCancel() {
        return setKeyboardOpen(false);
      },
      className: 'pisell-discount-keyboard-modal pisell-adjust-time-payment-keyboard-wrap'
    },
    selectType: "dark",
    amountSymbol: currencySymbol,
    defaultValue: String(dueAmount || 0),
    originalValue: String(autoDueAmount || 0),
    valueState: priceInputState,
    onValueStateChange: onPriceInputStateChange,
    doneText: locales.getText('confirm') || 'Done',
    onEnter: function onEnter(discountAmount, state) {
      setKeyboardOpen(false);
      onPriceInputStateChange === null || onPriceInputStateChange === void 0 || onPriceInputStateChange(state);
      onDueAmountChange(resolveDiscountedDueAmount(autoDueAmount, discountAmount), state);
    }
  }));
};