"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.RoomSelector = 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 _Rates = require("./Rates.l10n");
var style = _interopRequireWildcard(require("./Rates.module.css"));
var _helpers = require("../helpers");
const _excluded = ["cart", "occupation", "room", "onPress"];
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 RoomSelector = _ref => {
  var _itemsRef$current, _itemsRef$current2, _itemsRef$current2$, _itemsRef$current2$$p;
  let {
      cart = [],
      occupation = [],
      room: roomIndex,
      onPress = () => {}
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    isMobile
  } = (0, _ui.useDevice)();
  const {
    translate
  } = (0, _locale.useLocale)();
  const itemsRef = (0, _react.useRef)([]);
  const {
    value: {
      finder = {},
      hotel: {
        accommodationType: hotelAccommodationType,
        variants = {}
      } = {}
    }
  } = (0, _dataSources.useStore)();
  const [scrollTo, setScrollTo] = (0, _react.useState)();
  (0, _react.useEffect)(() => {
    setScrollTo(0);
  }, []);
  (0, _react.useEffect)(() => {
    var _itemsRef$current$roo;
    const next = roomIndex === 0 ? 0 : (_itemsRef$current$roo = itemsRef.current[roomIndex]) === null || _itemsRef$current$roo === void 0 ? void 0 : _itemsRef$current$roo.offsetLeft;
    if (next) setScrollTo(next - (isMobile ? 8 : 0));
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [roomIndex]);
  const handlePress = index => {
    onPress(index);
  };
  const {
    place: {
      id: [id] = [],
      isHotel = true
    } = {}
  } = finder;
  const {
    accommodationType = hotelAccommodationType
  } = variants[id] || {};
  const itemsWidth = (itemsRef === null || itemsRef === void 0 ? void 0 : (_itemsRef$current = itemsRef.current) === null || _itemsRef$current === void 0 ? void 0 : _itemsRef$current.reduce((total, current) => total + (current === null || current === void 0 ? void 0 : current.offsetWidth) || 0, 0)) + (8 * occupation.length - 1);
  const containerWidth = (itemsRef === null || itemsRef === void 0 ? void 0 : (_itemsRef$current2 = itemsRef.current) === null || _itemsRef$current2 === void 0 ? void 0 : (_itemsRef$current2$ = _itemsRef$current2[0]) === null || _itemsRef$current2$ === void 0 ? void 0 : (_itemsRef$current2$$p = _itemsRef$current2$.parentElement) === null || _itemsRef$current2$$p === void 0 ? void 0 : _itemsRef$current2$$p.offsetWidth) || 0;
  return isHotel && occupation.length >= 2 ? /*#__PURE__*/_react.default.createElement(_ui.ScrollView, _extends({}, others, {
    defaultScrollIndicator: true,
    horizontal: true,
    scrollIndicator: !isMobile && itemsWidth > containerWidth,
    scrollTo: scrollTo,
    snap: false,
    className: (0, _ui.styles)(style.roomSelector, others.className)
  }), occupation.map(function () {
    let items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    let index = arguments.length > 1 ? arguments[1] : undefined;
    const guests = items.reduce(function (total) {
      let {
        amount
      } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      return total + amount;
    }, 0);
    const hasCart = index <= roomIndex || cart[index] !== undefined || cart[index - 1] !== undefined;
    return /*#__PURE__*/_react.default.createElement(_ui.View, {
      key: index,
      ref: el => itemsRef.current[index] = el
    }, /*#__PURE__*/_react.default.createElement(_ui.Button, {
      disabled: !hasCart,
      secondary: hasCart && index !== roomIndex,
      onPress: () => handlePress(index)
    }, "".concat(index === roomIndex && !cart[index] ? "".concat(translate(_Rates.L10N.LABEL_SELECT)) : '', " ").concat(translate(_Rates.L10N.LABEL_ACCOMMODATION_TYPE, {
      type: accommodationType
    }), " ").concat(index + 1).trim(), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      headline: true,
      className: [style.separator, index === roomIndex ? style.active : hasCart ? style.secondary : style.disabled]
    }, "|"), /*#__PURE__*/_react.default.createElement(_ui.Icon, {
      headline: true,
      level: 2,
      value: guests === 1 || guests > 3 ? _helpers.ICON.PERSON : guests === 2 ? _helpers.ICON.GROUP : _helpers.ICON.GROUPS
    }), guests));
  })) : null;
};
exports.RoomSelector = RoomSelector;
RoomSelector.displayName = 'Mirai:Core:Rates:RoomSelector';
RoomSelector.propTypes = {
  cart: _propTypes.default.arrayOf(_propTypes.default.shape({})),
  occupation: _propTypes.default.arrayOf(_propTypes.default.arrayOf(_propTypes.default.shape({
    ages: _propTypes.default.arrayOf(_propTypes.default.number),
    amount: _propTypes.default.number,
    type: _propTypes.default.number
  }))),
  room: _propTypes.default.number,
  onPress: _propTypes.default.func
};
//# sourceMappingURL=Rates.RoomSelector.js.map