"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 }; }
const Hotelverse = function () {
  let {
    country,
    hotelverse: {
      hash,
      token
    } = {},
    infantAge,
    items = [],
    multiRoom,
    taxes,
    onSelect = () => {}
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  const {
    isMobile
  } = (0, _ui.useDevice)();
  const {
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      currency,
      language,
      finder: {
        calendar: [checkIn, checkOut] = [],
        occupation: [occupation = []] = [],
        promocode = ''
      } = {},
      urlParams: {
        idtokenprovider: idHotel
      } = {}
    } = {}
  } = (0, _dataSources.useStore)();
  const [open, setOpen] = (0, _react.useState)(false);
  (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 [{
          hvIsOr,
          hvLocator: idHotelverse,
          hvRoomNumber: roomNumber,
          resultKey
        }] = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
        setOpen(false);
        const boardId = resultKey.replace(/\|/g, '--');
        const board = items.flatMap(function () {
          let {
            boards
          } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
          return boards;
        }).find(function () {
          let {
            id
          } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
          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 {
    btoa
  } = window;
  const commandInterface = btoa(JSON.stringify({
    onSelectRoom: {
      callback: 'callbackSelectRoom',
      enabled: true,
      method: 'PostMessage',
      customParams: []
    }
  }));
  const {
    amount: adults = 0
  } = occupation.find(function () {
    let {
      type
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return type !== _shared__.TYPE_OCCUPATION.CHILD;
  }) || {};
  const {
    ages: childAges = []
  } = occupation.find(function () {
    let {
      type
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return type === _shared__.TYPE_OCCUPATION.CHILD;
  }) || {};
  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