function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 } from 'react';
import { Radio } from 'antd';
import { VirtualKeyboard } from '@pisell/materials';
import classNames from 'classnames';
import "./index.less";
var PisellPriceRadio = function PisellPriceRadio(_ref) {
  var _ref$checked = _ref.checked,
    checked = _ref$checked === void 0 ? false : _ref$checked,
    onSelect = _ref.onSelect,
    _ref$value = _ref.value,
    value = _ref$value === void 0 ? '' : _ref$value,
    onChange = _ref.onChange,
    _ref$title = _ref.title,
    title = _ref$title === void 0 ? '' : _ref$title,
    _ref$currencySymbol = _ref.currencySymbol,
    currencySymbol = _ref$currencySymbol === void 0 ? '$' : _ref$currencySymbol,
    _ref$theme = _ref.theme,
    theme = _ref$theme === void 0 ? 'purple' : _ref$theme,
    _ref$showMinus = _ref.showMinus,
    showMinus = _ref$showMinus === void 0 ? false : _ref$showMinus,
    _ref$disabled = _ref.disabled,
    disabled = _ref$disabled === void 0 ? false : _ref$disabled,
    _ref$placeholder = _ref.placeholder,
    placeholder = _ref$placeholder === void 0 ? 'Amount' : _ref$placeholder;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    showKeyboard = _useState2[0],
    setShowKeyboard = _useState2[1];

  // 当选中状态改变时，自动显示/隐藏键盘
  useEffect(function () {
    if (checked) {
      setShowKeyboard(true);
    } else {
      setShowKeyboard(false);
    }
  }, [checked]);

  // 处理输入框点击
  var handleInputClick = function handleInputClick() {
    if (!disabled) {
      onSelect === null || onSelect === void 0 || onSelect();
      setShowKeyboard(true);
    }
  };

  // 处理Radio点击
  var handleRadioClick = function handleRadioClick() {
    if (!disabled) {
      onSelect === null || onSelect === void 0 || onSelect();
    }
  };

  // 处理价格输入变化
  var handlePriceChange = function handlePriceChange(newValue) {
    onChange === null || onChange === void 0 || onChange(newValue);
  };

  // 自定义输入框渲染
  var renderInput = function renderInput(props) {
    var displayValue = showMinus && value ? "-".concat(value) : value;
    return /*#__PURE__*/React.createElement("button", {
      type: "button",
      className: classNames('pisell-price-radio-input-container', _defineProperty(_defineProperty({}, "pisell-price-radio-input-container-".concat(theme), checked), 'pisell-price-radio-input-container-disabled', disabled)),
      onClick: handleInputClick,
      disabled: disabled
    }, /*#__PURE__*/React.createElement("span", {
      className: "pisell-price-radio-currency"
    }, currencySymbol), /*#__PURE__*/React.createElement("div", {
      className: "pisell-price-radio-input-content"
    }, displayValue || /*#__PURE__*/React.createElement("span", {
      className: "pisell-price-radio-placeholder"
    }, showMinus ? '-' : '', placeholder)));
  };
  return /*#__PURE__*/React.createElement("div", {
    className: classNames('pisell-price-radio', _defineProperty(_defineProperty(_defineProperty({}, "pisell-price-radio-".concat(theme), checked), 'pisell-price-radio-checked', checked), 'pisell-price-radio-disabled', disabled))
  }, /*#__PURE__*/React.createElement("div", {
    className: "pisell-price-radio-header"
  }, /*#__PURE__*/React.createElement(Radio, {
    checked: checked,
    onChange: handleRadioClick,
    disabled: disabled,
    className: classNames('pisell-price-radio-button', _defineProperty({}, "pisell-price-radio-button-".concat(theme), checked))
  }), /*#__PURE__*/React.createElement("span", {
    className: "pisell-price-radio-title"
  }, title)), /*#__PURE__*/React.createElement(VirtualKeyboard, {
    value: value,
    onChange: function onChange(e) {
      return handlePriceChange(e.target.value);
    },
    renderInput: renderInput,
    showDelete: false,
    onValueSelect: function onValueSelect(selected) {
      if (selected && !checked) {
        onSelect === null || onSelect === void 0 || onSelect();
      }
    },
    defaultSelect: checked && showKeyboard,
    placeholder: placeholder,
    disabled: disabled
  }));
};
export default PisellPriceRadio;