"use strict";

exports.__esModule = true;
exports.useStyles = exports.default = exports.AdvancedNumericInput = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styles = require("@material-ui/core/styles");
var _InputBase = _interopRequireDefault(require("@material-ui/core/InputBase"));
var _InputBaseProps = _interopRequireWildcard(require("./InputBaseProps"));
var _classnames = _interopRequireDefault(require("classnames"));
var _reactNumberFormat = require("react-number-format");
var _inputHelper = require("../../../utils/inputHelper");
var _excluded = ["inputRef", "onChange"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var useStyles = exports.useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    container: {
      display: "flex",
      flexDirection: "column"
    },
    inputContainer: {
      display: "flex"
    },
    prepend: {
      fontSize: theme.spacing(1.3),
      lineHeight: theme.spacing(1.3),
      padding: theme.spacing(0.85),
      minWidth: theme.spacing(6),
      marginRight: theme.spacing(-0.1),
      height: theme.spacing(3),
      display: "inline-flex",
      borderRadius: theme.spacing(0.5, 0, 0, 0.5),
      border: "".concat(theme.spacing(0.1), " solid ").concat(theme.palette.grey.borders),
      boxSizing: "border-box",
      backgroundColor: theme.palette.grey.light,
      whiteSpace: "nowrap"
    },
    disabledPrepend: {
      border: 0,
      padding: theme.spacing(0.8),
      height: theme.spacing(2.8),
      backgroundColor: theme.palette.grey.borders
    },
    controlInput: {
      minHeight: theme.spacing(1.6),
      maxWidth: "unset",
      minWidth: 0,
      border: "".concat(theme.spacing(0.1), " solid ").concat(theme.palette.grey.borders),
      borderRadius: function borderRadius(props) {
        return props.label ? theme.spacing(0, 0.5, 0.5, 0) : theme.spacing(0.5);
      },
      paddingLeft: theme.spacing(0.85),
      "&:focus, &:active": {
        border: "".concat(theme.spacing(0.1), " solid ").concat(theme.palette.focus),
        borderRadius: function borderRadius(props) {
          return props.label ? theme.spacing(0, 0.5, 0.5, 0) : theme.spacing(0.5);
        }
      }
    },
    errorInput: {
      "& input, & textarea": {
        border: "".concat(theme.spacing(0.1), " solid ").concat(theme.palette.error.main),
        "&:focus, &:active": {
          border: "".concat(theme.spacing(0.1), " solid ").concat(theme.palette.error.main),
          boxShadow: "".concat(theme.spacing(0, 0, 0.4), " ").concat(theme.palette.error.main)
        }
      }
    },
    errorText: {
      marginTop: theme.spacing(0.5),
      color: theme.palette.error.main,
      fontSize: theme.typography.fieldLabelSize,
      float: function float(props) {
        return props.errorPosition === "right" ? "right" : "left";
      }
    },
    disabled: {
      "& input, & textarea": {
        color: theme.palette.text.primary,
        backgroundColor: theme.palette.grey.light,
        border: 0,
        "&:focus, &:active": {
          border: 0,
          boxShadow: 0
        }
      }
    },
    multiline: {
      padding: 0
    },
    inputMultiline: {
      padding: theme.spacing(0.6, 0.6, 0.6, 0.85)
    }
  };
});
var AdvancedNumericInput = exports.AdvancedNumericInput = function AdvancedNumericInput(props) {
  var inputRef = props.inputRef,
    onChange = props.onChange,
    other = _objectWithoutProperties(props, _excluded);

  // https://github.com/s-yadav/react-number-format

  return /*#__PURE__*/_react.default.createElement(_reactNumberFormat.NumericFormat, _extends({}, other, {
    getInputRef: inputRef,
    onValueChange: function onValueChange(values) {
      onChange({
        target: {
          name: props.name,
          value: values.value
        }
      });
    }
  }));
};
var InputBase = function InputBase(_ref) {
  var _inputProps$get;
  var inputProps = _ref.inputProps;
  if ((0, _InputBaseProps.isInputProps)(inputProps) === false) {
    throw new TypeError("inputProps property is not of type InputBaseProps");
  }
  var multiline = (inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.multiline)) || false;
  var update = inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.update);
  var value = (_inputProps$get = inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.value)) != null ? _inputProps$get : "";
  var label = !multiline && (inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.label));
  var type = (inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.type)) || "text";
  var inputAttributes = (inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.inputAttributes)) || {};
  var placeholder = inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.placeholder);
  var error = inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.error);
  var errorPosition = inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.errorPosition);
  var disabled = (inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.disabled)) || false;
  var onBlur = (inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.onBlur)) || null;
  var startAdornment = inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.startAdornment);
  var endAdornment = inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.endAdornment);
  var metadata = inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.metadata);
  var autoComplete = inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.autoComplete);
  var timeoutDelay = (inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.timeoutDelay)) || 100;
  var rowsProps = inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.rows);
  var numericInputProps = (inputProps == null ? void 0 : inputProps.get(_InputBaseProps.default.propNames.numericInputProps)) || null;
  var isAdvancedNumericInput = type.toLowerCase() === "advancednumericinput";
  var inputComponent = isAdvancedNumericInput ? AdvancedNumericInput : undefined;
  var inputControlType = isAdvancedNumericInput ? "text" : type;
  if (isAdvancedNumericInput && numericInputProps) {
    Object.keys(numericInputProps).forEach(function (key) {
      if (key !== "blurFormattingSkipFixedDecimalScale") {
        inputAttributes[key] = numericInputProps[key];
      }
    });
  }
  if (isAdvancedNumericInput) {
    var _numericInputProps$de;
    if (inputAttributes.max === undefined) {
      inputAttributes.max = 2147483647;
    }
    if (inputAttributes.min === undefined) {
      inputAttributes.min = -2147483648;
    }
    var decimalScale = (_numericInputProps$de = numericInputProps == null ? void 0 : numericInputProps.decimalScale) != null ? _numericInputProps$de : 0;
    var lengthForMin = Math.trunc(inputAttributes.min).toString().length;
    var lengthForMax = Math.trunc(inputAttributes.max).toString().length;
    inputAttributes.maxLength = Math.max(lengthForMin, lengthForMax) + (decimalScale > 0 ? decimalScale + 1 : 0);
    inputAttributes.isAllowed = function (val) {
      return val.value === "" || val.value === "-" || val.value !== null;
    };
  }
  var defaultRows = 4;
  var rows = rowsProps;
  if (rows === null || rows === undefined) rows = defaultRows;
  var tooltipText = type === "text" ? value : "";
  var onClick = function onClick(item) {
    // Fixes FireFox issue, where the input number buttons do not focus on input control,
    // causing onBlur to never fire
    item.target.focus();
  };
  var classes = useStyles({
    label: label,
    errorPosition: errorPosition
  });
  var onChangeHandler = function onChangeHandler(event) {
    if (event.persist) {
      event.persist();
    }
    if (!event.target.value || window.bypassDebounce === true) {
      update(event.target.value, metadata);
    }
    setInputText(event.target.value);
  };
  var timerId = (0, _react.useRef)(null);
  _react.default.useEffect(function () {
    return function () {
      clearTimeout(timerId.current);
      timerId.current = null;
    };
  }, []);
  var onBlurInternal = function onBlurInternal(e) {
    var updateValue = (inputText != null ? inputText : value).toString(); // value cannot be null as defined above

    if (timerId.current) {
      clearTimeout(timerId.current);
      timerId.current = null;
    }
    if (isAdvancedNumericInput) {
      var _numericInputProps$de2;
      var formattingProps = _objectSpread({}, numericInputProps);
      delete formattingProps.blurFormattingSkipFixedDecimalScale;
      if ((numericInputProps == null ? void 0 : numericInputProps.blurFormattingSkipFixedDecimalScale) !== true) {
        formattingProps.fixedDecimalScale = true;
      }
      updateValue = (0, _inputHelper.trimSpacesAndLeadingZeros)(updateValue, (_numericInputProps$de2 = numericInputProps == null ? void 0 : numericInputProps.defaultValue) != null ? _numericInputProps$de2 : "");
      updateValue = (0, _reactNumberFormat.numericFormatter)(updateValue, formattingProps);
    }
    update(updateValue, metadata);
    setInputText(null);
    if (onBlur) {
      onBlur(e);
    }
  };
  var inputBaseInputStyle = inputProps == null ? void 0 : inputProps.getStyle(_InputBaseProps.default.ruleNames.input);
  var errorTextStyle = inputProps == null ? void 0 : inputProps.getStyle(_InputBaseProps.default.ruleNames.errorText);
  var _React$useState = _react.default.useState(null),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    inputText = _React$useState2[0],
    setInputText = _React$useState2[1];
  var textToDisplay = inputText != null ? inputText : value;
  _react.default.useEffect(function () {
    if (inputText === null || window.bypassDebounce === true) {
      return;
    }
    if (inputText !== value) {
      clearTimeout(timerId.current);
      timerId.current = setTimeout(function () {
        update(inputText, metadata);
        setInputText(null);
      }, timeoutDelay);
      return function () {
        clearTimeout(timerId.current);
        timerId.current = null;
      };
    } else {
      setInputText(null);
      clearTimeout(timerId.current);
    }

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [inputText, metadata, timeoutDelay, update, value]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.container
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: classes.inputContainer
  }, label && /*#__PURE__*/_react.default.createElement("label", {
    className: "".concat(classes.prepend, " ").concat(disabled && classes.disabledPrepend)
  }, label), /*#__PURE__*/_react.default.createElement(_InputBase.default, {
    classes: {
      input: (0, _classnames.default)(classes.controlInput, inputBaseInputStyle),
      error: classes.errorInput,
      disabled: classes.disabled,
      multiline: classes.multiline,
      inputMultiline: classes.inputMultiline
    },
    onBlur: onBlurInternal,
    onClick: onClick,
    type: inputControlType,
    placeholder: placeholder,
    value: textToDisplay,
    fullWidth: true,
    onChange: function onChange(event) {
      return onChangeHandler(event);
    },
    error: !!error,
    inputProps: inputAttributes,
    inputComponent: inputComponent,
    disabled: disabled,
    multiline: multiline,
    startAdornment: startAdornment,
    endAdornment: endAdornment,
    minRows: rows,
    title: tooltipText,
    autoComplete: autoComplete
  })), error && /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _classnames.default)(classes.errorText, errorTextStyle)
  }, error));
};
__signature__(InputBase, "useStyles{classes}\nuseRef{timerId}\nuseEffect{}\nuseState{[inputText, setInputText](null)}\nuseEffect{}", function () {
  return [useStyles];
});
__signature__(InputBase, "useStyles{classes}\nuseRef{timerId}\nuseEffect{}\nuseState{[inputText, setInputText](null)}\nuseEffect{}", function () {
  return [useStyles];
});
var compareInputBase = function compareInputBase(prev, next) {
  return prev.inputProps.get(_InputBaseProps.default.propNames.value) === next.inputProps.get(_InputBaseProps.default.propNames.value) && prev.inputProps.get(_InputBaseProps.default.propNames.error) === next.inputProps.get(_InputBaseProps.default.propNames.error) && prev.inputProps.get(_InputBaseProps.default.propNames.update) === next.inputProps.get(_InputBaseProps.default.propNames.update) && prev.inputProps.get(_InputBaseProps.default.propNames.disabled) === next.inputProps.get(_InputBaseProps.default.propNames.disabled);
};
var _default = _react.default.memo(InputBase, compareInputBase);
var _default2 = _default;
var _default3 = exports.default = _default2;
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/InputBase.js");
  reactHotLoader.register(AdvancedNumericInput, "AdvancedNumericInput", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/InputBase.js");
  reactHotLoader.register(InputBase, "InputBase", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/InputBase.js");
  reactHotLoader.register(compareInputBase, "compareInputBase", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/InputBase.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/InputBase.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/InputBase.js");
  reactHotLoader.register(AdvancedNumericInput, "AdvancedNumericInput", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/InputBase.js");
  reactHotLoader.register(InputBase, "InputBase", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/InputBase.js");
  reactHotLoader.register(compareInputBase, "compareInputBase", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/InputBase.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/InputBase.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();