"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _formiga = require("formiga");
var _inputPropTypes = require("../../props/inputPropTypes.cjs");
var _inputDefaultProps = require("../../props/inputDefaultProps.cjs");
var _FInputAddon = require("../../addon/FInputAddon.cjs");
var _isNotNumber = _interopRequireDefault(require("../../helpers/float/isNotNumber"));
var _useStepOrDecimals = _interopRequireDefault(require("../../helpers/props/useStepOrDecimals.cjs"));
var _FFSHidden = _interopRequireDefault(require("./FFSHidden.cjs"));
var _FFSInput = _interopRequireDefault(require("./FFSInput.cjs"));
var _FFSTotal = _interopRequireDefault(require("./FFSTotal.cjs"));
var _withWrapControlledForArray = _interopRequireDefault(require("./withWrapControlledForArray.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); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var _getInputSigns = value => {
  if (value == undefined || value.length == 0) {
    return [];
  }
  return value.map(v => (0, _isNotNumber.default)(v) ? true : v >= 0);
};
var _FInputFloatSum = props => {
  var {
    id,
    name,
    value,
    setValue,
    placeholder,
    readOnly,
    step,
    decimals,
    decimalSign,
    autocomplete,
    inputStyle,
    showValidity,
    bsSize
  } = props;
  var stepOrDecimals = (0, _useStepOrDecimals.default)(step, decimals);
  var input = (0, _formiga.useInput)(_objectSpread(_objectSpread({}, props), {}, {
    // TODO
    // wrap this checks (as we save array in the hidden input):
    //      min, max, stepOrDecimals,
    //checkValue: (v) => true

    // transformValue,
    // allowedValues, 
    // disallowedValues, 
    // doRepeat, 
    // doNotRepeat, 
    // feedback
    decimals: undefined,
    inputFilter: undefined
  }));
  var [inputSigns, setInputsSigns] = (0, _react.useState)(_getInputSigns(value));
  var [inputWithFocus, setInputWithFocus] = (0, _react.useState)(0);
  (0, _react.useEffect)(() => {
    input.setValue(value);
    input.validate();
  }, [input, value]);

  // Add a new input and focus it
  // innerRepr must be handled in paralell
  var handleAddValue = (0, _react.useCallback)(pos => {
    var nValue = [...value];
    nValue.push(0.0);
    var nSigns = [...inputSigns];
    nSigns.push(pos);
    setValue(nValue, false);
    setInputsSigns(nSigns);
    setInputWithFocus(nValue.length - 1);
  }, [value, setValue, inputSigns]);

  // Remove an input's value and its repr
  // innerRepr must be handled in paralell  
  var handleRemValue = (0, _react.useCallback)(iIndex => {
    var nValue = value != undefined ? [...value] : [0.0];
    nValue.splice(iIndex, 1);
    setValue(nValue, true);
    var nSigns = [...inputSigns];
    nSigns.splice(iIndex, 1);
    setInputsSigns(nSigns);
    setInputWithFocus(Math.max(iIndex - 1, 0));
  }, [value, setValue, inputSigns]);

  // Update an input's value and its repr
  // innerRepr must be handled in paralell
  var handleChange = (0, _react.useCallback)((tValue, confirmed, iIndex) => {
    var nValue = [...value];
    if (nValue.length > iIndex) {
      nValue[iIndex] = tValue;
    } else {
      nValue.push(tValue);
    }
    setValue(nValue, confirmed);
  }, [value, setValue]);
  return /*#__PURE__*/_react.default.createElement(_FInputAddon.FInputAddon, _extends({}, props, {
    valid: input.valid,
    invalid: !input.valid,
    feedback: input.feedback
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: "formiga-reactstrap-float-sum"
  }, /*#__PURE__*/_react.default.createElement(_FFSHidden.default, _extends({}, props, {
    inputRef: input.ref,
    defaultValue: value
  })), value.map((iValue, iIndex) => {
    var sign = inputSigns[iIndex];
    return /*#__PURE__*/_react.default.createElement("div", {
      className: "formiga-reactstrap-float-sum-box ".concat(sign ? 'positive' : 'negative'),
      key: "formiga-reactstrap-float-sum-".concat(id || name, "-repr-").concat(iIndex)
    }, /*#__PURE__*/_react.default.createElement(_FFSInput.default, {
      className: "formiga-reactstrap-float-sum-input",
      placeholder: placeholder || "",
      readOnly: readOnly != undefined ? readOnly : false,
      valid: input.valid,
      showValidity: showValidity,
      autocomplete: autocomplete,
      inputStyle: inputStyle,
      stepOrDecimals: stepOrDecimals,
      focusIt: inputWithFocus == iIndex,
      bsSize: bsSize,
      value: iValue,
      sign: sign,
      decimalSign: decimalSign,
      onChange: (v, c) => handleChange(v, c, iIndex),
      onAddValue: pos => handleAddValue(pos),
      onRemValue: () => handleRemValue(iIndex),
      isLastOne: iIndex == value.length - 1,
      removable: value.length > 1
    }));
  }), /*#__PURE__*/_react.default.createElement(_FFSTotal.default, {
    value: value,
    decimalSign: decimalSign
  })));
};
var FInputFloatSum = (0, _withWrapControlledForArray.default)(_FInputFloatSum);
FInputFloatSum.propTypes = _objectSpread(_objectSpread({}, _inputPropTypes.inputPropTypes), {}, {
  value: function value(props, _propName, _componentName) {
    if (!('value' in props) && !('defaultValue' in props)) {
      return new Error('Please provide a {value} or a {defaultValue}');
    }
  },
  defaultValue: _propTypes.default.arrayOf(_propTypes.default.number),
  placeholder: _propTypes.default.string,
  max: _propTypes.default.number,
  min: _propTypes.default.number,
  step: _propTypes.default.number,
  decimals: _propTypes.default.number,
  decimalSign: _propTypes.default.oneOf([',', '.']),
  autocomplete: _propTypes.default.oneOf(["on", "off"])
});
FInputFloatSum.defaultProps = _objectSpread(_objectSpread({}, _inputDefaultProps.inputDefaultProps), {}, {
  icon: "sigma",
  decimals: undefined,
  decimalSign: '.'
});
var _default = FInputFloatSum;
exports.default = _default;