"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ModalRoomSelection = 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 _Booking = require("./Booking.RoomCard");
var _Occupation = require("../../__shared__/Occupation/Occupation.constants");
var _Booking2 = require("../Booking.l10n");
var style = _interopRequireWildcard(require("../Booking.module.css"));
var _helpers = require("../helpers");
const _excluded = ["dataSource", "rooms", "title", "visible", "onBack", "onClose", "onNext"];
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 ModalRoomSelection = _ref => {
  var _storeValue$occupatio;
  let {
      dataSource = {},
      rooms: propRooms,
      title,
      visible,
      onBack = () => {},
      onClose = () => {},
      onNext = () => {}
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    isMobile
  } = (0, _ui.useDevice)();
  const {
    translate
  } = (0, _locale.useLocale)();
  const {
    value: storeValue = {}
  } = (0, _dataSources.useStore)() || {};
  const [room, setRoom] = (0, _react.useState)();
  (0, _react.useEffect)(() => {
    if (!visible) return;
    setRoom();
  }, [visible]);
  const {
    details = [],
    modify: {
      occupation: {
        rooms: defaultRooms = []
      } = {}
    } = {}
  } = dataSource;
  const configDataSource = Array.isArray(storeValue === null || storeValue === void 0 ? void 0 : (_storeValue$occupatio = storeValue.occupation) === null || _storeValue$occupatio === void 0 ? void 0 : _storeValue$occupatio.dataSource) ? storeValue.occupation.dataSource : _Occupation.DATASOURCE;
  const rooms = propRooms || defaultRooms;
  const roomDataSource = (0, _react.useMemo)(() => (0, _helpers.getRoomDataSource)({
    configDataSource,
    details,
    occupation: rooms,
    translate
  }), [configDataSource, details, rooms, translate]);
  const totalRooms = (0, _react.useMemo)(() => (0, _helpers.getDetailsByRoom)(details).length || roomDataSource.length, [details, roomDataSource.length]);
  const hasPartialRoomSelection = totalRooms > 1 && roomDataSource.length < totalRooms;
  const {
    testId
  } = others;
  return /*#__PURE__*/_react.default.createElement(_ui.Modal, _extends({}, others, {
    fit: true,
    title: title,
    visible: visible,
    onClose: onClose,
    className: (0, _ui.styles)(style.modalRoomSelection, others.className),
    testId: "".concat(testId, "-modal-room-selector")
  }), /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.panel,
    testId: "".concat(testId, "-room-selector")
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.body
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    paragraph: true
  }, translate(_Booking2.L10N.LABEL_SELECT_ROOM)), /*#__PURE__*/_react.default.createElement(_ui.ScrollView, {
    horizontal: false,
    snap: false,
    className: style.scrollView
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.list
  }, roomDataSource.map(_ref2 => {
    var _roomValue$index;
    let {
      image,
      index,
      occupation,
      plan,
      rate,
      room: roomValue,
      title
    } = _ref2;
    return /*#__PURE__*/_react.default.createElement(_Booking.RoomCard, {
      key: "".concat(roomValue === null || roomValue === void 0 ? void 0 : roomValue.id, "-").concat((_roomValue$index = roomValue === null || roomValue === void 0 ? void 0 : roomValue.index) !== null && _roomValue$index !== void 0 ? _roomValue$index : index),
      selected: (room === null || room === void 0 ? void 0 : room.id) === (roomValue === null || roomValue === void 0 ? void 0 : roomValue.id) && (room === null || room === void 0 ? void 0 : room.index) === (roomValue === null || roomValue === void 0 ? void 0 : roomValue.index),
      onPress: () => setRoom(roomValue),
      testId: "".concat(testId, "-room-").concat(index),
      image,
      occupation,
      plan,
      rate,
      title
    });
  }))), hasPartialRoomSelection && /*#__PURE__*/_react.default.createElement(_ui.Notification, {
    info: true,
    wide: true,
    testId: "".concat(testId, "-room-notification")
  }, translate(_Booking2.L10N.NOTIFICATION_EDIT_PARTIAL_ROOM_SELECTION))), /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.footerActions
  }, isMobile && /*#__PURE__*/_react.default.createElement(_ui.Button, {
    secondary: true,
    onPress: onBack,
    testId: "".concat(testId, "-room-back")
  }, translate(_Booking2.L10N.ACTION_BACK)), /*#__PURE__*/_react.default.createElement(_ui.Button, {
    disabled: room === undefined,
    onPress: () => onNext(room),
    testId: "".concat(testId, "-room-next")
  }, translate(_Booking2.L10N.ACTION_NEXT))))));
};
exports.ModalRoomSelection = ModalRoomSelection;
ModalRoomSelection.displayName = 'Mirai:Core:Booking:ModalRoomSelection';
ModalRoomSelection.propTypes = {
  dataSource: _propTypes.default.shape({
    details: _propTypes.default.arrayOf(_propTypes.default.shape({})),
    modify: _propTypes.default.shape({
      occupation: _propTypes.default.shape({
        rooms: _propTypes.default.arrayOf(_propTypes.default.shape({
          id: _propTypes.default.string,
          maxOccupation: _propTypes.default.shape({
            adults: _propTypes.default.number,
            children: _propTypes.default.number
          }),
          minOccupation: _propTypes.default.shape({
            adults: _propTypes.default.number,
            children: _propTypes.default.number
          })
        }))
      })
    })
  }),
  rooms: _propTypes.default.arrayOf(_propTypes.default.shape({
    id: _propTypes.default.string
  })),
  title: _propTypes.default.string,
  visible: _propTypes.default.bool,
  onBack: _propTypes.default.func,
  onClose: _propTypes.default.func,
  onNext: _propTypes.default.func
};
//# sourceMappingURL=Booking.ModalRoomSelection.js.map