"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Search = void 0;
var _locale = require("@mirai/locale");
var _ui = require("@mirai/ui");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _Bookings = require("./Bookings.l10n");
var style = _interopRequireWildcard(require("./Bookings.module.css"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (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 (const 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 _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const Search = _ref => {
  let {
    value: propValue = '',
    onChange = () => {}
  } = _ref;
  const {
    translate
  } = (0, _locale.useLocale)();
  const ref = (0, _react.useRef)(null);
  const [visible, setVisible] = (0, _react.useState)(false);
  const [value, setValue] = (0, _react.useState)(propValue);
  (0, _react.useEffect)(() => {
    const handleClickOutside = _ref2 => {
      var _ref$current;
      let {
        target
      } = _ref2;
      if (!((_ref$current = ref.current) !== null && _ref$current !== void 0 && _ref$current.contains(target)) && !!visible) setVisible(undefined);
    };
    document.addEventListener('click', handleClickOutside, true);
    return () => document.removeEventListener('click', handleClickOutside, true);
  }, [visible]);
  const handleChange = (next, event) => {
    setValue(next);
    if (!next) {
      onChange(next, event);
    }
  };
  const handlePress = event => {
    setVisible(false);
    onChange(value, event);
  };
  return /*#__PURE__*/_react.default.createElement(_ui.Menu, {
    options: [{
      children: /*#__PURE__*/_react.default.createElement(_ui.View, {
        ref: ref,
        className: style.form
      }, /*#__PURE__*/_react.default.createElement(_ui.InputText, {
        name: "search",
        type: "search",
        placeholder: translate(_Bookings.L10N.LABEL_SEARCH_PLACEHOLDER),
        value: value,
        onChange: handleChange,
        className: style.input
      }), /*#__PURE__*/_react.default.createElement(_ui.Button, {
        disabled: !value,
        wide: true,
        onPress: handlePress,
        className: style.button,
        testId: "button-search"
      }, translate(_Bookings.L10N.ACTION_SEARCH_CTA)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
        bold: true,
        small: true
      }, translate(_Bookings.L10N.LABEL_RESERVATION_CODE)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
        small: true
      }, translate(_Bookings.L10N.LABEL_RESERVATION_CODE_DESCRIPTION)))
    }],
    visible: visible,
    onPress: () => setVisible(false),
    className: style.search,
    testId: "menu-search-bookings"
  }, /*#__PURE__*/_react.default.createElement(_ui.Action, {
    onPress: () => setVisible(true),
    className: style.actionSearch,
    testId: "show-search"
  }, translate(_Bookings.L10N.ACTION_SEARCH)));
};
exports.Search = Search;
Search.displayName = 'Mirai:Core:Profile:Bookings:Search';
Search.propTypes = {
  value: _propTypes.default.func,
  onChange: _propTypes.default.func
};
//# sourceMappingURL=Bookings.Search.js.map