"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Extras = 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 _helpers = require("./helpers");
var _ModalExtras = require("./ModalExtras.l10n");
var style = _interopRequireWildcard(require("./ModalExtras.module.css"));
var _helpers2 = require("../../../helpers");
const _excluded = ["selected"];
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 _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); }
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; }
const Extras = _ref => {
  let _ref$extras = _ref.extras,
    propExtras = _ref$extras === void 0 ? [] : _ref$extras,
    _ref$onChange = _ref.onChange,
    onChange = _ref$onChange === void 0 ? () => {} : _ref$onChange;
  const _useDevice = (0, _ui.useDevice)(),
    isMobile = _useDevice.isMobile,
    isTablet = _useDevice.isTablet,
    width = _useDevice.width;
  const _useLocale = (0, _locale.useLocale)(),
    currencyFormat = _useLocale.currencyFormat,
    translate = _useLocale.translate;
  const _useStore = (0, _dataSources.useStore)(),
    _useStore$value = _useStore.value,
    _useStore$value2 = _useStore$value === void 0 ? {} : _useStore$value,
    currency = _useStore$value2.currency,
    _useStore$value2$find = _useStore$value2.finder,
    finder = _useStore$value2$find === void 0 ? {} : _useStore$value2$find,
    _useStore$value2$hote = _useStore$value2.hotel,
    _useStore$value2$hote2 = _useStore$value2$hote === void 0 ? {} : _useStore$value2$hote,
    hotelAccommodationType = _useStore$value2$hote2.accommodationType,
    _useStore$value2$hote3 = _useStore$value2$hote2.variants,
    variants = _useStore$value2$hote3 === void 0 ? {} : _useStore$value2$hote3;
  const _useState = (0, _react.useState)(propExtras),
    _useState2 = _slicedToArray(_useState, 2),
    extras = _useState2[0],
    setExtras = _useState2[1];
  const handleChange = function () {
    let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      id = _ref2.id,
      field = _ref2.field,
      value = _ref2.value;
    // ! Why parse Number()? should come from the service.
    setExtras(extras.map(extra => _objectSpread(_objectSpread({}, extra), {}, {
      [field]: extra.id === id ? parseInt(value) : extra[field]
    })));
  };
  const handleSelect = id => {
    const next = extras.map(_ref3 => {
      let _ref3$selected = _ref3.selected,
        selected = _ref3$selected === void 0 ? false : _ref3$selected,
        extra = _objectWithoutProperties(_ref3, _excluded);
      return _objectSpread(_objectSpread({}, extra), {}, {
        selected: extra.id === id ? !selected : selected
      });
    });
    setExtras(next);
    onChange(next.filter(_ref4 => {
      let included = _ref4.included,
        selected = _ref4.selected;
      return !!selected || included;
    }));
  };
  const hasSelected = extras.filter(item => !!item.selected).length > 0;
  const _finder$place = finder.place,
    _finder$place2 = _finder$place === void 0 ? {} : _finder$place,
    _finder$place2$id = _finder$place2.id,
    _finder$place2$id2 = _finder$place2$id === void 0 ? [] : _finder$place2$id,
    _finder$place2$id3 = _slicedToArray(_finder$place2$id2, 1),
    id = _finder$place2$id3[0];
  const _ref5 = variants[id] || {},
    _ref5$accommodationTy = _ref5.accommodationType,
    accommodationType = _ref5$accommodationTy === void 0 ? hotelAccommodationType : _ref5$accommodationTy;
  const standaloneCard = extras.length === 1;
  const spaceM = 16;
  const cardWidth = isMobile ? width - spaceM * 4 : isTablet ? width / 2 - spaceM * 2 : 320;
  return /*#__PURE__*/_react.default.createElement(_ui.Slider, {
    visibleItems: isMobile ? 1 : isTablet ? 2 : 2.5,
    width: cardWidth,
    className: style.slider
  }, extras.map(function () {
    let _ref6 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      _ref6$accommodationTh = _ref6.accommodationThreshold,
      accommodationThreshold = _ref6$accommodationTh === void 0 ? 0 : _ref6$accommodationTh,
      _ref6$amount = _ref6.amount,
      amount = _ref6$amount === void 0 ? 0 : _ref6$amount,
      conditions = _ref6.conditions,
      frequency = _ref6.frequency,
      _ref6$guestThreshold = _ref6.guestThreshold,
      guestThreshold = _ref6$guestThreshold === void 0 ? 0 : _ref6$guestThreshold,
      guestType = _ref6.guestType,
      id = _ref6.id,
      image = _ref6.image,
      _ref6$included = _ref6.included,
      included = _ref6$included === void 0 ? false : _ref6$included,
      _ref6$inputGuests = _ref6.inputGuests,
      inputGuests = _ref6$inputGuests === void 0 ? false : _ref6$inputGuests,
      _ref6$inputNights = _ref6.inputNights,
      inputNights = _ref6$inputNights === void 0 ? false : _ref6$inputNights,
      _ref6$nights = _ref6.nights,
      nights = _ref6$nights === void 0 ? 0 : _ref6$nights,
      _ref6$selected = _ref6.selected,
      selected = _ref6$selected === void 0 ? false : _ref6$selected,
      text = _ref6.text,
      title = _ref6.title,
      type = _ref6.type,
      _ref6$value = _ref6.value,
      value = _ref6$value === void 0 ? {} : _ref6$value,
      _ref6$valueAmount = _ref6.valueAmount,
      valueAmount = _ref6$valueAmount === void 0 ? amount : _ref6$valueAmount,
      _ref6$valueNights = _ref6.valueNights,
      valueNights = _ref6$valueNights === void 0 ? nights : _ref6$valueNights;
    const _ref7 = value[valueAmount][valueNights] || {},
      total = _ref7.price;
    return /*#__PURE__*/_react.default.createElement(_ui.View, {
      key: id,
      wide: true,
      className: (0, _ui.styles)(style.card, standaloneCard && style.standalone, hasSelected && selected ? style.selected : undefined),
      style: {
        maxWidth: cardWidth,
        minWidth: cardWidth
      }
    }, /*#__PURE__*/_react.default.createElement(_ui.View, {
      className: style.image,
      style: {
        backgroundImage: "url(".concat(image, ")")
      }
    }), /*#__PURE__*/_react.default.createElement(_ui.View, {
      wide: true,
      className: [style.gap, style.offset, style.anchor]
    }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true
    }, title), text && /*#__PURE__*/_react.default.createElement(_ui.Text, {
      small: true
    }, text), accommodationThreshold > 0 && /*#__PURE__*/_react.default.createElement(_ui.Notification, {
      inline: true,
      small: true,
      warning: true
    }, translate(_ModalExtras.L10N.LABEL_APPLIES_SELECTED_ROOMS, {
      value: accommodationThreshold
    })), guestThreshold > 0 && /*#__PURE__*/_react.default.createElement(_ui.Notification, {
      inline: true,
      small: true,
      warning: true
    }, translate(_ModalExtras.L10N.LABEL_APPLIES_INDIVIDUALS_RESERVATION, {
      value: guestThreshold
    })), conditions && /*#__PURE__*/_react.default.createElement(_ui.Text, {
      light: true,
      small: true
    }, conditions), (inputGuests || inputNights) && /*#__PURE__*/_react.default.createElement(_ui.View, {
      row: true,
      className: (0, _ui.styles)(style.gap, style.inputs)
    }, inputGuests && /*#__PURE__*/_react.default.createElement(_ui.InputSelect, {
      capitalize: true,
      name: "guests",
      label: (0, _helpers2.getGuestLabel)({
        plural: true,
        type: guestType
      }, translate),
      options: Array.from({
        length: amount
      }).map((_, index) => index + 1),
      value: valueAmount === null || valueAmount === void 0 ? void 0 : valueAmount.toString(),
      onChange: value => handleChange({
        id,
        field: 'valueAmount',
        value
      })
    }), inputNights && /*#__PURE__*/_react.default.createElement(_ui.InputSelect, {
      name: "nights",
      label: translate(_ModalExtras.L10N.LABEL_NIGHTS),
      options: Array.from({
        length: nights
      }).map((_, index) => index + 1),
      value: valueNights === null || valueNights === void 0 ? void 0 : valueNights.toString(),
      onChange: value => handleChange({
        id,
        field: 'valueNights',
        value
      })
    }))), /*#__PURE__*/_react.default.createElement(_ui.View, {
      row: true,
      className: [style.offset, style.spaceBetween]
    }, /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      bold: true,
      headline: true
    }, total ? currencyFormat({
      currency,
      maximumFractionDigits: 0,
      minimumFractionDigits: 0,
      value: total
    }) : translate(_ModalExtras.L10N.LABEL_FREE)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      light: true,
      small: true
    }, (0, _helpers.getCaption)({
      accommodationType,
      amount,
      frequency,
      guestType,
      inputGuests,
      type,
      valueAmount,
      valueNights
    }, translate))), /*#__PURE__*/_react.default.createElement(_ui.Button, {
      small: true,
      secondary: !selected && !included,
      onPress: !included ? () => handleSelect(id) : undefined
    }, translate(included ? _ModalExtras.L10N.LABEL_INCLUDED : selected ? _ModalExtras.L10N.ACTION_REMOVE : _ModalExtras.L10N.ACTION_ADD))));
  }));
};
exports.Extras = Extras;
Extras.propTypes = {
  extras: _propTypes.default.any,
  onChange: _propTypes.default.func
};
Extras.displayName = 'Mirai:Core:Rates:Extras';
//# sourceMappingURL=Extras.js.map