"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.Suffix = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _system = require("@wix/wix-ui-icons-common/system");
var _CloseButton = _interopRequireDefault(require("../../../CloseButton"));
var _StatusIndicator = _interopRequireDefault(require("../../../StatusIndicator"));
var _InputSt = require("../../Input.st.css.js");
var _Input = require("../../Input.constants");
var _Tooltip = _interopRequireDefault(require("../../../Tooltip"));
var _StatusContext = require("../../../FormField/StatusContext");
var _WixDesignSystemIconThemeProvider = require("../../../WixDesignSystemIconThemeProvider");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/Input/components/Suffix/Suffix.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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) { (0, _defineProperty2.default)(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; }
var Suffix = _ref => {
  var {
    statusMessage,
    status,
    disabled,
    menuArrow,
    suffix,
    tooltipPlacement,
    clearButtonTooltipContent,
    clearButtonTooltipProps,
    clearButtonAriaLabel,
    statusMessageTooltipProps,
    hideStatusSuffix,
    onClear,
    clearButton,
    value,
    size,
    onInputClicked,
    focusOnClearClick,
    inputElementRef
  } = _ref;
  var icons = (0, _WixDesignSystemIconThemeProvider.useIcons)('InputSuffix', {
    DropDownArrow: _system.DropDownArrow
  });
  var isClearButtonVisible = (!!onClear || !!clearButton) && !!value && !disabled;
  var statusContext = (0, _react.useContext)(_StatusContext.StatusContext);
  var handleClick = (0, _react.useCallback)(event => {
    if (!disabled) {
      var _inputElementRef$curr;
      (_inputElementRef$curr = inputElementRef.current) == null || _inputElementRef$curr.focus();
      onInputClicked == null || onInputClicked(event);
    }
  }, [disabled, inputElementRef, onInputClicked]);
  var handleClear = (0, _react.useCallback)(event => {
    if (focusOnClearClick) {
      var _inputElementRef$curr2;
      (_inputElementRef$curr2 = inputElementRef.current) == null || _inputElementRef$curr2.focus();
    }
    onClear == null || onClear(event);
  }, [focusOnClearClick, inputElementRef, onClear]);
  var ClearButton = (0, _react.useMemo)(() => {
    if (!isClearButtonVisible) return null;
    var handleKeyDown = event => {
      if (event.key === 'Enter' || event.key === ' ') {
        event.preventDefault();
        handleClear(event);
      }
    };
    var clearButtonSizeMap = {
      [_Input.SIZES.tiny]: 'small',
      [_Input.SIZES.small]: 'small',
      [_Input.SIZES.medium]: 'medium',
      [_Input.SIZES.large]: 'medium'
    };
    var ClearButtonElement = /*#__PURE__*/_react.default.createElement(_CloseButton.default, {
      as: "button",
      className: _InputSt.classes.clearButton,
      dataHook: _Input.dataHooks.clearButton,
      skin: "standardFilled",
      size: clearButtonSizeMap[size !== null && size !== void 0 ? size : _Input.SIZES.medium],
      onClick: handleClear,
      onKeyDown: handleKeyDown,
      "aria-label": clearButtonAriaLabel,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 83,
        columnNumber: 7
      }
    });
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _InputSt.classes.clearButtonWrapper,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 96,
        columnNumber: 7
      }
    }, clearButtonTooltipContent ? /*#__PURE__*/_react.default.createElement(_Tooltip.default, (0, _extends2.default)({}, clearButtonTooltipProps, {
      content: clearButtonTooltipContent,
      dataHook: _Input.dataHooks.tooltip,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 98,
        columnNumber: 11
      }
    }), ClearButtonElement) : ClearButtonElement);
  }, [isClearButtonVisible, handleClear, clearButtonTooltipContent, clearButtonTooltipProps, clearButtonAriaLabel, size]);
  var ErrorWarningIndicator = (0, _react.useMemo)(() => {
    if (disabled || hideStatusSuffix) return null;
    if (status !== 'error' && status !== 'warning') return null;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _InputSt.classes.statusWrapper,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 123,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react.default.createElement(_StatusIndicator.default, {
      dataHook: _Input.dataHooks.status,
      status: status,
      message: statusMessage,
      tooltipProps: _objectSpread({
        placement: tooltipPlacement
      }, statusMessageTooltipProps),
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 124,
        columnNumber: 9
      }
    }));
  }, [disabled, hideStatusSuffix, status, statusMessage, tooltipPlacement, statusMessageTooltipProps]);
  var LoadingIndicator = (0, _react.useMemo)(() => {
    var _statusContext$status;
    if (disabled || hideStatusSuffix) return null;
    if (statusContext.status !== 'loading' && status !== 'loading') return null;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _InputSt.classes.statusWrapper,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 148,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react.default.createElement(_StatusIndicator.default, {
      dataHook: _Input.dataHooks.status,
      status: (_statusContext$status = statusContext.status) !== null && _statusContext$status !== void 0 ? _statusContext$status : status,
      message: statusMessage,
      tooltipProps: _objectSpread({
        placement: tooltipPlacement
      }, statusMessageTooltipProps),
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 149,
        columnNumber: 9
      }
    }));
  }, [disabled, hideStatusSuffix, statusMessage, tooltipPlacement, statusMessageTooltipProps, statusContext.status, status]);
  var CustomSuffix = (0, _react.useMemo)(() => {
    if (!suffix) return null;
    return /*#__PURE__*/_react.default.isValidElement(suffix) ? (/*#__PURE__*/_react.default.cloneElement(suffix)) : /*#__PURE__*/_react.default.createElement("div", {
      className: _InputSt.classes.suffix,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 175,
        columnNumber: 7
      }
    }, suffix);
  }, [suffix]);
  var MenuArrow = (0, _react.useMemo)(() => {
    if (!menuArrow) return null;
    return /*#__PURE__*/_react.default.createElement("div", {
      "data-hook": _Input.dataHooks.menuArrow,
      className: _InputSt.classes.menuArrow,
      onClick: handleClick,
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 182,
        columnNumber: 7
      }
    }, /*#__PURE__*/_react.default.createElement(icons.DropDownArrow, {
      __self: void 0,
      __source: {
        fileName: _jsxFileName,
        lineNumber: 187,
        columnNumber: 9
      }
    }));
  }, [menuArrow, handleClick, icons]);
  if (!ClearButton && !ErrorWarningIndicator && !LoadingIndicator && !CustomSuffix && !MenuArrow) return null;
  return /*#__PURE__*/_react.default.createElement("div", {
    "data-hook": _Input.dataHooks.suffixes,
    className: _InputSt.classes.suffixes,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 202,
      columnNumber: 5
    }
  }, ClearButton, ErrorWarningIndicator, LoadingIndicator, CustomSuffix, MenuArrow);
};
exports.Suffix = Suffix;
//# sourceMappingURL=Suffix.js.map