var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/pisellInput/components/BankCard/index.tsx
var BankCard_exports = {};
__export(BankCard_exports, {
  default: () => BankCard_default
});
module.exports = __toCommonJS(BankCard_exports);
var import_react = __toESM(require("react"));
var import_input = __toESM(require("../../../input"));
var import_utils = require("./utils");
var import_index = require("./index.less");
var bankCardMaxLength = (str) => {
  return str.slice(0, 19);
};
var BankCard = (props) => {
  const { onChange, value: propsValue, ...others } = props;
  const inputRef = (0, import_react.useRef)(null);
  const [_value, setValue] = (0, import_react.useState)(propsValue || "");
  const [currentBankType, setCurrentBankType] = (0, import_react.useState)();
  (0, import_react.useEffect)(() => {
    document.body.id = "body";
  }, []);
  (0, import_react.useEffect)(() => {
    setValue(bankCardMaxLength(formatBankCardNumber(`${propsValue || ""}`)));
  }, [propsValue]);
  const formatBankCardNumber = (value) => {
    const cleanValue = value.replace(/\D/g, "");
    const formattedValue = cleanValue.replace(/(.{4})/g, "$1 ").trim();
    return formattedValue;
  };
  const handleChange = (e) => {
    var _a, _b;
    const { value } = e.target;
    setCurrentBankType((0, import_utils.getCardType)(value));
    const formattedValue = formatBankCardNumber(value);
    let start = ((_b = (_a = inputRef.current) == null ? void 0 : _a.input) == null ? void 0 : _b.selectionStart) || 0;
    const val = bankCardMaxLength(formattedValue);
    setValue(val);
    onChange == null ? 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(() => {
        var _a2;
        (_a2 = inputRef.current) == null ? void 0 : _a2.setSelectionRange(start, start);
      });
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_input.default,
    {
      ...others,
      suffix: currentBankType ? /* @__PURE__ */ import_react.default.createElement(
        "img",
        {
          className: "pisell-input-bank-card-suffix",
          src: import_utils.bankTypeMap[currentBankType],
          alt: currentBankType
        }
      ) : /* @__PURE__ */ import_react.default.createElement("span", null),
      type: "tel",
      placeholder: "1234 1234 1234 1234",
      size: "large",
      ref: inputRef,
      value: _value,
      onChange: handleChange
    }
  );
};
var BankCard_default = BankCard;
