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, { useState, useEffect, useRef } from 'react';
import { Modal, Input, Checkbox, Button } from 'antd';
// import { X } from '@pisell/icon';
import { locales } from '@pisell/utils';
import { useToast } from '@pisell/materials';
// import PisellPriceRadio from '../../../../pro/pisellPriceRadio';
import PisellNumberSelector from "../../../../pro/pisellNumberSelector";
import PisellPriceKeyboardModal from "../../../../pro/pisellPriceKeyboardModal";
import useEngineContext from "../../../../hooks/useEngineContext";
import "./index.less";
var CustomiseItemModal = function CustomiseItemModal(_ref) {
  var _ref$visible = _ref.visible,
    visible = _ref$visible === void 0 ? false : _ref$visible,
    onClose = _ref.onClose,
    onSave = _ref.onSave,
    _ref$currencySymbol = _ref.currencySymbol,
    currencySymbol = _ref$currencySymbol === void 0 ? '$' : _ref$currencySymbol,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled;
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    productName = _useState2[0],
    setProductName = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    vouchersApplicable = _useState4[0],
    setVouchersApplicable = _useState4[1];
  var _useState5 = useState('sell'),
    _useState6 = _slicedToArray(_useState5, 2),
    priceType = _useState6[0],
    setPriceType = _useState6[1];
  var _useState7 = useState(''),
    _useState8 = _slicedToArray(_useState7, 2),
    sellPrice = _useState8[0],
    setSellPrice = _useState8[1];
  var _useState9 = useState(''),
    _useState10 = _slicedToArray(_useState9, 2),
    deductionPrice = _useState10[0],
    setDeductionPrice = _useState10[1];
  var _useState11 = useState(1),
    _useState12 = _slicedToArray(_useState11, 2),
    quantity = _useState12[0],
    setQuantity = _useState12[1];
  var _useState13 = useState(false),
    _useState14 = _slicedToArray(_useState13, 2),
    showPriceKeyboard = _useState14[0],
    setShowPriceKeyboard = _useState14[1];
  var _useState15 = useState(undefined),
    _useState16 = _slicedToArray(_useState15, 2),
    currentTriggerRef = _useState16[0],
    setCurrentTriggerRef = _useState16[1];

  // 用于追踪触发价格键盘的元素
  var sellPriceButtonRef = useRef(null);
  var deductionPriceButtonRef = useRef(null);
  var context = useEngineContext();
  var Toast = useToast();

  // 重置表单数据
  var resetForm = function resetForm() {
    setProductName('');
    setVouchersApplicable(false);
    setPriceType('sell');
    setSellPrice('');
    setDeductionPrice('');
    setQuantity(1);
    setShowPriceKeyboard(false);
  };

  // 弹窗显示时重置表单
  useEffect(function () {
    if (visible) {
      resetForm();
    }
  }, [visible]);

  // 显示价格键盘
  var showKeyboard = function showKeyboard(triggerRef, priceType) {
    setCurrentTriggerRef(triggerRef);
    setPriceType(priceType);
    setShowPriceKeyboard(true);
  };

  // 处理关闭
  var handleClose = function handleClose() {
    resetForm();
    onClose === null || onClose === void 0 || onClose();
  };

  // 处理保存
  var handleSave = function handleSave() {
    // 验证价格输入
    var currentPriceValue = priceType === 'sell' ? sellPrice : deductionPrice;
    if (!currentPriceValue || currentPriceValue.trim() === '') {
      var priceTypeName = priceType === 'sell' ? locales.getText('pisell2.customise-item.sell-price') : locales.getText('pisell2.customise-item.manual-deduction-price');

      // 显示错误提示
      var errorMessage = locales.getText('pisell2.customise-item.price-required-error')(priceTypeName);
      if (Toast) {
        Toast.info(errorMessage);
      } else {
        alert(errorMessage);
      }
      return;
    }
    var data = {
      productName: productName || locales.getText('pisell2.customise-item.default-product-name'),
      vouchersApplicable: vouchersApplicable,
      priceType: priceType,
      priceValue: currentPriceValue,
      quantity: quantity
    };
    onSave === null || onSave === void 0 || onSave(data);
    handleClose();
  };

  // 商品名称验证 - 放宽验证以支持中文输入法
  var validateProductName = function validateProductName(value) {
    // 只验证长度，支持更宽泛的字符以兼容中文输入法
    return value.length <= 60;
  };

  // 处理商品名称输入
  var handleProductNameChange = function handleProductNameChange(e) {
    var value = e.target.value;

    // 直接更新值，只验证长度
    if (validateProductName(value)) {
      setProductName(value);
    }
  };
  return /*#__PURE__*/React.createElement(Modal, {
    open: visible,
    onCancel: handleClose,
    footer: null,
    closable: false,
    width: 800,
    centered: true,
    className: "customise-item-modal"
  }, /*#__PURE__*/React.createElement("div", {
    className: "customise-item-modal-content"
  }, /*#__PURE__*/React.createElement("div", {
    className: "customise-item-modal-header"
  }, /*#__PURE__*/React.createElement("h2", {
    className: "customise-item-modal-title"
  }, locales.getText('pisell2.customise-item.modal-title')), /*#__PURE__*/React.createElement(Button, {
    type: "text",
    onClick: handleClose,
    className: "customise-item-modal-close",
    size: "large"
  }, "\xD7")), /*#__PURE__*/React.createElement("div", {
    className: "customise-item-modal-body"
  }, /*#__PURE__*/React.createElement("div", {
    className: "customise-item-modal-illustration"
  }, /*#__PURE__*/React.createElement("img", {
    src: "https://static.pisellcdn.com/custom-product-img.png",
    alt: "Custom item illustration",
    className: "customise-item-illustration-image",
    style: {
      width: '100%',
      height: '100%',
      objectFit: 'contain'
    }
  })), /*#__PURE__*/React.createElement("div", {
    className: "customise-item-modal-form"
  }, /*#__PURE__*/React.createElement("div", {
    className: "customise-item-form-group"
  }, /*#__PURE__*/React.createElement("label", {
    className: "customise-item-form-label"
  }, locales.getText('pisell2.customise-item.product-name')), /*#__PURE__*/React.createElement(Input, {
    value: productName,
    onChange: handleProductNameChange,
    placeholder: locales.getText('pisell2.customise-item.default-product-name'),
    maxLength: 60,
    className: "customise-item-product-name-input",
    disabled: disabled
  })), /*#__PURE__*/React.createElement("div", {
    className: "customise-item-form-group"
  }, /*#__PURE__*/React.createElement("div", {
    className: "customise-item-vouchers-checkbox"
  }, /*#__PURE__*/React.createElement("label", {
    className: "vouchers-label",
    onClick: function onClick() {
      return !disabled && setVouchersApplicable(!vouchersApplicable);
    }
  }, locales.getText('pisell2.customise-item.vouchers-applicable')), /*#__PURE__*/React.createElement(Checkbox, {
    checked: vouchersApplicable,
    onChange: function onChange(e) {
      return setVouchersApplicable(e.target.checked);
    },
    disabled: disabled,
    className: "vouchers-checkbox-input"
  }))), /*#__PURE__*/React.createElement("div", {
    className: "customise-item-form-group"
  }, /*#__PURE__*/React.createElement("div", {
    className: "customise-item-price-option"
  }, /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "customise-item-radio ".concat(priceType === 'sell' ? 'active purple' : ''),
    onClick: function onClick() {
      return setPriceType('sell');
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "radio-dot"
  })), /*#__PURE__*/React.createElement("div", {
    className: "customise-item-price-content"
  }, /*#__PURE__*/React.createElement("span", {
    className: "customise-item-price-title ".concat(priceType === 'sell' ? 'purple' : '')
  }, currencySymbol, ' ', locales.getText('pisell2.customise-item.sell-price')), /*#__PURE__*/React.createElement("button", {
    ref: sellPriceButtonRef,
    type: "button",
    className: "customise-item-price-input ".concat(priceType === 'sell' ? 'active purple' : ''),
    onClick: function onClick() {
      return showKeyboard(sellPriceButtonRef, 'sell');
    },
    disabled: disabled
  }, /*#__PURE__*/React.createElement("span", {
    className: "price-currency"
  }, currencySymbol), /*#__PURE__*/React.createElement("div", {
    className: "price-input-display ".concat(!sellPrice ? 'placeholder' : '')
  }, sellPrice || locales.getText('pisell2.customise-item.amount-placeholder'))))), /*#__PURE__*/React.createElement("div", {
    className: "customise-item-price-option"
  }, /*#__PURE__*/React.createElement("button", {
    type: "button",
    className: "customise-item-radio ".concat(priceType === 'deduction' ? 'active red' : ''),
    onClick: function onClick() {
      return setPriceType('deduction');
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "radio-dot"
  })), /*#__PURE__*/React.createElement("div", {
    className: "customise-item-price-content"
  }, /*#__PURE__*/React.createElement("span", {
    className: "customise-item-price-title ".concat(priceType === 'deduction' ? 'red' : '')
  }, currencySymbol, ' ', locales.getText('pisell2.customise-item.manual-deduction-price')), /*#__PURE__*/React.createElement("button", {
    ref: deductionPriceButtonRef,
    type: "button",
    className: "customise-item-price-input ".concat(priceType === 'deduction' ? 'active red' : ''),
    onClick: function onClick() {
      return showKeyboard(deductionPriceButtonRef, 'deduction');
    },
    disabled: disabled
  }, /*#__PURE__*/React.createElement("span", {
    className: "price-currency"
  }, currencySymbol), /*#__PURE__*/React.createElement("span", {
    className: "price-minus"
  }, "-"), /*#__PURE__*/React.createElement("div", {
    className: "price-input-display ".concat(!deductionPrice ? 'placeholder' : '')
  }, deductionPrice || locales.getText('pisell2.customise-item.amount-placeholder')))))), /*#__PURE__*/React.createElement("div", {
    className: "customise-item-modal-footer"
  }, /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    size: "large",
    onClick: handleSave,
    disabled: disabled,
    className: "customise-item-save-btn"
  }, locales.getText('pisell2.customise-item.save'))))), /*#__PURE__*/React.createElement("div", {
    className: "customise-item-quantity-selector-fixed"
  }, /*#__PURE__*/React.createElement(PisellNumberSelector, {
    value: quantity,
    min: 1,
    max: 999,
    step: 1,
    disabled: disabled,
    onChange: function onChange(value) {
      if (value < 1) {
        setQuantity(1);
      } else {
        setQuantity(value);
      }
    },
    size: "middle",
    enablePriceAdjust: true
  }))), /*#__PURE__*/React.createElement(PisellPriceKeyboardModal, {
    visible: showPriceKeyboard,
    value: priceType === 'sell' ? sellPrice : deductionPrice,
    onChange: function onChange(val) {
      if (priceType === 'sell') {
        setSellPrice(String(val));
      } else {
        setDeductionPrice(String(val));
      }
    },
    onClose: function onClose() {
      return setShowPriceKeyboard(false);
    },
    triggerRef: currentTriggerRef,
    showCurrencySymbol: true,
    useThousandsSeparator: false
  }));
};
export default CustomiseItemModal;