import { useContext, useState, useRef, useEffect } from "react";
import Input from "../Input/input.js";
import { isObject, isUndefined, isFunction } from "../_util/is.js";
import { ConfigContext } from "../ConfigProvider/index.js";
import { jsxs, jsx, Fragment } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
function PageJumper(props) {
  var defaultInputText = props.simple ? props.current : void 0;
  var locale = useContext(ConfigContext).locale;
  var _a = __read(useState(defaultInputText), 2), inputText = _a[0], setInputText = _a[1];
  var inputRef = useRef();
  useEffect(function() {
    if (props.simple) {
      setInputText(props.current);
    }
  }, [props.simple, props.current]);
  var handleChange = function(val) {
    var value = parseInt(val, 10);
    setInputText(isNaN(value) ? void 0 : value);
  };
  var handleJump = function() {
    var onPageChange = props.onPageChange, totalPages2 = props.totalPages, current = props.current, simple2 = props.simple;
    if (isUndefined(inputText)) {
      return;
    }
    if (inputText === current && !simple2) {
      setInputText(void 0);
      return;
    }
    var page = isNaN(Number(inputText)) ? current : Number(inputText);
    if (page < 1) {
      page = 1;
    } else if (page > totalPages2) {
      page = totalPages2;
    }
    setInputText(simple2 ? page : void 0);
    isFunction(onPageChange) && onPageChange(page);
  };
  var onFocus = function() {
    var input = inputRef.current.dom;
    if (String(inputText) && input) {
      input.setSelectionRange(0, String(inputText).length);
    }
  };
  var rootPrefixCls = props.rootPrefixCls, totalPages = props.totalPages, simple = props.simple, size = props.size, disabled = props.disabled;
  var prefixCls = rootPrefixCls + "-jumper";
  var inputConfig = __assign({
    showJumper: true
  }, isObject(simple) ? simple : {});
  return /* @__PURE__ */ jsxs("div", {
    className: "" + prefixCls,
    children: [!simple && /* @__PURE__ */ jsx("span", {
      className: prefixCls + "-text-goto",
      children: locale.Pagination.goto
    }), inputConfig.showJumper ? /* @__PURE__ */ jsx(Input, {
      _ignorePropsFromGlobal: true,
      ref: function(ref) {
        return inputRef.current = ref;
      },
      className: prefixCls + "-input",
      value: !isUndefined(inputText) ? inputText.toString() : void 0,
      size,
      disabled: disabled || !totalPages,
      onChange: handleChange,
      onPressEnter: handleJump,
      onFocus,
      onBlur: handleJump
    }) : /* @__PURE__ */ jsx("span", {
      children: inputText
    }), simple && /* @__PURE__ */ jsxs(Fragment, {
      children: [/* @__PURE__ */ jsx("span", {
        className: prefixCls + "-separator",
        children: "/"
      }), /* @__PURE__ */ jsx("span", {
        children: totalPages
      })]
    })]
  });
}
export {
  PageJumper as default
};
//# sourceMappingURL=page-jumper.js.map
