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(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
var import_constant = require("../utils/constant");
var import_utils = require("../utils");
var import_use_update_effect = __toESM(require("../hooks/use-update-effect"));
const DEFAULT_CELL_LENGTH = 6;
const PasswordInput = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("password-input", prefixCls);
  const rootRef = (0, import_react.useRef)(null);
  const nativeInputRef = (0, import_react.useRef)(null);
  const [inputValue, setInputValue] = (0, import_react.useState)(props.value);
  const {
    length,
    value,
    focused
  } = props;
  const info = props.errorInfo || props.info;
  const cellLength = length > 0 && length < Infinity ? Math.floor(length) : DEFAULT_CELL_LENGTH;
  (0, import_use_update_effect.default)(() => {
    if (["", void 0].includes(value)) {
      setInputValue("");
    }
    setInputValue(value.slice(0, cellLength));
  }, [value, cellLength]);
  (0, import_react.useEffect)(() => {
    var _a, _b;
    if (inputValue.length >= cellLength) {
      (_a = props.onFill) == null ? void 0 : _a.call(props, inputValue);
    }
    (_b = props.onChange) == null ? void 0 : _b.call(props, inputValue.slice(0, cellLength));
  }, [props.onChange, props.onFill, inputValue, cellLength]);
  const onFocus = () => {
    var _a, _b;
    if (!props.keyboard) {
      (_a = nativeInputRef.current) == null ? void 0 : _a.focus();
    }
    (_b = props.onFocus) == null ? void 0 : _b.call(props);
  };
  const onBlur = () => {
    var _a;
    (_a = props.onBlur) == null ? void 0 : _a.call(props);
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    focus: () => onFocus(),
    blur: () => {
      var _a;
      onBlur();
      (_a = nativeInputRef.current) == null ? void 0 : _a.blur();
    },
    resetValue: () => {
      setInputValue("");
    }
  }));
  const onTouchStart = (event) => {
    var _a;
    event.stopPropagation();
    (_a = props.onFocus) == null ? void 0 : _a.call(props);
  };
  const renderPoints = () => {
    const Points = [];
    const {
      mask,
      gutter
    } = props;
    for (let i = 0; i < length; i++) {
      const char = inputValue[i];
      const showBorder = i !== 0 && !gutter;
      const showCursor = focused && i === inputValue.length;
      let style;
      if (i !== 0 && gutter) {
        style = {
          marginLeft: (0, import_utils.addUnit)(gutter)
        };
      }
      Points.push(/* @__PURE__ */ import_react.default.createElement("li", {
        key: i,
        className: (0, import_classnames.default)({
          [import_constant.BORDER_LEFT]: showBorder
        }, bem("item", {
          focus: showCursor
        })),
        style
      }, mask ? /* @__PURE__ */ import_react.default.createElement("i", {
        style: {
          visibility: char ? "visible" : "hidden"
        }
      }) : char, showCursor && /* @__PURE__ */ import_react.default.createElement("div", {
        className: (0, import_classnames.default)(bem("cursor"))
      })));
    }
    return Points;
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement("div", {
    ref: rootRef,
    tabIndex: 0,
    onFocus,
    onBlur,
    className: (0, import_classnames.default)(bem())
  }, /* @__PURE__ */ import_react.default.createElement("ul", {
    className: (0, import_classnames.default)(bem("security"), {
      [import_constant.BORDER_SURROUND]: !props.gutter
    }),
    onTouchStart
  }, renderPoints()), info && /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem(props.errorInfo ? "error-info" : "info"))
  }, info), /* @__PURE__ */ import_react.default.createElement("input", {
    ref: nativeInputRef,
    className: (0, import_classnames.default)(bem("native-input")),
    value: inputValue,
    type: "text",
    pattern: "[0-9]*",
    inputMode: "numeric",
    onChange: (e) => {
      setInputValue(e.target.value.slice(0, props.length));
    }
  })), props.keyboard && /* @__PURE__ */ import_react.default.cloneElement(props.keyboard, {
    visible: focused,
    onInput: (v) => {
      if (inputValue.length < cellLength) {
        setInputValue((inputValue + v).slice(0, props.length));
      }
    },
    onDelete: () => {
      setInputValue(inputValue.slice(0, -1));
    },
    onClose: () => {
      var _a;
      (_a = rootRef.current) == null ? void 0 : _a.blur();
    },
    onBlur: () => {
      var _a;
      (_a = rootRef.current) == null ? void 0 : _a.blur();
    }
  }));
});
PasswordInput.defaultProps = {
  value: "",
  info: null,
  errorInfo: null,
  length: DEFAULT_CELL_LENGTH,
  gutter: 0,
  mask: true,
  focused: false,
  onChange: () => {
  }
};
var stdin_default = PasswordInput;
