"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 }; }
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; }
const Search = _ref => {
  let _ref$value = _ref.value,
    propValue = _ref$value === void 0 ? '' : _ref$value,
    _ref$onChange = _ref.onChange,
    onChange = _ref$onChange === void 0 ? () => {} : _ref$onChange;
  const _useLocale = (0, _locale.useLocale)(),
    translate = _useLocale.translate;
  const ref = (0, _react.useRef)(null);
  const _useState = (0, _react.useState)(false),
    _useState2 = _slicedToArray(_useState, 2),
    visible = _useState2[0],
    setVisible = _useState2[1];
  const _useState3 = (0, _react.useState)(propValue),
    _useState4 = _slicedToArray(_useState3, 2),
    value = _useState4[0],
    setValue = _useState4[1];
  (0, _react.useEffect)(() => {
    const handleClickOutside = _ref2 => {
      var _ref$current;
      let target = _ref2.target;
      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