"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 _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 {
      dataSource = {},
      expanded: propExpanded = false
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    isDesktop
  } = (0, _ui.useDevice)();
  const {
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      config: {
        accomodation: {
          occupation = [],
          variants = []
        } = {}
      } = {},
      hotel: {
        id: hotelId
      } = {},
      urlParams: {
        roomNumberBySelectedRoom
      } = {}
    }
  } = (0, _dataSources.useStore)();
  const [expanded, setExpanded] = (0, _react.useState)(propExpanded);
  (0, _react.useEffect)(() => {
    const {
      details: [{
        items = []
      } = {}] = [],
      extras = []
    } = dataSource;
    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,
    details = [],
    extras = []
  } = dataSource;
  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 {
      amount,
      extras = [],
      image,
      items = [],
      observations = [],
      occupation: {
        adults,
        babies,
        children
      } = {},
      rate,
      type
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    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(_ref2 => {
        let {
          id
        } = _ref2;
        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