"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Menu = 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 _Booking = require("../Booking.constants");
var _Booking2 = require("../Booking.l10n");
var style = _interopRequireWildcard(require("../Booking.module.css"));
const _excluded = ["dataSource", "onPress", "onVisibleChange", "visible"];
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 Menu = _ref => {
  let {
      dataSource = {},
      onPress = () => {},
      onVisibleChange = () => {},
      visible
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    isDesktop
  } = (0, _ui.useDevice)();
  const {
    translate
  } = (0, _locale.useLocale)();
  const refContainer = (0, _react.useRef)();
  (0, _react.useEffect)(() => {
    onVisibleChange(isDesktop || false);
    !isDesktop && (refContainer === null || refContainer === void 0 ? void 0 : refContainer.current) && _dataSources.Event.publish(_helpers.EVENT.FOOTER_HEIGHT_CHANGE, {
      height: refContainer === null || refContainer === void 0 ? void 0 : refContainer.current.offsetHeight
    });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isDesktop]);
  const handlePress = action => {
    if (!isDesktop) {
      onVisibleChange(false);
    }
    if (!action) return;
    if (action === _Booking.ACTION.CANCEL) {
      _dataSources.Event.publish(_helpers.EVENT.METRICS, {
        id: _helpers.METRICS_EVENTS.BOOKING_INTERACTION,
        action: 'cancel_intent'
      });
    }
    if (action === _Booking.ACTION.SHOW_CALENDAR) {
      _dataSources.Event.publish(_helpers.EVENT.METRICS, {
        id: _helpers.METRICS_EVENTS.BOOKING_INTERACTION,
        action: 'modify_dates_intent'
      });
    }
    onPress(action);
  };
  const {
    isPostponable = false,
    modify = {},
    state = _Booking.STATE.CONFIRMED
  } = dataSource;
  const {
    board,
    date = false,
    occupation
  } = modify;
  const boardRooms = Array.isArray(board === null || board === void 0 ? void 0 : board.rooms) ? board.rooms : [];
  const occupationRooms = Array.isArray(occupation === null || occupation === void 0 ? void 0 : occupation.rooms) ? occupation.rooms : [];
  const is = {
    cancelled: state === _Booking.STATE.CANCELLED,
    confirmed: state === _Booking.STATE.CONFIRMED,
    modifiedBoard: state === _Booking.STATE.CONFIRMED && boardRooms.length > 0,
    modifiedDate: state === _Booking.STATE.CONFIRMED && date,
    modifiedOccupation: state === _Booking.STATE.CONFIRMED && occupationRooms.length > 0,
    postponable: state === _Booking.STATE.CONFIRMED && isPostponable
  };
  return /*#__PURE__*/_react.default.createElement(_ui.View, _extends({
    ref: refContainer
  }, others, {
    className: (0, _ui.styles)(style.menu, style.noPrint, others.className)
  }), /*#__PURE__*/_react.default.createElement(_ui.Menu, {
    options: [{
      icon: _helpers.ICON.PRINT,
      label: translate(_Booking2.L10N.ACTION_PRINT),
      value: _Booking.ACTION.PRINT,
      divider: true
    }, {
      icon: _helpers.ICON.MAIL,
      label: translate(_Booking2.L10N.ACTION_RESEND_BOOKING),
      value: _Booking.ACTION.RESEND,
      divider: true
    }, is.postponable ? {
      icon: _helpers.ICON.CALENDAR_EDIT,
      label: translate(_Booking2.L10N.ACTION_EDIT_BOOKING),
      value: _Booking.ACTION.EDIT,
      divider: true
    } : undefined, is.confirmed ? {
      icon: _helpers.ICON.CALENDAR_CANCEL,
      label: translate(_Booking2.L10N.ACTION_CANCEL_BOOKING),
      value: _Booking.ACTION.CANCEL,
      divider: is.modifiedDate
    } : undefined, is.modifiedDate ? {
      icon: _helpers.ICON.CALENDAR_EDIT,
      label: translate(_Booking2.L10N.ACTION_EDIT_DATE),
      value: _Booking.ACTION.SHOW_CALENDAR,
      divider: is.modifiedOccupation || is.modifiedBoard
    } : undefined, is.modifiedOccupation ? {
      icon: _helpers.ICON.GROUP,
      label: translate(_Booking2.L10N.ACTION_EDIT_OCCUPATION),
      value: _Booking.ACTION.EDIT_OCCUPATION,
      divider: is.modifiedBoard
    } : undefined, is.modifiedBoard ? {
      icon: _helpers.ICON.LIST,
      label: translate(_Booking2.L10N.ACTION_EDIT_BOARD),
      value: _Booking.ACTION.EDIT_BOARD
    } : undefined],
    visible: visible,
    onPress: handlePress,
    testId: "booking-menu"
  }), !isDesktop && /*#__PURE__*/_react.default.createElement(_ui.Button, {
    large: true,
    wide: true,
    onPress: () => {
      onVisibleChange(!visible);
    },
    testId: "show-booking-menu"
  }, translate(_Booking2.L10N.ACTION_SHOW_OPTIONS)));
};
exports.Menu = Menu;
Menu.displayName = 'Mirai:Core:Booking:Menu';
Menu.propTypes = {
  dataSource: _propTypes.default.shape({
    state: _propTypes.default.oneOf(Object.values(_Booking.STATE))
  }),
  onPress: _propTypes.default.func,
  onVisibleChange: _propTypes.default.func,
  visible: _propTypes.default.bool
};
//# sourceMappingURL=Booking.Menu.js.map