"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 _helpers = require("../../../helpers");
var _Booking = require("../../Booking.constants");
const _excluded = ["hotelverse", "state"];
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 _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
const Hotelverse = _ref => {
  let {
      hotelverse: {
        hash,
        token
      } = {},
      state
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      config: {
        hotels,
        lodgingDenomination: individual
      } = {},
      language,
      urlParams: {
        idHotel,
        reservationCode: id
      } = {}
    }
  } = (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');
    return () => {
      document.body.classList.remove('hotelverse-open');
      if (scriptEmbed) document.head.removeChild(scriptEmbed);
      if (scriptListener) document.head.removeChild(scriptListener);
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [open]);
  const {
    btoa
  } = window;
  const commandInterface = btoa(JSON.stringify({
    commandsInterface: {
      disabledBackButton: true,
      metrics: true
    },
    alg: 'HS256'
  }));
  const handlePress = () => setOpen(true);
  const accommodationType = (0, _helpers.getAccommodationType)({
    dataSource: hotels,
    hotels: [idHotel],
    individual: individual && typeof individual === 'object' ? individual[idHotel] : individual
  });
  return state !== _Booking.STATE.CANCELLED && token && hash ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, " ".concat(translate(_Hotelverse.L10N.LABEL_CHOOSE_ROOM, {
    type: accommodationType
  }), " "), /*#__PURE__*/_react.default.createElement(_ui.Action, _extends({}, others, {
    underline: true,
    onPress: handlePress,
    className: style.action
  }), translate(_Hotelverse.L10N.ACTION_CHOOSE_ROOM, {
    type: accommodationType
  })), /*#__PURE__*/_react.default.createElement(_ui.Modal, {
    blur: true,
    fit: true,
    portal: true,
    visible: open,
    onClose: () => setOpen(false),
    className: style.modal
  }, open && /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.View, {
    id: "hv",
    "data-hvhash": hash,
    "data-hvaccesstokenhv": token,
    "data-hvclientid": "6",
    "data-hvcommandsinterface": commandInterface,
    "data-hvexthotelid": idHotel,
    "data-hvextlocator": id,
    "data-hvlanguage": language.toUpperCase(),
    "data-hvproduct": "ChooseYourRoom"
  })))) : null;
};
exports.Hotelverse = Hotelverse;
Hotelverse.propTypes = {
  hotelverse: _propTypes.default.shape({
    hash: _propTypes.default.string,
    token: _propTypes.default.string
  }),
  state: _propTypes.default.number
};
Hotelverse.displayName = 'Mirai:User:Booking:Hotelverse';
//# sourceMappingURL=Hotelverse.js.map