"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BookingSummary = 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 _BookingSummary = require("./BookingSummary.constants");
var _BookingSummary2 = require("./BookingSummary.l10n");
var style = _interopRequireWildcard(require("./BookingSummary.module.css"));
var _helpers = require("../../helpers");
var _ButtonMore = require("../ButtonMore");
var _PriceBreakdown = require("../PriceBreakdown");
const _excluded = ["box", "children", "dataSource"],
  _excluded2 = ["roomNumberBySelectedRoom"];
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 _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; }
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; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const ADULT = _helpers.OCCUPATION_TYPE.ADULT,
  BABY = _helpers.OCCUPATION_TYPE.BABY,
  CHILD = _helpers.OCCUPATION_TYPE.CHILD;
const getBookingRoomNames = function () {
  let rooms = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  return Object.values(rooms.reduce(function (total) {
    var _total$type;
    let _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
      amount = _ref.amount,
      type = _ref.type;
    return _objectSpread(_objectSpread({}, total), type ? {
      [type]: {
        amount: (((_total$type = total[type]) === null || _total$type === void 0 ? void 0 : _total$type.amount) || 0) + amount,
        type
      }
    } : undefined);
  }, {})).map(function () {
    let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      amount = _ref2.amount,
      type = _ref2.type;
    return "".concat(type).concat(amount > 1 ? " (x".concat(amount, ")") : '');
  });
};
const getBookingOccupationLabel = _ref3 => {
  let _ref3$amount = _ref3.amount,
    amount = _ref3$amount === void 0 ? 0 : _ref3$amount,
    translate = _ref3.translate,
    type = _ref3.type;
  return "".concat(amount, " ").concat((0, _helpers.getGuestLabel)({
    plural: amount !== 1,
    type
  }, translate));
};
const getBookingAccommodationLabel = _ref4 => {
  let _ref4$amount = _ref4.amount,
    amount = _ref4$amount === void 0 ? 0 : _ref4$amount,
    translate = _ref4.translate,
    type = _ref4.type;
  return "".concat(amount, " ").concat((0, _helpers.getAccommodationLabel)({
    plural: amount !== 1,
    type
  }, translate).toLowerCase());
};
const getPositiveLabels = function () {
  let items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  return items.filter(function () {
    let _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      amount = _ref5.amount;
    return amount > 0;
  }).map(_ref6 => {
    let label = _ref6.label;
    return label;
  });
};
const BookingSummary = _ref7 => {
  var _ref12;
  let _ref7$box = _ref7.box,
    box = _ref7$box === void 0 ? false : _ref7$box,
    children = _ref7.children,
    _ref7$dataSource = _ref7.dataSource,
    dataSource = _ref7$dataSource === void 0 ? {} : _ref7$dataSource,
    others = _objectWithoutProperties(_ref7, _excluded);
  const _useDevice = (0, _ui.useDevice)(),
    isDesktop = _useDevice.isDesktop;
  const _useLocale = (0, _locale.useLocale)(),
    dateFormat = _useLocale.dateFormat,
    translate = _useLocale.translate;
  const _useStore = (0, _dataSources.useStore)(),
    _useStore$value = _useStore.value,
    _useStore$value$compo = _useStore$value.components,
    components = _useStore$value$compo === void 0 ? {} : _useStore$value$compo,
    _useStore$value$finde = _useStore$value.finder,
    _useStore$value$finde2 = _useStore$value$finde === void 0 ? {} : _useStore$value$finde,
    _useStore$value$finde3 = _useStore$value$finde2.promocode,
    promocode = _useStore$value$finde3 === void 0 ? '' : _useStore$value$finde3,
    _useStore$value$hotel = _useStore$value.hotel,
    _useStore$value$hotel2 = _useStore$value$hotel === void 0 ? {} : _useStore$value$hotel,
    hotelAccommodationType = _useStore$value$hotel2.accommodationType,
    _useStore$value$hotel3 = _useStore$value$hotel2.features,
    features = _useStore$value$hotel3 === void 0 ? {} : _useStore$value$hotel3,
    id = _useStore$value$hotel2.id,
    _useStore$value$hotel4 = _useStore$value$hotel2.variants,
    variants = _useStore$value$hotel4 === void 0 ? {} : _useStore$value$hotel4,
    language = _useStore$value.language,
    _useStore$value$urlPa = _useStore$value.urlParams,
    _useStore$value$urlPa2 = _useStore$value$urlPa === void 0 ? {} : _useStore$value$urlPa,
    roomNumberBySelectedRoom = _useStore$value$urlPa2.roomNumberBySelectedRoom,
    urlParams = _objectWithoutProperties(_useStore$value$urlPa2, _excluded2),
    variant = _useStore$value.variant;
  const _useState = (0, _react.useState)(isDesktop),
    _useState2 = _slicedToArray(_useState, 2),
    expanded = _useState2[0],
    setExpanded = _useState2[1];
  const checkIn = dataSource.checkIn,
    checkInTime = dataSource.checkInTime,
    checkOut = dataSource.checkOut,
    checkOutTime = dataSource.checkOutTime,
    currency = dataSource.currency,
    _dataSource$details = dataSource.details,
    details = _dataSource$details === void 0 ? [] : _dataSource$details,
    _dataSource$cancellat = dataSource.cancellationPolicy,
    _dataSource$cancellat2 = _dataSource$cancellat === void 0 ? {} : _dataSource$cancellat,
    cancellationCaption = _dataSource$cancellat2.caption,
    cancellationName = _dataSource$cancellat2.name,
    _dataSource$extras = dataSource.extras,
    extras = _dataSource$extras === void 0 ? [] : _dataSource$extras,
    _dataSource$hotel = dataSource.hotel,
    _dataSource$hotel2 = _dataSource$hotel === void 0 ? {} : _dataSource$hotel,
    address = _dataSource$hotel2.address,
    city = _dataSource$hotel2.city,
    country = _dataSource$hotel2.country,
    email = _dataSource$hotel2.email,
    _dataSource$hotel2$lo = _dataSource$hotel2.location,
    location = _dataSource$hotel2$lo === void 0 ? [] : _dataSource$hotel2$lo,
    _dataSource$hotel2$na = _dataSource$hotel2.name,
    hotelName = _dataSource$hotel2$na === void 0 ? '' : _dataSource$hotel2$na,
    phone = _dataSource$hotel2.phone,
    site = _dataSource$hotel2.site,
    nights = dataSource.nights;
  const handleBack = () => {
    _dataSources.Event.publish(_helpers.EVENT.METRICS, {
      id: _helpers.METRICS_EVENTS.CHECKOUT_INTERACTION,
      action: 'button_click',
      element_clicked: 'change_selection'
    });
    setTimeout(() => {
      document.location.href = (0, _helpers.getRatesUrl)(_objectSpread({
        currency,
        language,
        nights,
        promocode,
        variant
      }, urlParams));
    }, [_helpers.SUBMIT_DELAY]);
  };
  const handlePress = () => {
    setExpanded(true);
    _dataSources.Event.publish(_helpers.EVENT.METRICS, {
      id: _helpers.METRICS_EVENTS.CHECKOUT_INTERACTION,
      action: 'button_click',
      element_clicked: 'view_more'
    });
  };
  const _dateDiff = (0, _locale.dateDiff)((0, _locale.parseDate)(checkIn), (0, _locale.parseDate)(checkOut)),
    days = _dateDiff.days;
  const isCheckout = !!components.checkout;
  const items = details.filter(function () {
    let _ref8 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      type = _ref8.type;
    return !!type;
  }) || [];
  const roomNumber = (0, _helpers.getRoomNumber)(roomNumberBySelectedRoom);
  const urlLocation = location.length ? "https://maps.google.com?q=".concat(location.join(',')) : undefined;
  const _ref9 = features[id] || {},
    _ref9$name = _ref9.name,
    name = _ref9$name === void 0 ? hotelName : _ref9$name,
    type = _ref9.type;
  const _ref0 = variants[id] || {},
    _ref0$accommodationTy = _ref0.accommodationType,
    accommodationType = _ref0$accommodationTy === void 0 ? hotelAccommodationType : _ref0$accommodationTy;
  const bookingDetails = details.reduce(function (total) {
    let _ref1 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
      amount = _ref1.amount,
      _ref1$extras = _ref1.extras,
      extras = _ref1$extras === void 0 ? [] : _ref1$extras,
      _ref1$occupation = _ref1.occupation,
      _ref1$occupation2 = _ref1$occupation === void 0 ? {} : _ref1$occupation,
      _ref1$occupation2$adu = _ref1$occupation2.adults,
      adults = _ref1$occupation2$adu === void 0 ? 0 : _ref1$occupation2$adu,
      _ref1$occupation2$bab = _ref1$occupation2.babies,
      babies = _ref1$occupation2$bab === void 0 ? 0 : _ref1$occupation2$bab,
      _ref1$occupation2$chi = _ref1$occupation2.children,
      children = _ref1$occupation2$chi === void 0 ? 0 : _ref1$occupation2$chi,
      rooms = _ref1.rooms;
    const roomAmount = rooms || amount || 1;
    return {
      adults: total.adults + adults * roomAmount,
      babies: total.babies + babies * roomAmount,
      children: total.children + children * roomAmount,
      extras: [...total.extras, ...extras],
      rooms: total.rooms + roomAmount
    };
  }, {
    adults: 0,
    babies: 0,
    children: 0,
    extras: [],
    rooms: 0
  });
  const bookingRoomNames = getBookingRoomNames(details);
  const bookingExtras = [...bookingDetails.extras, ...extras];
  const bookingExtraNames = bookingExtras.map(function () {
    let _ref10 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      title = _ref10.title;
    return title;
  }).filter(Boolean);
  const bookingSummaryLabels = getPositiveLabels([{
    amount: nights || days || 0,
    label: translate(_BookingSummary2.L10N.LABEL_NIGHT_COUNT, {
      value: nights || days || 0
    })
  }, {
    amount: bookingDetails.rooms,
    label: getBookingAccommodationLabel({
      amount: bookingDetails.rooms,
      translate,
      type: accommodationType
    })
  }, {
    amount: bookingExtras.length,
    label: translate(_BookingSummary2.L10N.LABEL_EXTRA_COUNT, {
      value: bookingExtras.length
    })
  }]);
  const bookingOccupationLabels = getPositiveLabels([{
    amount: bookingDetails.adults,
    label: getBookingOccupationLabel({
      amount: bookingDetails.adults,
      translate,
      type: ADULT
    })
  }, {
    amount: bookingDetails.children,
    label: getBookingOccupationLabel({
      amount: bookingDetails.children,
      translate,
      type: CHILD
    })
  }, {
    amount: bookingDetails.babies,
    label: getBookingOccupationLabel({
      amount: bookingDetails.babies,
      translate,
      type: BABY
    })
  }]);
  const showCancellation = cancellationName || cancellationCaption;
  return /*#__PURE__*/_react.default.createElement(_ui.View, _extends({}, others, {
    wide: true,
    className: (0, _ui.styles)(style.bookingSummary, box && style.box, others.className)
  }), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.section
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: isDesktop
  }, translate(_BookingSummary2.L10N.LABEL_BOOKING_SUMMARY)), /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    wide: true,
    className: style.dates
  }, /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true
  }, translate(_BookingSummary2.L10N.LABEL_CHECK_IN)), /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, dateFormat((0, _locale.parseDate)(checkIn), _BookingSummary.FORMAT_DATE)), checkInTime && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    light: true,
    small: true
  }, translate(_BookingSummary2.L10N.LABEL_CHECKIN_TIME, {
    value: checkInTime
  })))), /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true
  }, translate(_BookingSummary2.L10N.LABEL_CHECK_OUT)), /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, dateFormat((0, _locale.parseDate)(checkOut), _BookingSummary.FORMAT_DATE)), checkOutTime && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    light: true,
    small: true
  }, translate(_BookingSummary2.L10N.LABEL_CHECKOUT_TIME, {
    value: checkOutTime
  }))))), /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true
  }, translate(_BookingSummary2.L10N.LABEL_YOUR_BOOKING)), (bookingSummaryLabels === null || bookingSummaryLabels === void 0 ? void 0 : bookingSummaryLabels.length) > 0 && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, bookingSummaryLabels.join(', ')), (bookingOccupationLabels === null || bookingOccupationLabels === void 0 ? void 0 : bookingOccupationLabels.length) > 0 && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, bookingOccupationLabels.join(', ')), roomNumber ? items.map(function () {
    let _ref11 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      amount = _ref11.amount,
      type = _ref11.type;
    let index = arguments.length > 1 ? arguments[1] : undefined;
    return /*#__PURE__*/_react.default.createElement(_ui.View, {
      key: index,
      row: true,
      className: style.info
    }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      light: true,
      small: true
    }, "".concat(type).concat(roomNumber ? " (".concat(translate(_BookingSummary2.L10N.LABEL_ROOM_NUMBER, {
      roomNumber
    }), ")") : '', " ").concat(amount > 1 ? " (x".concat(amount, ")") : '')), roomNumber && /*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
      pressable: true,
      text: translate(_BookingSummary2.L10N.LABEL_SUBJECT_TO_AVAILABILITY)
    }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
      value: _helpers.ICON.INFO
    })));
  }) : /*#__PURE__*/_react.default.createElement(_ui.Text, {
    light: true,
    small: true
  }, bookingRoomNames.join(', ')), (bookingExtraNames === null || bookingExtraNames === void 0 ? void 0 : bookingExtraNames.length) > 0 && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    light: true,
    small: true
  }, bookingExtraNames.join(', ')), showCancellation && /*#__PURE__*/_react.default.createElement(_ui.View, null, cancellationName && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true,
    bold: true,
    success: true
  }, cancellationName), cancellationCaption && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    light: true,
    small: true
  }, cancellationCaption))), /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true
  }, translate(_BookingSummary2.L10N.LABEL_ACCOMMODATION)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true,
    markdown: false
  }, (_ref12 = "".concat(type && name ? translate(_BookingSummary2.L10N.LABEL_PROPERTY_TYPE, {
    type
  }) : '', " ").concat(name)) === null || _ref12 === void 0 ? void 0 : _ref12.trim()), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    light: true,
    small: true
  }, "".concat(address, ", ").concat(city, ", ").concat(country)), !isCheckout && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    light: true,
    small: true
  }, phone)), !isCheckout && (site || email || urlLocation) && /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    wide: true,
    className: (0, _ui.styles)(style.links, style.noPrint)
  }, site && /*#__PURE__*/_react.default.createElement(_ui.Action, {
    href: site,
    underline: true,
    small: true,
    target: "_blank"
  }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    value: _helpers.ICON.SITE
  }), translate(_BookingSummary2.L10N.ACTION_VIEW_HOTEL)), email && /*#__PURE__*/_react.default.createElement(_ui.Action, {
    href: "mailto:".concat(email),
    underline: true,
    small: true,
    target: "_blank"
  }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    value: _helpers.ICON.MAIL
  }), translate(_BookingSummary2.L10N.ACTION_SEND_EMAIL)), urlLocation && /*#__PURE__*/_react.default.createElement(_ui.Action, {
    href: urlLocation,
    underline: true,
    small: true,
    target: "_blank"
  }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    value: _helpers.ICON.LOCATION
  }), translate(_BookingSummary2.L10N.ACTION_VIEW_MAP))), isCheckout && /*#__PURE__*/_react.default.createElement(_ui.Action, {
    underline: true,
    onPress: handleBack,
    testId: "action-change-selection"
  }, translate(_BookingSummary2.L10N.ACTION_CHANGE_SELECTION))), /*#__PURE__*/_react.default.createElement(_PriceBreakdown.PriceBreakdown, {
    dataSource: dataSource,
    className: style.section
  }), children ? expanded ? /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.section
  }, children) : /*#__PURE__*/_react.default.createElement(_ButtonMore.ButtonMore, {
    onPress: handlePress
  }) : undefined);
};
exports.BookingSummary = BookingSummary;
BookingSummary.displayName = 'Mirai:Core:BookingSummary';
BookingSummary.propTypes = {
  box: _propTypes.default.bool,
  children: _propTypes.default.any,
  dataSource: _propTypes.default.shape({})
};
//# sourceMappingURL=BookingSummary.js.map