"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Bookings = void 0;
var _dataSources = require("@mirai/data-sources");
var _locale = require("@mirai/locale");
var _services = require("@mirai/services");
var _ui = require("@mirai/ui");
var _react = _interopRequireWildcard(require("react"));
var _Bookings = require("./Bookings.constants");
var _Bookings2 = require("./Bookings.l10n");
var style = _interopRequireWildcard(require("./Bookings.module.css"));
var _Bookings3 = require("./Bookings.Search");
var _Bookings4 = _interopRequireDefault(require("./Bookings.Skeleton"));
var _helpers = require("./helpers");
var _shared__ = require("../../../__shared__");
var _helpers2 = require("../../../helpers");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
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 _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); }
const Bookings = () => {
  const {
    currencyFormat,
    locale,
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      skeleton = false
    } = {}
  } = (0, _dataSources.useStore)();
  const [dataSource, setDataSource] = (0, _react.useState)();
  const [search, setSearch] = (0, _react.useState)();
  (0, _react.useEffect)(() => {
    (async () => {
      // * TODO: We should cache as long as the service responds so slow
      const storage = new _dataSources.Storage({
        adapter: _dataSources.LocalAdapter,
        cache: _Bookings.CACHE_LIMIT
      });
      const cache = storage.get(_Bookings.STORAGE_KEY);
      if (cache) setDataSource(cache);
      const response = await _services.ServiceUser.bookings().catch(() => {});
      storage.set(_Bookings.STORAGE_KEY, response);
      setDataSource(response || []);
    })();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
  const handleManage = url => {
    document.location = url;
  };
  const bookings = (0, _helpers.groupDatasource)(dataSource, search);
  const caption = {
    action: true
  };
  const has = {
    bookings: Object.keys(bookings || {}).length > 0 || search,
    dataSource: dataSource !== undefined
  };
  return !skeleton ? /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: style.container
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.title
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    headline: true,
    level: 2
  }, translate(_Bookings2.L10N.ACTION_MY_BOOKINGS)), has.bookings && /*#__PURE__*/_react.default.createElement(_Bookings3.Search, {
    value: search,
    onChange: setSearch
  })), has.bookings ? Object.entries(bookings || {}).map((_ref, index) => {
    let [id, items = []] = _ref;
    return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.Text, {
      key: index,
      bold: true,
      headline: true
    }, translate({
      id
    }, {
      num: items.length
    })), /*#__PURE__*/_react.default.createElement(_shared__.Cards, null, items.map(function () {
      let {
        booking,
        checkIn,
        checkOut,
        currency,
        hotel,
        image,
        location: {
          city
        } = {},
        price: {
          base,
          total,
          text: taxesText
        } = {},
        status,
        url: {
          manage,
          site
        } = {}
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      const is = {
        cancelled: status === 'CANCELLED',
        past: id === _Bookings2.L10N.LABEL_PAST_BOOKINGS.id
      };
      return /*#__PURE__*/_react.default.createElement(_shared__.Card, {
        key: booking,
        className: style.card
      }, !is.past && /*#__PURE__*/_react.default.createElement(_ui.View, {
        className: style.image,
        style: {
          backgroundImage: "url(".concat(image, ")")
        }
      }), /*#__PURE__*/_react.default.createElement(_ui.View, {
        className: style.offset
      }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
        bold: true,
        headline: true
      }, hotel), checkIn && checkOut && /*#__PURE__*/_react.default.createElement(_ui.Text, caption, (0, _helpers2.getCalendarCaption)([(0, _locale.parseDate)(checkIn), (0, _locale.parseDate)(checkOut)], translate, undefined, locale)), city && /*#__PURE__*/_react.default.createElement(_ui.Text, caption, city), /*#__PURE__*/_react.default.createElement(_ui.Action, _extends({}, caption, {
        href: site,
        underline: true
      }), translate(_Bookings2.L10N.ACTION_VIEW_HOTEL))), /*#__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,
        level: total >= 10000 ? 3 : 2
      }, currencyFormat({
        currency,
        symbol: true,
        value: total
      })), /*#__PURE__*/_react.default.createElement(_ui.Tooltip, {
        text: "".concat(translate(_Bookings2.L10N.LABEL_BASE_PRICE_VALUE, {
          value: currencyFormat({
            currency,
            symbol: true,
            value: base
          })
        }), " ").concat(taxesText)
      }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
        accent: true,
        action: true,
        medium: true
      }, translate(_Bookings2.L10N.LABEL_PRICE_DETAILS)))), !is.cancelled && !is.past && /*#__PURE__*/_react.default.createElement(_ui.Button, {
        onPress: () => handleManage(manage)
      }, translate(_Bookings2.L10N.ACTION_MANAGE_BOOKING)), is.cancelled && /*#__PURE__*/_react.default.createElement(_ui.Button, {
        disabled: true
      }, translate(_Bookings2.L10N.LABEL_CANCELLED))));
    })));
  }) : has.dataSource ? /*#__PURE__*/_react.default.createElement(_ui.Notification, {
    info: true,
    large: true,
    className: style.notification
  }, translate(_Bookings2.L10N.LABEL_NO_BOOKINGS)) : null) : /*#__PURE__*/_react.default.createElement(_Bookings4.default, null);
};
exports.Bookings = Bookings;
Bookings.displayName = 'Mirai:Core:Profile:Bookings';
Bookings.propTypes = {};
//# sourceMappingURL=Bookings.js.map