"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const react = require("@emotion/react");
const React = require("react");
const jsxRuntime = require("@emotion/react/jsx-runtime");
const style = require("./style.js");
const string = require("../../utils/string.js");
const Input = function({
  value,
  placeholder,
  type = "text",
  min = 0,
  readOnly = false,
  onChange,
  onKeyDown,
  onBlur,
  onClick,
  rootCss,
  inputCss,
  className,
  outerSkin,
  inputSkin,
  canBeEmpty = true,
  pattern,
  decimalPlaces,
  positive = false,
  warning
}) {
  const reg = pattern ? new RegExp(pattern) : void 0;
  const [currentValue, setCurrentValue] = React.useState(value || "");
  const [regCheck, setRegCheck] = React.useState(false);
  const validKey = React.useRef(true);
  React.useEffect(() => {
    if (value === void 0)
      return;
    onChange == null ? void 0 : onChange(value || "");
    setCurrentValue(value || "");
  }, [onChange, value]);
  React.useEffect(() => {
    if (reg) {
      setRegCheck(!!currentValue);
      if (reg) {
        setRegCheck(reg.test(currentValue));
      }
      setCurrentValue(currentValue);
      onChange == null ? void 0 : onChange(currentValue);
    }
  }, [reg]);
  const handleOnChange = (event) => {
    var _a;
    if (type === "number" && event.currentTarget.value !== "") {
      event.currentTarget.value = string.checkUniqueChar(
        ((_a = event.currentTarget.value.match(/[0-9|.]+/)) == null ? void 0 : _a[0]) || currentValue,
        "."
      );
      if (decimalPlaces !== void 0 && !string.decimalPlacesLimit(event.currentTarget.value, decimalPlaces)) {
        event.currentTarget.value = currentValue;
      }
    }
    if (reg) {
      setRegCheck(reg.test(event.currentTarget.value));
    }
    setCurrentValue(event.currentTarget.value);
    onChange == null ? void 0 : onChange(event.currentTarget.value);
  };
  const handleOnKeyDown = (event) => {
    if (type === "number" && event.key === "+" || event.key === "-" || event.key === "e" || event.key === "." && decimalPlaces === 0) {
      validKey.current = false;
    } else {
      validKey.current = true;
      onKeyDown == null ? void 0 : onKeyDown(event.currentTarget.value);
    }
  };
  const handleOnInput = (event) => {
    if (type === "number" && !validKey.current) {
      event.currentTarget.value = currentValue;
    }
  };
  let extraInputCss = "";
  {
    const warningStyle = "border: 1px solid #ff4d4f;";
    if (inputSkin === "data" && (!canBeEmpty && currentValue === "" || reg && !regCheck || positive && !(parseFloat(currentValue) > 0))) {
      extraInputCss = warningStyle;
    }
    if (warning !== void 0 && warning) {
      extraInputCss = warningStyle;
    }
  }
  return /* @__PURE__ */ jsxRuntime.jsx(
    style.InputContainer,
    {
      outerSkin,
      inputSkin,
      rootCss,
      inputCss: react.css`
        ${inputCss}
        ${extraInputCss}
      `,
      className,
      children: /* @__PURE__ */ jsxRuntime.jsx(
        "input",
        {
          value: currentValue,
          placeholder,
          type,
          min,
          readOnly,
          onChange: handleOnChange,
          onKeyDown: handleOnKeyDown,
          onInput: handleOnInput,
          onBlur: (event) => onBlur == null ? void 0 : onBlur(event.currentTarget.value),
          onClick: (event) => onClick == null ? void 0 : onClick(event)
        }
      )
    }
  );
};
exports.Input = Input;
//# sourceMappingURL=index.js.map
