"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PasscodeInput = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _bound = require("../../utils/bound");
var _nativeProps = require("../../utils/native-props");
var _usePropsValue = require("../../utils/use-props-value");
var _withDefaultProps = require("../../utils/with-default-props");
var _configProvider = require("../config-provider");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const classPrefix = 'adm-passcode-input';
const defaultProps = {
  defaultValue: '',
  length: 6,
  plain: false,
  error: false,
  seperated: false,
  caret: true,
  inputMode: 'numeric'
};
const PasscodeInput = exports.PasscodeInput = (0, _react.forwardRef)((p, ref) => {
  const props = (0, _withDefaultProps.mergeProps)(defaultProps, p);
  // 防止 length 值不合法
  const cellLength = props.length > 0 && props.length < Infinity ? Math.floor(props.length) : defaultProps.length;
  const {
    locale
  } = (0, _configProvider.useConfig)();
  const [focused, setFocused] = (0, _react.useState)(false);
  const [value, setValue] = (0, _usePropsValue.usePropsValue)(props);
  const rootRef = (0, _react.useRef)(null);
  const nativeInputRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    var _a;
    if (value.length >= cellLength) {
      (_a = props.onFill) === null || _a === void 0 ? void 0 : _a.call(props, value);
    }
  }, [value, cellLength]);
  const onFocus = () => {
    var _a, _b;
    if (!props.keyboard) {
      (_a = nativeInputRef.current) === null || _a === void 0 ? void 0 : _a.focus();
    }
    setFocused(true);
    (_b = props.onFocus) === null || _b === void 0 ? void 0 : _b.call(props);
  };
  (0, _react.useEffect)(() => {
    if (!focused) return;
    const timeout = window.setTimeout(() => {
      var _a;
      (_a = rootRef.current) === null || _a === void 0 ? void 0 : _a.scrollIntoView({
        block: 'center',
        inline: 'center',
        behavior: 'smooth'
      });
    }, 100);
    return () => {
      window.clearTimeout(timeout);
    };
  }, [focused]);
  const onBlur = () => {
    var _a;
    setFocused(false);
    (_a = props.onBlur) === null || _a === void 0 ? void 0 : _a.call(props);
  };
  (0, _react.useImperativeHandle)(ref, () => ({
    focus: () => {
      var _a;
      return (_a = rootRef.current) === null || _a === void 0 ? void 0 : _a.focus();
    },
    blur: () => {
      var _a, _b;
      (_a = rootRef.current) === null || _a === void 0 ? void 0 : _a.blur();
      (_b = nativeInputRef.current) === null || _b === void 0 ? void 0 : _b.blur();
    }
  }));
  const renderCells = () => {
    const cells = [];
    const chars = value.split('');
    const caretIndex = chars.length; // 光标位置index等于当前文字长度
    const focusedIndex = (0, _bound.bound)(chars.length, 0, cellLength - 1);
    for (let i = 0; i < cellLength; i++) {
      cells.push(_react.default.createElement("div", {
        className: (0, _classnames.default)(`${classPrefix}-cell`, {
          [`${classPrefix}-cell-caret`]: props.caret && caretIndex === i && focused,
          [`${classPrefix}-cell-focused`]: focusedIndex === i && focused,
          [`${classPrefix}-cell-dot`]: !props.plain && chars[i]
        }),
        key: i
      }, chars[i] && props.plain ? chars[i] : ''));
    }
    return cells;
  };
  const cls = (0, _classnames.default)(classPrefix, {
    [`${classPrefix}-focused`]: focused,
    [`${classPrefix}-error`]: props.error,
    [`${classPrefix}-seperated`]: props.seperated
  });
  return _react.default.createElement(_react.default.Fragment, null, (0, _nativeProps.withNativeProps)(props, _react.default.createElement("div", {
    ref: rootRef,
    tabIndex: 0,
    className: cls,
    onFocus: onFocus,
    onBlur: onBlur,
    role: 'button',
    "aria-label": locale.PasscodeInput.name
  }, _react.default.createElement("div", {
    className: `${classPrefix}-cell-container`
  }, renderCells()), _react.default.createElement("input", {
    ref: nativeInputRef,
    className: `${classPrefix}-native-input`,
    value: value,
    type: 'text',
    pattern: '[0-9]*',
    inputMode: props.inputMode,
    onChange: e => {
      setValue(e.target.value.slice(0, props.length));
    },
    "aria-hidden": true
  }))), props.keyboard && _react.default.cloneElement(props.keyboard, {
    visible: focused,
    onInput: v => {
      if (value.length < cellLength) {
        setValue((value + v).slice(0, props.length));
      }
    },
    onDelete: () => {
      setValue(value.slice(0, -1));
    },
    onClose: () => {
      var _a;
      (_a = rootRef.current) === null || _a === void 0 ? void 0 : _a.blur();
    }
  }));
});