"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _typeof = require("@babel/runtime/helpers/typeof");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _js = require("../../globals/js");

var _Input = _interopRequireDefault(require("../Input"));

var _iconsReact = require("@wfp/icons-react");

var _excluded = ["className", "closeButtonLabelText", "disabled", "formItemClassName", "id", "hideLabel", "hideControls", "labelText", "kind", "onChange", "onClick", "onSearchIconClick", "helperText", "light", "inputRef"];

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; }

var prefix = _js.settings.prefix;
/** Search enables users to specify a word or a phrase to find relevant pieces of content without the use of navigation. */

function Search(props) {
  var _classNames;

  var className = props.className,
      closeButtonLabelText = props.closeButtonLabelText,
      disabled = props.disabled,
      formItemClassName = props.formItemClassName,
      id = props.id,
      hideLabel = props.hideLabel,
      hideControls = props.hideControls,
      labelText = props.labelText,
      _props$kind = props.kind,
      kind = _props$kind === void 0 ? 'large' : _props$kind,
      _props$onChange = props.onChange,
      onChange = _props$onChange === void 0 ? function () {} : _props$onChange,
      _props$onClick = props.onClick,
      onClick = _props$onClick === void 0 ? function () {} : _props$onClick,
      _props$onSearchIconCl = props.onSearchIconClick,
      onSearchIconClick = _props$onSearchIconCl === void 0 ? function () {} : _props$onSearchIconCl,
      helperText = props.helperText,
      light = props.light,
      inputRef = props.inputRef,
      other = (0, _objectWithoutProperties2.default)(props, _excluded);
  var initialValue = props.value;

  var _useState = (0, _react.useState)(initialValue),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      value = _useState2[0],
      setValue = _useState2[1];

  (0, _react.useEffect)(function () {
    setValue(props.value);
  }, [props.value]);
  var newInputRef = (0, _react.useRef)(null);

  var _inputRef = inputRef ? inputRef : newInputRef;

  var handleChange = function handleChange(evt) {
    if (!disabled) {
      evt.persist();
      evt.imaginaryTarget = _inputRef;
      setValue(evt.target.value);
      onChange(evt.target.value, evt);
    }
  };

  var clearSearch = function clearSearch() {
    var valueState = '';
    setValue(valueState);
    onChange(valueState);
  };

  var numberInputClasses = (0, _classnames.default)("".concat(prefix, "--number"), className, (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--number--light"), light), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--number--helpertext"), helperText), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--number--nolabel"), hideLabel), (0, _defineProperty2.default)(_classNames, "".concat(prefix, "--number--nocontrols"), hideControls), (0, _defineProperty2.default)(_classNames, 'wfp--search', true), (0, _defineProperty2.default)(_classNames, 'wfp--search--lg', kind === 'large'), (0, _defineProperty2.default)(_classNames, 'wfp--search--sm', kind === 'small'), (0, _defineProperty2.default)(_classNames, 'wfp--search--main', kind === 'main'), (0, _defineProperty2.default)(_classNames, 'wfp--search--banner', kind === 'banner'), (0, _defineProperty2.default)(_classNames, 'wfp--search--light', kind === 'light'), _classNames));
  var newProps = {
    disabled: disabled,
    id: id,
    onChange: handleChange,
    value: value
  };
  var clearClasses = (0, _classnames.default)({
    'wfp--search-close': true,
    'wfp--search-close--hidden': !value
  });
  return /*#__PURE__*/_react.default.createElement(_Input.default, (0, _extends2.default)({}, props, {
    formItemClassName: numberInputClasses,
    inputWrapperClassName: "".concat(prefix, "--search-input__wrapper")
  }), function () {
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_iconsReact.SearchGlyph, {
      description: labelText,
      className: "".concat(prefix, "--search-magnifier-icon"),
      onClick: onSearchIconClick
    }), /*#__PURE__*/_react.default.createElement("input", (0, _extends2.default)({
      className: "wfp--search-input",
      type: "text"
    }, other, newProps, {
      ref: _inputRef
    })), /*#__PURE__*/_react.default.createElement("button", {
      className: clearClasses,
      onClick: clearSearch,
      type: "button",
      "aria-label": closeButtonLabelText
    }, /*#__PURE__*/_react.default.createElement(_iconsReact.CloseGlyph, {
      description: closeButtonLabelText
    })));
  });
}

Search.propTypes = {
  /**
   * Specify if the control should be disabled, or not
   */
  disabled: _propTypes.default.bool,

  /**
   * Generic `label` that will be used as the textual representation of what
   * this field is for
   */
  labelText: _propTypes.default.node,

  /**
   * Specify whether you want the underlying label to be visually hidden
   */
  hideLabel: _propTypes.default.bool,

  /**
   * Provide text that is used alongside the control label for additional help
   */
  helperText: _propTypes.default.node,

  /**
   * Specify the placeholder attribute for the &lt;input&gt;
   */
  placeholder: _propTypes.default.string,

  /**
   * Specify an optional className to be applied to the wrapper node
   */
  className: _propTypes.default.string,

  /**
   * Specify an optional className to be applied to the form-item node
   */
  formItemClassName: _propTypes.default.string,

  /**
   * Specify a custom `id` for the input
   */
  id: _propTypes.default.string.isRequired,

  /**
   * The new value is available first arg 'searchValue' and evt object if needed is on second arg.
   * i.e.
   * const handleChange = (searchValue, evt) => {
   * console.log("searchValue", searchValue); // a string
   * console.log("evt", evt); // the whole event object
   * }
   */
  onChange: _propTypes.default.func,

  /**
   * Provide an optional function to be called when the up/down button is clicked
   */
  onClick: _propTypes.default.func
};
Search.defaultProps = {
  disabled: false
};
var _default = Search;
exports.default = _default;