"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _formiga = require("formiga");
var _reactstrap = require("reactstrap");
var _floatFilters = require("../../helpers/float/floatFilters.cjs");
var _floatConv = require("./floatConv.cjs");
var _roundFloat = _interopRequireDefault(require("../../helpers/float/roundFloat.cjs"));
var _countDecimals = _interopRequireDefault(require("../../helpers/float/countDecimals.cjs"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
var _startsWithTrashZero = s => {
  if (!s) {
    return false;
  }
  if (s.length < 2) {
    return false;
  }
  var firstIdx = s.slice(0, 1) == '-' ? 1 : 0;
  if (s.slice(firstIdx, firstIdx + 1) != '0') {
    return false;
  }
  var nextIdx = firstIdx + 1;
  if (isNaN(s.slice(nextIdx, nextIdx + 1))) {
    return false;
  }
  return true;
};
var FFSInput = _ref => {
  var {
    className,
    placeholder,
    readOnly,
    valid,
    autocomplete,
    inputStyle,
    stepOrDecimals,
    focusIt,
    showValidity,
    bsSize,
    sign,
    value,
    onChange,
    onAddValue,
    onRemValue,
    isLastOne,
    removable,
    decimalSign
  } = _ref;
  var reprRef = (0, _react.useRef)(undefined);
  (0, _formiga.useInputFilter)(reprRef === null || reprRef === void 0 ? void 0 : reprRef.current, _floatFilters.FLT_FLOAT_BOTH);
  var [innerRepr, setInnerRepr] = (0, _react.useState)((0, _floatConv.fromFloat)(value, decimalSign, sign));
  (0, _react.useEffect)(() => {
    if (focusIt) {
      reprRef.current.focus();
    }
  }, [focusIt]);
  var showValidProps = showValidity == 1 || showValidity == 4 ? {
    valid: valid,
    invalid: !valid
  } : {};
  var incrValue = (0, _react.useCallback)((factor, confirmed) => {
    var curValue = (0, _floatConv.toFloat)(value);
    var incr = stepOrDecimals != undefined ? parseFloat(stepOrDecimals) : 1.00;
    var nValue = (0, _roundFloat.default)(curValue + parseFloat(factor) * incr, (0, _countDecimals.default)(stepOrDecimals));
    var nRepr = (0, _floatConv.fromFloat)(nValue, decimalSign, sign);

    //console.log(`incrValue() current ${typeof curValue} ${curValue} + incr ${typeof incr} ${incr} => ${typeof nValue} ${nValue} (repr: ${typeof nRepr} ${nRepr})`)

    setInnerRepr(nRepr);
    onChange(nValue, confirmed);
  }, [value, onChange, stepOrDecimals, sign, decimalSign]);
  var getInnerReprFromEvent = (0, _react.useCallback)(event => {
    var nRepr = event.target.value;
    //console.log(`${innerRepr} ==> ${nRepr}`)
    if (nRepr != '' && innerRepr == '-0' && nRepr.indexOf('-') < 0) {
      nRepr = "-".concat(nRepr);
    }
    if (_startsWithTrashZero(nRepr)) {
      nRepr = nRepr.replace('0', '');
    }
    return nRepr;
  }, [innerRepr]);
  var handleChange = (0, _react.useCallback)(event => {
    var nRepr = getInnerReprFromEvent(event);
    setInnerRepr(nRepr);
    var nValue = (0, _floatConv.toFloat)(nRepr);
    onChange(nValue, false);
  }, [onChange, getInnerReprFromEvent]);
  var handleKeyDown = (0, _react.useCallback)(event => {
    // Simulate natives number input up/down
    if (event.key == 'ArrowUp' || event.key == 'ArrowDown') {
      event.preventDefault();
      var factor = event.key == 'ArrowUp' ? 1 : -1;
      incrValue(factor, false);
      return;
    }

    // Only allow to add new fields if we are in the last input
    if (event.key == '+' || event.key == '-') {
      if (isLastOne) {
        // // If value is positive and key is '-' and we are at the inputs beginning,
        // // means "change input's sign" instead of "add new"
        // let changeSign= false
        // if (event.key=='-' && event.target.value!='') {
        //   const f= parseFloat(event.target.value)
        //   if (f>0) {
        //     if (event.target.selectionStart==0) {
        //       changeSign= true
        //     }
        //   }
        // }
        // if (! changeSign) {
        var lastRepr = innerRepr.slice(-1)[0];
        if (lastRepr != '' && lastRepr != ',' && lastRepr != '.') {
          event.preventDefault();
          if (event.key == '+') {
            onAddValue(true);
          } else {
            onAddValue(false);
          }
        }
        //}
      }
    }

    if (event.key == 'Backspace' && event.target.value.length <= 1 && removable) {
      event.preventDefault();
      onRemValue();
    }
  }, [innerRepr, incrValue, isLastOne, onAddValue, onRemValue, removable]);
  var handleBlur = (0, _react.useCallback)(event => {
    var nRepr = getInnerReprFromEvent(event);
    var nValue = (0, _floatConv.toFloat)(nRepr);
    onChange(nValue, true);
  }, [onChange, getInnerReprFromEvent]);
  return /*#__PURE__*/_react.default.createElement(_reactstrap.Input, _extends({
    className: className,
    type: "text",
    innerRef: reprRef,
    placeholder: placeholder || "",
    readOnly: readOnly != undefined ? readOnly : false,
    autoComplete: autocomplete,
    style: inputStyle,
    size: Math.max(innerRepr.length || 0, 2),
    value: innerRepr,
    onChange: handleChange,
    onKeyDown: handleKeyDown,
    onBlur: handleBlur,
    bsSize: bsSize,
    step: stepOrDecimals
  }, showValidProps));
};
var _default = FFSInput;
exports.default = _default;