"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BookingDetails = 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 _BookingDetails = require("./BookingDetails.constants");
var _BookingDetails2 = require("./BookingDetails.l10n");
var style = _interopRequireWildcard(require("./BookingDetails.module.css"));
var _ = require("..");
var _helpers = require("../../helpers");
const _excluded = ["dataSource", "expanded"];
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; }
const BookingDetails = _ref => {
  let _ref$dataSource = _ref.dataSource,
    dataSource = _ref$dataSource === void 0 ? {} : _ref$dataSource,
    _ref$expanded = _ref.expanded,
    propExpanded = _ref$expanded === void 0 ? false : _ref$expanded,
    others = _objectWithoutProperties(_ref, _excluded);
  const _useDevice = (0, _ui.useDevice)(),
    isDesktop = _useDevice.isDesktop;
  const _useLocale = (0, _locale.useLocale)(),
    translate = _useLocale.translate;
  const _useStore = (0, _dataSources.useStore)(),
    _useStore$value = _useStore.value,
    _useStore$value$confi = _useStore$value.config,
    _useStore$value$confi2 = _useStore$value$confi === void 0 ? {} : _useStore$value$confi,
    _useStore$value$confi3 = _useStore$value$confi2.accomodation,
    _useStore$value$confi4 = _useStore$value$confi3 === void 0 ? {} : _useStore$value$confi3,
    _useStore$value$confi5 = _useStore$value$confi4.occupation,
    occupation = _useStore$value$confi5 === void 0 ? [] : _useStore$value$confi5,
    _useStore$value$confi6 = _useStore$value$confi4.variants,
    variants = _useStore$value$confi6 === void 0 ? [] : _useStore$value$confi6,
    _useStore$value$hotel = _useStore$value.hotel,
    _useStore$value$hotel2 = _useStore$value$hotel === void 0 ? {} : _useStore$value$hotel,
    hotelId = _useStore$value$hotel2.id,
    _useStore$value$urlPa = _useStore$value.urlParams,
    _useStore$value$urlPa2 = _useStore$value$urlPa === void 0 ? {} : _useStore$value$urlPa,
    roomNumberBySelectedRoom = _useStore$value$urlPa2.roomNumberBySelectedRoom;
  const _useState = (0, _react.useState)(propExpanded),
    _useState2 = _slicedToArray(_useState, 2),
    expanded = _useState2[0],
    setExpanded = _useState2[1];
  (0, _react.useEffect)(() => {
    const _dataSource$details = dataSource.details,
      _dataSource$details2 = _dataSource$details === void 0 ? [] : _dataSource$details,
      _dataSource$details3 = _slicedToArray(_dataSource$details2, 1),
      _dataSource$details3$ = _dataSource$details3[0],
      _dataSource$details3$2 = _dataSource$details3$ === void 0 ? {} : _dataSource$details3$,
      _dataSource$details3$3 = _dataSource$details3$2.items,
      items = _dataSource$details3$3 === void 0 ? [] : _dataSource$details3$3,
      _dataSource$extras = dataSource.extras,
      extras = _dataSource$extras === void 0 ? [] : _dataSource$extras;
    setExpanded(!isDesktop || items.length <= _BookingDetails.MAX_VISIBLE_DETAILS && extras.length === 0);

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  (0, _react.useEffect)(() => {
    setExpanded(propExpanded);
  }, [propExpanded]);
  const handlePress = () => {
    _dataSources.Event.publish(_helpers.EVENT.METRICS, {
      id: _helpers.METRICS_EVENTS.BOOKING_INTERACTION,
      action: 'button_click',
      element_clicked: 'view_more'
    });
    setExpanded(true);
  };
  const currency = dataSource.currency,
    _dataSource$details4 = dataSource.details,
    details = _dataSource$details4 === void 0 ? [] : _dataSource$details4,
    _dataSource$extras2 = dataSource.extras,
    extras = _dataSource$extras2 === void 0 ? [] : _dataSource$extras2;
  const propsTable = {
    currency
  };
  const roomNumber = (0, _helpers.getRoomNumber)(roomNumberBySelectedRoom);
  const multiRoom = details.length > 1;
  return /*#__PURE__*/_react.default.createElement(_ui.View, _extends({}, others, {
    className: (0, _ui.styles)(style.details, others.className)
  }), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: isDesktop
  }, translate(_BookingDetails2.L10N.LABEL_BOOKING_DETAILS)), details.slice(0, expanded ? undefined : 1).map(function () {
    var _variants$find;
    let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      amount = _ref2.amount,
      _ref2$extras = _ref2.extras,
      extras = _ref2$extras === void 0 ? [] : _ref2$extras,
      image = _ref2.image,
      _ref2$items = _ref2.items,
      items = _ref2$items === void 0 ? [] : _ref2$items,
      _ref2$observations = _ref2.observations,
      observations = _ref2$observations === void 0 ? [] : _ref2$observations,
      _ref2$occupation = _ref2.occupation,
      _ref2$occupation2 = _ref2$occupation === void 0 ? {} : _ref2$occupation,
      adults = _ref2$occupation2.adults,
      babies = _ref2$occupation2.babies,
      children = _ref2$occupation2.children,
      rate = _ref2.rate,
      type = _ref2.type;
    let key = arguments.length > 1 ? arguments[1] : undefined;
    return /*#__PURE__*/_react.default.createElement(_ui.View, {
      key: key,
      wide: true
    }, /*#__PURE__*/_react.default.createElement(_ui.View, {
      row: true,
      className: style.item
    }, /*#__PURE__*/_react.default.createElement(_ui.View, {
      className: style.image,
      style: {
        backgroundImage: "url(".concat(image, ")")
      }
    }), /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.View, {
      row: true,
      className: style.inline
    }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true
    }, "".concat(type).concat(roomNumber ? " (".concat(translate(_BookingDetails2.L10N.LABEL_ROOM_NUMBER, {
      roomNumber
    }), ")") : '', " ").concat(amount > 1 ? " (x".concat(amount, ")") : '')), roomNumber && /*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
      pressable: true,
      text: translate(_BookingDetails2.L10N.LABEL_SUBJECT_TO_AVAILABILITY)
    }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
      value: _ui.ICON.INFO
    }))), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      action: true
    }, rate.replace("".concat(type, ":"), '').trim()), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      light: true,
      small: true
    }, (0, _helpers.getRoomOccupationLabel)({
      adults,
      babies,
      children,
      dataSource: ((_variants$find = variants.find(_ref3 => {
        let id = _ref3.id;
        return id === Number(hotelId);
      })) === null || _variants$find === void 0 ? void 0 : _variants$find.occupation) || occupation,
      translate
    })))), /*#__PURE__*/_react.default.createElement(_.BookingTable, _extends({}, propsTable, {
      dataSource: items.slice(0, expanded ? undefined : _BookingDetails.MAX_VISIBLE_DETAILS),
      showTotal: multiRoom && expanded
    })), expanded && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_.BookingTable, _extends({}, propsTable, {
      dataSource: extras,
      label: translate(_BookingDetails2.L10N.LABEL_EXTRAS)
    })), observations.length > 0 && /*#__PURE__*/_react.default.createElement(_ui.View, {
      className: style.observations
    }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true,
      small: true
    }, translate(_BookingDetails2.L10N.LABEL_OBSERVATIONS)), observations.map((item, index) => /*#__PURE__*/_react.default.createElement(_ui.Text, {
      key: index,
      small: true
    }, item)))));
  }), expanded && /*#__PURE__*/_react.default.createElement(_.BookingTable, _extends({}, propsTable, {
    dataSource: extras,
    label: translate(_BookingDetails2.L10N.LABEL_BOOKING_EXTRAS)
  })), !expanded && /*#__PURE__*/_react.default.createElement(_.ButtonMore, {
    onPress: handlePress,
    className: style.buttonMore,
    testId: "button-more"
  }));
};
exports.BookingDetails = BookingDetails;
BookingDetails.displayName = 'Mirai:Core:BookingDetails';
BookingDetails.propTypes = {
  dataSource: _propTypes.default.shape({}),
  expanded: _propTypes.default.bool
};
//# sourceMappingURL=BookingDetails.js.map