var _excluded = ["onChange", "value"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useEffect, useRef, useState } from 'react';
import Input from "../../../input";
import { bankTypeMap, getCardType } from "./utils";
import "./index.less";
var bankCardMaxLength = function bankCardMaxLength(str) {
  return str.slice(0, 19);
};
var BankCard = function BankCard(props) {
  var onChange = props.onChange,
    propsValue = props.value,
    others = _objectWithoutProperties(props, _excluded);
  var inputRef = useRef(null);
  var _useState = useState(propsValue || ''),
    _useState2 = _slicedToArray(_useState, 2),
    _value = _useState2[0],
    setValue = _useState2[1];
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    currentBankType = _useState4[0],
    setCurrentBankType = _useState4[1];

  // const [state, setState] = useControllableValue<string>(props, {
  //   defaultValue: '',
  // });

  useEffect(function () {
    document.body.id = 'body';
  }, []);
  useEffect(function () {
    setValue(bankCardMaxLength(formatBankCardNumber("".concat(propsValue || ''))));
  }, [propsValue]);
  var formatBankCardNumber = function formatBankCardNumber(value) {
    var cleanValue = value.replace(/\D/g, '');
    var formattedValue = cleanValue.replace(/(.{4})/g, '$1 ').trim();
    return formattedValue;
  };
  var handleChange = function handleChange(e) {
    var _inputRef$current, _inputRef$current$inp;
    var value = e.target.value;
    setCurrentBankType(getCardType(value));
    var formattedValue = formatBankCardNumber(value);
    var start = ((_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : (_inputRef$current$inp = _inputRef$current.input) === null || _inputRef$current$inp === void 0 ? void 0 : _inputRef$current$inp.selectionStart) || 0;
    var val = bankCardMaxLength(formattedValue);
    setValue(val);
    onChange === null || onChange === void 0 ? void 0 : onChange(val.replace(/\s/g, ''));
    if (formattedValue.length > 19 || formattedValue.length < _value.length) {
      if ([4, 9, 14, 19].includes(start)) {
        start = start + 1;
      }
      setTimeout(function () {
        var _inputRef$current2;
        (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 ? void 0 : _inputRef$current2.setSelectionRange(start, start);
      });
    }
  };
  return /*#__PURE__*/React.createElement(Input, _extends({}, others, {
    suffix: currentBankType ? /*#__PURE__*/React.createElement("img", {
      className: "pisell-input-bank-card-suffix",
      src: bankTypeMap[currentBankType],
      alt: currentBankType
    }) : /*#__PURE__*/React.createElement("span", null)
    // 数字类型时value无法带有空格
    ,
    type: "tel",
    placeholder: '1234 1234 1234 1234',
    size: "large",
    ref: inputRef,
    value: _value,
    onChange: handleChange
  }));
};
export default BankCard;