var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/pro/priceKeyboard/hooks/keyActions.ts
var keyActions_exports = {};
__export(keyActions_exports, {
  default: () => keyActions_default
});
module.exports = __toCommonJS(keyActions_exports);
var import_react = require("react");
var import_utils = require("@pisell/utils");
var useKeyActions = (params) => {
  const [error, setError] = (0, import_react.useState)(null);
  const { onChange, onEnter, onReset, value = "", min, max, mode = "append", scale } = params;
  const checkScale = (0, import_react.useCallback)(
    (val) => {
      if (scale === void 0) return true;
      const decimalIndex = val.indexOf(".");
      if (decimalIndex === -1) return true;
      const decimalPart = val.substring(decimalIndex + 1);
      return decimalPart.length <= scale;
    },
    [scale]
  );
  const validateRange = (0, import_react.useCallback)(
    (val) => {
      const trimmedVal = val.trim();
      const numVal = Number(trimmedVal);
      if (isNaN(numVal)) {
        const errorMsg = "invalid";
        setError({ msg: errorMsg });
        return { isValid: false, errorMsg };
      }
      if (min !== void 0 && numVal < min) {
        const errorMsg = "invalid";
        setError({ msg: errorMsg });
        return { isValid: false, errorMsg };
      }
      if (max !== void 0 && numVal > max) {
        const errorMsg = "invalid";
        setError({ msg: errorMsg });
        return { isValid: false, errorMsg };
      }
      setError(null);
      return { isValid: true };
    },
    [min, max]
  );
  const handleChangeValue = (0, import_react.useCallback)(
    (val) => {
      if (!checkScale(val)) {
        return { isValid: false, errorMsg: "decimal_places_exceeded" };
      }
      const validation = validateRange(val);
      if (validation.isValid) {
        onChange == null ? void 0 : onChange(val);
      }
      return validation;
    },
    [onChange, validateRange, checkScale]
  );
  const handleNumberInput = (0, import_react.useCallback)(
    (inputVal) => {
      const numStr = String(inputVal);
      if (mode === "replace") {
        return handleChangeValue(numStr);
      }
      if (!value) {
        return handleChangeValue(numStr);
      }
      if (/^0+$/.test(value)) {
        if (numStr === "0") {
          return handleChangeValue(value + "0");
        } else {
          return handleChangeValue(numStr);
        }
      }
      if (/^0\.\d*$/.test(value)) {
        return handleChangeValue(value + numStr);
      }
      if (/^0+\d+$/.test(value)) {
        const withoutLeadingZeros = value.replace(/^0+/, "");
        return handleChangeValue(withoutLeadingZeros + numStr);
      }
      return handleChangeValue(value + numStr);
    },
    [mode, value, handleChangeValue]
  );
  const handleDecimalInput = (0, import_react.useCallback)(() => {
    if (mode === "replace") {
      return handleChangeValue("0.");
    }
    if (value.includes(".")) {
      return;
    }
    if (!value) {
      return handleChangeValue("0.");
    }
    if (/^0+$/.test(value)) {
      return handleChangeValue("0.");
    }
    if (/^0+\d+$/.test(value)) {
      const withoutLeadingZeros = value.replace(/^0+/, "");
      return handleChangeValue(withoutLeadingZeros + ".");
    }
    return handleChangeValue(value + ".");
  }, [mode, value, handleChangeValue]);
  const handleOtherInput = (0, import_react.useCallback)(
    (inputVal) => {
      const inputStr = String(inputVal);
      if (mode === "replace") {
        return handleChangeValue(inputStr);
      }
      return handleChangeValue(value + inputStr);
    },
    [mode, value, handleChangeValue]
  );
  const handleKeyInput = (0, import_react.useCallback)(
    (val) => {
      switch (val) {
        case void 0:
          break;
        case "ok":
        case "enter":
        case "done":
          const validation = validateRange(value);
          if (validation.isValid) {
            onEnter == null ? void 0 : onEnter(value);
          }
          break;
        case "reset":
          setError(null);
          onReset == null ? void 0 : onReset();
          break;
        case "clear":
          setError(null);
          handleChangeValue("");
          break;
        case "delete":
          handleChangeValue(value.slice(0, -1));
          break;
        case ".":
          handleDecimalInput();
          break;
        default:
          if ((0, import_utils.isNumber)(val) || String(val) === "0") {
            handleNumberInput(val);
          } else {
            handleOtherInput(val);
          }
          break;
      }
    },
    [value, validateRange, onEnter, onReset, handleChangeValue, handleDecimalInput, handleNumberInput, handleOtherInput]
  );
  return {
    handleChange: handleKeyInput,
    error
  };
};
var keyActions_default = useKeyActions;
