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 { useState, useEffect } from 'react';
import { Modal } from 'antd';
import "./index.less";
import React from 'react';
import classNames from 'classnames';
import usePrefix from "../../../../hooks/usePrefix";
// @ts-ignore - @pisell/utils types not available
import { locales } from '@pisell/utils';
import checkoutLocales from "../../locales";
import { useEngineContext } from "../../../../hooks";
import useToast from "../../../../hooks/useToast";
import { isWalkIn } from "../../../booking/utils";
function SendPaymentLinkModal(_ref) {
  var _context$appHelper$ut, _context$appHelper$ut2, _context$appHelper$ut3, _context$appHelper$ut4;
  var _ref$balanceAmount = _ref.balanceAmount,
    balanceAmount = _ref$balanceAmount === void 0 ? '$15.1' : _ref$balanceAmount,
    _ref$customerEmail = _ref.customerEmail,
    customerEmail = _ref$customerEmail === void 0 ? 'customer@email.com' : _ref$customerEmail,
    _ref$customerName = _ref.customerName,
    customerName = _ref$customerName === void 0 ? '' : _ref$customerName,
    onClose = _ref.onClose,
    sendPaymentLink = _ref.sendPaymentLink,
    customerId = _ref.customerId;
  var prefix = usePrefix();
  var context = useEngineContext();
  locales.init(checkoutLocales, ((_context$appHelper$ut = context.appHelper.utils) === null || _context$appHelper$ut === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut.getApp) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.call(_context$appHelper$ut)) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut2 = _context$appHelper$ut2.locales) === null || _context$appHelper$ut2 === void 0 || (_context$appHelper$ut3 = _context$appHelper$ut2.getLocale) === null || _context$appHelper$ut3 === void 0 ? void 0 : _context$appHelper$ut3.call(_context$appHelper$ut2)) || ((_context$appHelper$ut4 = context.appHelper.utils) === null || _context$appHelper$ut4 === void 0 || (_context$appHelper$ut4 = _context$appHelper$ut4.storage) === null || _context$appHelper$ut4 === void 0 ? void 0 : _context$appHelper$ut4.get('umi_locale')) || 'en');
  var Toast = useToast();

  // Helper function to get localized text
  var getText = function getText(key, fallback) {
    return locales.getText(key) || fallback;
  };

  // Helper function to format customer name based on language
  var formatCustomerName = function formatCustomerName(customerName) {
    if (isWalkIn(customerId)) {
      return '';
    }

    // Use language pack template for formatting customer name
    var formatTemplate = getText('pisell2.checkout.send-payment-link.customer-name-format');
    if (typeof formatTemplate === 'function') {
      return formatTemplate(customerName);
    }

    // Fallback to English format if template is not available
    return "".concat(customerName, "'s ");
  };

  // Email validation function
  var validateEmail = function validateEmail(email) {
    var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
  };

  // 简化邮箱状态管理 - 只需要一个邮箱输入框
  var _useState = useState(customerEmail || ''),
    _useState2 = _slicedToArray(_useState, 2),
    emailAddress = _useState2[0],
    setEmailAddress = _useState2[1];

  // 当customerEmail变化时同步到emailAddress
  useEffect(function () {
    setEmailAddress(customerEmail || '');
  }, [customerEmail]);
  var handleSendLink = function handleSendLink() {
    console.log('Send link clicked');
    console.log('Email to send:', emailAddress);
    console.log('Amount to send:', balanceAmount);

    // 邮箱校验
    if (!emailAddress || emailAddress.trim() === '') {
      Toast.fail(getText('pisell2.checkout.send-payment-link.email-required'));
      return;
    }
    if (!validateEmail(emailAddress.trim())) {
      Toast.fail(getText('pisell2.checkout.send-payment-link.email-invalid'));
      return;
    }

    // 调用发送支付链接函数
    sendPaymentLink({
      emails: [emailAddress.trim()],
      amount: balanceAmount
    });
    onClose === null || onClose === void 0 || onClose();
  };
  var modalContent = /*#__PURE__*/React.createElement("div", {
    className: classNames('send-payment-link-modal')
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('modal-content')
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('main-content')
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('illustration-container')
  }, /*#__PURE__*/React.createElement("img", {
    src: prefix + '/send_payment_link.png',
    className: classNames('illustration')
  })), /*#__PURE__*/React.createElement("div", {
    className: classNames('form-container')
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('email-options-container')
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('email-section')
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('section-label')
  }, formatCustomerName(customerName), getText('pisell2.checkout.send-payment-link.email-address', 'Email Address')), /*#__PURE__*/React.createElement("div", {
    className: classNames('email-option')
  }, /*#__PURE__*/React.createElement("div", {
    className: classNames('email-input-container')
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classNames('email-input'), " ").concat(classNames('email-input-selected'))
  }, /*#__PURE__*/React.createElement("input", {
    type: "email",
    value: emailAddress,
    onChange: function onChange(e) {
      return setEmailAddress(e.target.value);
    },
    placeholder: getText('pisell2.checkout.send-payment-link.email-placeholder', 'Enter email address'),
    className: classNames('email-text-input')
  })))))), /*#__PURE__*/React.createElement("div", {
    className: classNames('footer')
  }, /*#__PURE__*/React.createElement("button", {
    className: classNames('send-button'),
    onClick: handleSendLink
  }, getText('pisell2.checkout.send-payment-link.send-button', 'Send Link')))))));
  return /*#__PURE__*/React.createElement(Modal, {
    title: getText('pisell2.checkout.send-payment-link.modal-title', 'Send Payment Link'),
    width: 680,
    footer: null,
    centered: true,
    wrapClassName: "send-payment-link-modal-wrapper",
    open: true,
    onCancel: onClose
  }, modalContent);
}
export default SendPaymentLinkModal;