"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 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); }
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 BookingSummary = _ref => {
  var _ref2;
  let {
      box = false,
      children,
      dataSource = {}
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    isDesktop
  } = (0, _ui.useDevice)();
  const {
    dateFormat,
    translate
  } = (0, _locale.useLocale)();
  const _useStore = (0, _dataSources.useStore)(),
    {
      value: {
        components = {},
        finder: {
          promocode = ''
        } = {},
        hotel: {
          features = {},
          id
        } = {},
        language,
        urlParams: {
          roomNumberBySelectedRoom
        } = {},
        variant
      }
    } = _useStore,
    urlParams = _objectWithoutProperties(_useStore.value.urlParams, _excluded2);
  const [expanded, setExpanded] = (0, _react.useState)(isDesktop);
  const {
    checkIn,
    checkInTime,
    checkOut,
    checkOutTime,
    currency,
    details = [],
    hotel: {
      address,
      city,
      country,
      email,
      location = [],
      name: hotelName = '',
      phone,
      site
    } = {},
    nights
  } = dataSource;
  const handleBack = () => {
    setTimeout(() => {
      document.location.href = (0, _helpers.getRatesUrl)(_objectSpread({
        currency,
        language,
        nights,
        promocode,
        variant
      }, urlParams));
    }, [_helpers.SUBMIT_DELAY]);
  };
  const handlePress = () => {
    setExpanded(true);
  };
  const {
    days
  } = (0, _locale.dateDiff)((0, _locale.parseDate)(checkIn), (0, _locale.parseDate)(checkOut));
  const isCheckout = !!components.checkout;
  const items = details.filter(function () {
    let {
      type
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return !!type;
  }) || [];
  const roomNumber = (0, _helpers.getRoomNumber)(roomNumberBySelectedRoom);
  const urlLocation = location.length ? "https://maps.google.com?q=".concat(location.join(',')) : undefined;
  const {
    name = hotelName,
    type
  } = features[id] || {};
  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_RESERVATION)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, "".concat(translate(_BookingSummary2.L10N.LABEL_NIGHT_COUNT, {
    value: nights || days || 0
  }), ", ").concat(translate(_BookingSummary2.L10N.LABEL_ROOM_COUNT, {
    value: items.reduce(function (total) {
      let {
        amount = 1
      } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      return total + amount;
    }, 0)
  }))), items.map(function () {
    let {
      amount,
      type
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    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.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
  }, (_ref2 = "".concat(type && name ? translate(_BookingSummary2.L10N.LABEL_PROPERTY_TYPE, {
    type
  }) : '', " ").concat(name)) === null || _ref2 === void 0 ? void 0 : _ref2.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