"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Hotelverse = void 0;
var _dataSources = require("@mirai/data-sources");
var _locale = require("@mirai/locale");
var _ui = require("@mirai/ui");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = _interopRequireWildcard(require("react"));
var _Hotelverse = require("./Hotelverse.l10n");
var style = _interopRequireWildcard(require("./Hotelverse.module.css"));
var _shared__ = require("../../../__shared__");
var _helpers = require("../../../helpers");
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 Hotelverse = function () {
  let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
    country = _ref.country,
    _ref$hotelverse = _ref.hotelverse,
    _ref$hotelverse2 = _ref$hotelverse === void 0 ? {} : _ref$hotelverse,
    hash = _ref$hotelverse2.hash,
    token = _ref$hotelverse2.token,
    infantAge = _ref.infantAge,
    _ref$items = _ref.items,
    items = _ref$items === void 0 ? [] : _ref$items,
    multiRoom = _ref.multiRoom,
    taxes = _ref.taxes,
    _ref$onSelect = _ref.onSelect,
    onSelect = _ref$onSelect === void 0 ? () => {} : _ref$onSelect;
  const _useDevice = (0, _ui.useDevice)(),
    isMobile = _useDevice.isMobile;
  const _useLocale = (0, _locale.useLocale)(),
    translate = _useLocale.translate;
  const _useStore = (0, _dataSources.useStore)(),
    _useStore$value = _useStore.value,
    _useStore$value2 = _useStore$value === void 0 ? {} : _useStore$value,
    currency = _useStore$value2.currency,
    language = _useStore$value2.language,
    _useStore$value2$find = _useStore$value2.finder,
    _useStore$value2$find2 = _useStore$value2$find === void 0 ? {} : _useStore$value2$find,
    _useStore$value2$find3 = _useStore$value2$find2.calendar,
    _useStore$value2$find4 = _useStore$value2$find3 === void 0 ? [] : _useStore$value2$find3,
    _useStore$value2$find5 = _slicedToArray(_useStore$value2$find4, 2),
    checkIn = _useStore$value2$find5[0],
    checkOut = _useStore$value2$find5[1],
    _useStore$value2$find6 = _useStore$value2$find2.occupation,
    _useStore$value2$find7 = _useStore$value2$find6 === void 0 ? [] : _useStore$value2$find6,
    _useStore$value2$find8 = _slicedToArray(_useStore$value2$find7, 1),
    _useStore$value2$find9 = _useStore$value2$find8[0],
    occupation = _useStore$value2$find9 === void 0 ? [] : _useStore$value2$find9,
    _useStore$value2$find0 = _useStore$value2$find2.promocode,
    promocode = _useStore$value2$find0 === void 0 ? '' : _useStore$value2$find0,
    _useStore$value2$urlP = _useStore$value2.urlParams,
    _useStore$value2$urlP2 = _useStore$value2$urlP === void 0 ? {} : _useStore$value2$urlP,
    idHotel = _useStore$value2$urlP2.idtokenprovider;
  const _useState = (0, _react.useState)(false),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  (0, _react.useEffect)(() => {
    const cdn = process.env.HOTELVERSE_CDN;
    const scriptEmbed = open && (0, _helpers.addScript)("".concat(cdn, "/scripts/embed.js"));
    const scriptListener = open && (0, _helpers.addScript)("".concat(cdn, "/scripts/interfaceListener.js"));
    if (open) {
      document.body.classList.add('hotelverse-open');
      window.callbackSelectRoom = function () {
        let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [],
          _ref3 = _slicedToArray(_ref2, 1),
          _ref3$ = _ref3[0],
          hvIsOr = _ref3$.hvIsOr,
          idHotelverse = _ref3$.hvLocator,
          roomNumber = _ref3$.hvRoomNumber,
          resultKey = _ref3$.resultKey;
        setOpen(false);
        const boardId = resultKey.replace(/\|/g, '--');
        const board = items.flatMap(function () {
          let _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
            boards = _ref4.boards;
          return boards;
        }).find(function () {
          let _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
            id = _ref5.id;
          return id === boardId;
        });
        onSelect(board, {
          idHotelverse,
          roomNumber,
          subjectToAvailability: hvIsOr
        });
      };
    }
    return () => {
      document.body.classList.remove('hotelverse-open');
      if (scriptEmbed) document.head.removeChild(scriptEmbed);
      if (scriptListener) document.head.removeChild(scriptListener);
      window.callbackSelectRoom = undefined;
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [open]);
  const dateFormat = date => (0, _locale.dateFormat)(date, {
    delimiter: '-',
    format: 'YYYY-MM-DD'
  });
  const _window = window,
    btoa = _window.btoa;
  const commandInterface = btoa(JSON.stringify({
    onSelectRoom: {
      callback: 'callbackSelectRoom',
      enabled: true,
      method: 'PostMessage',
      customParams: []
    }
  }));
  const _ref6 = occupation.find(function () {
      let _ref7 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        type = _ref7.type;
      return type !== _shared__.TYPE_OCCUPATION.CHILD;
    }) || {},
    _ref6$amount = _ref6.amount,
    adults = _ref6$amount === void 0 ? 0 : _ref6$amount;
  const _ref8 = occupation.find(function () {
      let _ref9 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        type = _ref9.type;
      return type === _shared__.TYPE_OCCUPATION.CHILD;
    }) || {},
    _ref8$ages = _ref8.ages,
    childAges = _ref8$ages === void 0 ? [] : _ref8$ages;
  const children = childAges.filter(age => age > infantAge).length;
  const infants = childAges.filter(age => age < infantAge).length;
  const search = btoa(JSON.stringify({
    checkIn: dateFormat(checkIn),
    checkOut: dateFormat(checkOut),
    paxes: [{
      adults,
      children,
      infants,
      childAges
    }],
    taxIncluded: !!taxes,
    currency,
    country,
    language,
    promocode
  }));
  return !multiRoom && hash && token ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.Button, {
    secondary: true,
    wide: isMobile,
    onPress: () => setOpen(!open)
  }, translate(_Hotelverse.L10N.ACTION_HOTEL_VERSE)), /*#__PURE__*/_react.default.createElement(_ui.Modal, {
    blur: true,
    fit: true,
    portal: true,
    title: translate(_Hotelverse.L10N.LABEL_HOTEL_VERSE),
    visible: open,
    onClose: () => setOpen(false),
    "data-testid": "hv-modal",
    className: style.modal
  }, open && /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", {
    id: "hv",
    "data-hvaccesstokenhv": token,
    "data-hvclientid": "6",
    "data-hvcommandsinterface": commandInterface,
    "data-hvcurrency": currency,
    "data-hvexthotelid": idHotel,
    "data-hvhash": hash,
    "data-hvlanguage": language.toUpperCase(),
    "data-hvproduct": "BookYourRoom",
    "data-hvsearch": search
  })))) : null;
};
exports.Hotelverse = Hotelverse;
Hotelverse.displayName = 'Mirai:Core:Rates:Hotelverse';
Hotelverse.propTypes = {
  country: _propTypes.default.string,
  hotelverse: _propTypes.default.shape({
    hash: _propTypes.default.string,
    token: _propTypes.default.string
  }),
  infantAge: _propTypes.default.number,
  items: _propTypes.default.array,
  multiRoom: _propTypes.default.bool,
  taxes: _propTypes.default.string,
  onSelect: _propTypes.default.func
};
//# sourceMappingURL=Hotelverse.js.map