"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Header = 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 _components = require("./components");
var _Header = require("./Header.l10n");
var style = _interopRequireWildcard(require("./Header.module.css"));
var _Core = require("../../Core.constants");
var _shared__ = require("../__shared__");
var _helpers = require("../helpers");
var _Session = require("../Session/Session.Account");
var _Session2 = require("../Session/Session.constants");
var _SessionLogin = require("../Session/Session.Login.Modal");
const _excluded = ["currency", "locale", "login", "ghost", "name"];
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 Header = _ref => {
  var _ref2;
  let {
      currency: showCurrency = false,
      locale: showLocale = true,
      login: showLogin = false,
      ghost,
      name: showName
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    isDesktop,
    isMobile
  } = (0, _ui.useDevice)();
  const {
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      club = {},
      finder: {
        place: {
          id: [placeId] = [],
          isHotel,
          title: placeName
        } = {}
      } = {},
      hotel: {
        features = {},
        id,
        url
      } = {},
      id: instanceId,
      language,
      session,
      skeleton = false
    } = {}
  } = (0, _dataSources.useStore)();
  const [modal, setModal] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    const callback = function () {
      let {
        visible = true
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return setModal(visible);
    };
    _dataSources.Event.subscribe(_helpers.EVENT.LOGIN, callback);
    return () => _dataSources.Event.unsubscribe(_helpers.EVENT.LOGIN, callback);
  }, []);
  const handleClose = () => setModal(false);
  const handleLogin = () => {
    const {
      authentication: {
        type = _Core.AUTH_TYPE.MIRAI
      } = {}
    } = club;
    if (type === _Core.AUTH_TYPE.MIRAI) return setModal(true);
    const url = "".concat(process.env.SERVICE_USER, "/").concat(_Session2.ENDPOINT.EXTERNAL_LOGIN).concat((0, _helpers.toUrlParams)({
      idtokenprovider: instanceId,
      domain: document.location.href,
      lang: language
    }));
    document.location.href = url;
  };
  const handleUrl = () => {
    setTimeout(() => window.location = url, _helpers.SUBMIT_DELAY / 2);
  };
  const {
    logo,
    name,
    type
  } = (placeId ? isHotel ? features[placeId] : _objectSpread({
    name: placeName
  }, features[instanceId]) : features[id]) || {};
  const largeName = (name === null || name === void 0 ? void 0 : name.length) > 32;
  const nameProps = {
    action: largeName
  };
  const fullName = (_ref2 = "".concat(type ? translate(_Header.L10N.LABEL_PROPERTY_TYPE, {
    type
  }) : '', " ").concat(name)) === null || _ref2 === void 0 ? void 0 : _ref2.trim();
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.View, {
    tag: "header",
    wide: true,
    testId: others.testId,
    className: (0, _ui.styles)(style.header, others.className)
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    wide: true,
    className: style.content
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.main
  }, skeleton ? /*#__PURE__*/_react.default.createElement(_shared__.Skeleton, {
    bold: true,
    brand: true,
    words: 3
  }) : (logo || ghost || !logo && name) && /*#__PURE__*/_react.default.createElement(_ui.Pressable, {
    onPress: url ? handleUrl : undefined
  }, ghost ? /*#__PURE__*/_react.default.createElement(_ui.View, {
    dangerouslySetInnerHTML: {
      __html: ghost.outerHTML
    }
  }) : logo ? /*#__PURE__*/_react.default.createElement(_ui.View, {
    alt: fullName,
    tag: "img",
    src: logo,
    className: style.image
  }) : /*#__PURE__*/_react.default.createElement(_ui.Text, _extends({}, nameProps, {
    bold: true,
    brand: true,
    className: style.textName
  }), fullName)), isDesktop && /*#__PURE__*/_react.default.createElement(_components.BookingProcess, {
    className: style.noPrint
  })), /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: [style.options, style.noPrint]
  }, showLocale && /*#__PURE__*/_react.default.createElement(_shared__.SelectEnvironment, {
    compacted: isMobile,
    headerMode: true,
    name: "locale"
  }), showCurrency && /*#__PURE__*/_react.default.createElement(_shared__.SelectEnvironment, {
    compacted: isMobile,
    headerMode: true,
    name: "currency"
  }), showLogin && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, session ? /*#__PURE__*/_react.default.createElement(_Session.Account, {
    compacted: true
  }) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.Pressable, {
    onPress: handleLogin,
    className: style.login
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true
  }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    headline: true,
    level: 2,
    value: _helpers.ICON.ACCOUNT
  }), !isMobile && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true,
    medium: true
  }, translate(_Header.L10N.ACTION_LOGIN)))), /*#__PURE__*/_react.default.createElement(_SessionLogin.Modal, {
    visible: modal,
    onClose: handleClose,
    onSuccess: handleClose
  })))))), showName && logo && name && /*#__PURE__*/_react.default.createElement(_ui.View, {
    className: (0, _ui.styles)(style.header, style.extra, others.className)
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    wide: true,
    className: style.content
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, nameProps, fullName))), !isDesktop && /*#__PURE__*/_react.default.createElement(_components.BookingProcess, null));
};
exports.Header = Header;
Header.displayName = 'Mirai:Core:Header';
Header.propTypes = {
  currency: _propTypes.default.bool,
  ghost: _propTypes.default.any,
  locale: _propTypes.default.bool,
  login: _propTypes.default.bool,
  name: _propTypes.default.bool
};
//# sourceMappingURL=Header.js.map