"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FormEditOccupation = void 0;
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 _hooks = require("./hooks");
var _helpers = require("../../helpers");
var _Booking2 = require("../Booking.l10n");
var style = _interopRequireWildcard(require("../Booking.module.css"));
var _helpers2 = require("../helpers");
const _excluded = ["dataSource", "responseOccupation", "onChange"];
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 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 _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 {
  ADULT,
  CHILD
} = _helpers.OCCUPATION_TYPE;
const isRoomMatch = (id, room) => id === room;
const getSelectedRoomDetail = function () {
  let {
    details = [],
    room
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  if (!room) return;
  const detailsByRoom = (0, _helpers2.getDetailsByRoom)(details);
  const roomIndex = room.index;
  return roomIndex !== undefined ? detailsByRoom[roomIndex] : detailsByRoom.find(function () {
    let {
      id
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return id !== undefined && isRoomMatch(id, room.id);
  });
};
const getOccupationText = function () {
  let occupation = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  let translate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : () => {};
  const labels = [occupation.adults ? (0, _helpers.getOccupationLabel)({
    amount: occupation.adults,
    type: ADULT
  }, translate) : undefined, occupation.children ? (0, _helpers.getOccupationLabel)({
    amount: occupation.children,
    type: CHILD
  }, translate) : undefined].filter(Boolean);
  return labels.join(', ');
};
const FormEditOccupation = _ref => {
  let {
      dataSource = {},
      responseOccupation = {},
      onChange = () => {}
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    isDesktop
  } = (0, _ui.useDevice)();
  const {
    currencyFormat,
    translate
  } = (0, _locale.useLocale)();
  const {
    details = [],
    modify: {
      occupation: {
        rooms = []
      } = {}
    } = {},
    price = {},
    currency
  } = dataSource;
  const {
    selectedRoomData: {
      image,
      occupation,
      plan,
      rate,
      title
    } = {},
    showSelectedRoom
  } = (0, _hooks.useSelectedRoomData)({
    details,
    room: responseOccupation.room,
    rooms
  });
  const viewData = (0, _react.useMemo)(() => {
    var _getSelectedRoomDetai, _responseOccupation$p;
    const currentOccupation = (_getSelectedRoomDetai = getSelectedRoomDetail({
      details,
      room: responseOccupation.room
    })) === null || _getSelectedRoomDetai === void 0 ? void 0 : _getSelectedRoomDetai.occupation;
    const nextOccupation = (0, _helpers2.getOccupationFromParty)(responseOccupation.party);
    const currentPrice = price.value;
    const nextPrice = (_responseOccupation$p = responseOccupation.price) === null || _responseOccupation$p === void 0 ? void 0 : _responseOccupation$p.value;
    return {
      currency: responseOccupation.currency || currency,
      current: currentOccupation,
      currentPrice,
      next: nextOccupation,
      nextPrice,
      option: responseOccupation.option
    };
  }, [currency, details, price.value, responseOccupation]);
  (0, _react.useEffect)(() => {
    var _responseOccupation$r;
    const roomId = (_responseOccupation$r = responseOccupation.room) === null || _responseOccupation$r === void 0 ? void 0 : _responseOccupation$r.id;
    onChange(_objectSpread({
      option: viewData.option,
      price: viewData.nextPrice
    }, roomId !== undefined ? {
      roomId
    } : undefined));
  }, [onChange, responseOccupation === null || responseOccupation === void 0 ? void 0 : responseOccupation.room, viewData.nextPrice, viewData.option]);
  const currentOccupationText = getOccupationText(viewData.current, translate);
  const nextOccupationText = getOccupationText(viewData.next, translate);
  const nextOccupationLines = nextOccupationText.split(', ').filter(Boolean);
  const comparison = /*#__PURE__*/_react.default.createElement(_ui.View, {
    wide: true,
    className: (0, _ui.styles)(style.summaryContainer, style.occupationComparison)
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: (0, _ui.styles)(style.column, style.current),
    testId: "booking-current-occupation-summary"
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: isDesktop
  }, translate(_Booking2.L10N.LABEL_CURRENT_OCCUPATION)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, currentOccupationText), /*#__PURE__*/_react.default.createElement(_ui.View, {
    wide: true
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true
  }, translate(_Booking2.L10N.LABEL_TOTAL)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: true
  }, currencyFormat({
    currency: viewData.currency,
    value: viewData.currentPrice
  })), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    small: true
  }, translate(_Booking2.L10N.LABEL_INCLUDES_TAXES)))), /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.column,
    testId: "booking-new-occupation-summary"
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: isDesktop
  }, translate(_Booking2.L10N.LABEL_NEW_OCCUPATION)), /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.highlights
  }, nextOccupationLines.length > 0 ? nextOccupationLines.map(line => /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    key: line,
    className: style.highlight
  }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    success: true,
    value: _helpers.ICON.SUCCESS
  }), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    success: true
  }, line))) : /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true
  }, nextOccupationText)), /*#__PURE__*/_react.default.createElement(_ui.View, {
    wide: true
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true
  }, translate(_Booking2.L10N.LABEL_TOTAL)), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: true
  }, currencyFormat({
    currency: viewData.currency,
    value: viewData.nextPrice
  })), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    small: true
  }, translate(_Booking2.L10N.LABEL_INCLUDES_TAXES)))), (responseOccupation === null || responseOccupation === void 0 ? void 0 : responseOccupation.adjustDifferenceAtProperty) && /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.adjustDifferent
  }, /*#__PURE__*/_react.default.createElement(_ui.Notification, {
    info: true,
    wide: true
  }, translate(_Booking2.L10N.NOTIFICATION_ADJUST_DIFFERENT))));
  return /*#__PURE__*/_react.default.createElement(_ui.View, _extends({}, others, {
    wide: true,
    className: (0, _ui.styles)(showSelectedRoom && style.formEditOccupation, others.className)
  }), showSelectedRoom && /*#__PURE__*/_react.default.createElement(_Booking.RoomCard, {
    image,
    occupation,
    plan,
    rate,
    title,
    className: style.selectedRoomCard,
    testId: "booking-edit-occupation-room-card"
  }), comparison);
};
exports.FormEditOccupation = FormEditOccupation;
FormEditOccupation.displayName = 'Mirai:Core:Booking:FormEditOccupation';
FormEditOccupation.propTypes = {
  dataSource: _propTypes.default.shape({
    currency: _propTypes.default.string,
    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
        }))
      })
    }),
    price: _propTypes.default.shape({
      value: _propTypes.default.number
    })
  }),
  responseOccupation: _propTypes.default.shape({
    adjustDifferenceAtProperty: _propTypes.default.bool,
    room: _propTypes.default.shape({
      id: _propTypes.default.string,
      index: _propTypes.default.number
    })
  }),
  onChange: _propTypes.default.func
};
//# sourceMappingURL=Booking.FormEditOccupation.js.map