"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.InputPassword = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _Input = require("./Input");
var _Styles = require("./Styles");
var _utils = require("../../utils");
var _IconButton = require("../IconButton");
var _Icons = require("../Icons");
var _Tooltip = require("../Tooltip");
var _Typography = require("../Typography");
var _excluded = ["size", "disabled", "show", "hideControl", "showTooltipText", "hideTooltipText", "value", "validation", "errorMessage", "invalid", "onChange"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 && Object.prototype.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 _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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
var transform = {
  biggest: "big",
  regular: "small",
  big: "medium"
};
var StypedEyeIconSmall = (0, _styledComponents.default)(_Icons.EyeSmallIcon).withConfig({
  displayName: "InputPassword__StypedEyeIconSmall",
  componentId: "sc-v930fj-0"
})(["", ""], {
  "cursor": "pointer"
});
var StypedEyeOffIconSmall = (0, _styledComponents.default)(_Icons.EyeOffSmallIcon).withConfig({
  displayName: "InputPassword__StypedEyeOffIconSmall",
  componentId: "sc-v930fj-1"
})(["", ""], {
  "cursor": "pointer"
});
var StypedEyeIcon = (0, _styledComponents.default)(_Icons.EyeIcon).withConfig({
  displayName: "InputPassword__StypedEyeIcon",
  componentId: "sc-v930fj-2"
})(["", ""], {
  "cursor": "pointer"
});
var StypedEyeOffIcon = (0, _styledComponents.default)(_Icons.EyeOffIcon).withConfig({
  displayName: "InputPassword__StypedEyeOffIcon",
  componentId: "sc-v930fj-3"
})(["", ""], {
  "cursor": "pointer"
});
var StyledInfoIcon = (0, _styledComponents.default)(_Icons.InfoIcon).withConfig({
  displayName: "InputPassword__StyledInfoIcon",
  componentId: "sc-v930fj-4"
})(["", ""], {
  "marginRight": "0.25rem",
  "cursor": "help",
  "fill": "var(--red-alert)"
});
var StyledInfoSmallIcon = (0, _styledComponents.default)(_Icons.InfoSmallIcon).withConfig({
  displayName: "InputPassword__StyledInfoSmallIcon",
  componentId: "sc-v930fj-5"
})(["", ""], {
  "marginRight": "0.25rem",
  "cursor": "help",
  "fill": "var(--red-alert)"
});
var StyledInputPasswordWrapper = _styledComponents.default.div.withConfig({
  displayName: "InputPassword__StyledInputPasswordWrapper",
  componentId: "sc-v930fj-6"
})(["width:fit-content;display:flex;flex-direction:column;"]);
var InputPasswordAdornment = function InputPasswordAdornment(_ref) {
  var invalid = _ref.invalid,
    errorMessage = _ref.errorMessage,
    size = _ref.size,
    hideControl = _ref.hideControl,
    showText = _ref.showText,
    hideTooltipText = _ref.hideTooltipText,
    showTooltipText = _ref.showTooltipText,
    disabled = _ref.disabled,
    toggleShow = _ref.toggleShow;
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, invalid && errorMessage && /*#__PURE__*/_react.default.createElement(_Tooltip.Tooltip, {
    title: errorMessage
  }, size === "regular" ? /*#__PURE__*/_react.default.createElement(StyledInfoSmallIcon, null) : /*#__PURE__*/_react.default.createElement(StyledInfoIcon, null)), !hideControl && /*#__PURE__*/_react.default.createElement(_Tooltip.Tooltip, {
    title: showText ? hideTooltipText : showTooltipText
  }, /*#__PURE__*/_react.default.createElement(_IconButton.IconButton, {
    variant: "text gray",
    type: "button",
    size: transform[size],
    disabled: disabled,
    onClick: toggleShow,
    style: {
      flexShrink: 0
    }
  }, showText ? size === "regular" ? /*#__PURE__*/_react.default.createElement(StypedEyeOffIconSmall, null) : /*#__PURE__*/_react.default.createElement(StypedEyeOffIcon, null) : size === "regular" ? /*#__PURE__*/_react.default.createElement(StypedEyeIconSmall, null) : /*#__PURE__*/_react.default.createElement(StypedEyeIcon, null))));
};
var InputPassword = exports.InputPassword = /*#__PURE__*/(0, _react.forwardRef)(function (_ref2, ref) {
  var _validation$levels;
  var _ref2$size = _ref2.size,
    size = _ref2$size === void 0 ? "regular" : _ref2$size,
    disabled = _ref2.disabled,
    _ref2$show = _ref2.show,
    show = _ref2$show === void 0 ? false : _ref2$show,
    hideControl = _ref2.hideControl,
    _ref2$showTooltipText = _ref2.showTooltipText,
    showTooltipText = _ref2$showTooltipText === void 0 ? "Show" : _ref2$showTooltipText,
    _ref2$hideTooltipText = _ref2.hideTooltipText,
    hideTooltipText = _ref2$hideTooltipText === void 0 ? "Hide" : _ref2$hideTooltipText,
    _ref2$value = _ref2.value,
    defaultValue = _ref2$value === void 0 ? "" : _ref2$value,
    validation = _ref2.validation,
    errorMessage = _ref2.errorMessage,
    invalid = _ref2.invalid,
    onChange = _ref2.onChange,
    rest = _objectWithoutProperties(_ref2, _excluded);
  var inputRef = (0, _react.useRef)(null);
  var handleRef = (0, _utils.useForkRef)(ref, inputRef);
  var _useState = (0, _react.useState)(show),
    _useState2 = _slicedToArray(_useState, 2),
    showText = _useState2[0],
    setShowText = _useState2[1];
  var _useState3 = (0, _react.useState)(0),
    _useState4 = _slicedToArray(_useState3, 2),
    strength = _useState4[0],
    setStrength = _useState4[1];
  var _useState5 = (0, _react.useState)(function () {
      return defaultValue;
    }),
    _useState6 = _slicedToArray(_useState5, 2),
    value = _useState6[0],
    setValue = _useState6[1];
  var handleChange = (0, _react.useCallback)(function (e) {
    setValue(e.target.value);
    typeof (validation === null || validation === void 0 ? void 0 : validation.getStrength) === "function" && setStrength(validation.getStrength(e.target.value));
    typeof onChange === "function" && onChange(e.target.value);
  }, [onChange, validation]);
  var toggleShow = (0, _react.useCallback)(function () {
    var _inputRef$current;
    (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || _inputRef$current.focus();
    setShowText(function (prevState) {
      return !prevState;
    });
  }, []);
  (0, _react.useEffect)(function () {
    setShowText(show);
  }, [show]);
  if (validation !== null && validation !== void 0 && validation.levels && validation !== null && validation !== void 0 && validation.getStrength && typeof (validation === null || validation === void 0 ? void 0 : validation.getStrength) === "function") {
    if (validation.levels.length !== 5) {
      console.warn("Levels length must be exactly 5.");
    }
    if (typeof validation.getStrength("test") !== "number") {
      console.warn("getStrength must be a function that accepts a string and returns a number.");
    }
  }
  return /*#__PURE__*/_react.default.createElement(StyledInputPasswordWrapper, null, /*#__PURE__*/_react.default.createElement(_Input.Input, _extends({}, rest, {
    ref: handleRef,
    size: size,
    value: value,
    disabled: disabled,
    type: showText ? "text" : "password",
    onChange: handleChange,
    invalid: invalid,
    "data-testid": "InputPassword",
    endAdornment: /*#__PURE__*/_react.default.createElement(InputPasswordAdornment, {
      invalid: invalid,
      errorMessage: errorMessage,
      size: size,
      hideControl: hideControl,
      showText: showText,
      hideTooltipText: hideTooltipText,
      showTooltipText: showTooltipText,
      disabled: disabled,
      toggleShow: toggleShow
    })
  })), (validation === null || validation === void 0 || (_validation$levels = validation.levels) === null || _validation$levels === void 0 ? void 0 : _validation$levels.length) === 5 && typeof (validation === null || validation === void 0 ? void 0 : validation.getStrength) === "function" && typeof (validation === null || validation === void 0 ? void 0 : validation.getStrength("test")) === "number" && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_Styles.StyledPasswordIndicatorWrapper, null, [...Array(4)].map(function (_, index) {
    return /*#__PURE__*/_react.default.createElement(_Styles.StyledPasswordIndicator, {
      key: index,
      $strength: strength,
      $index: index,
      "data-testid": strength >= index + 1 ? "active" : ""
    });
  })), /*#__PURE__*/_react.default.createElement(_Typography.Caption1, {
    align: "right",
    color: "tertiary"
  }, validation.levels[strength])));
});
InputPassword.displayName = "InputPassword";
//# sourceMappingURL=InputPassword.js.map