"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Banner = 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 _Banner = require("./Banner.constants");
var _Banner2 = require("./Banner.l10n");
var style = _interopRequireWildcard(require("./Banner.module.css"));
var _Banner3 = _interopRequireDefault(require("./Banner.skeleton"));
var _helpers = require("../helpers");
const _excluded = ["clubConditions"];
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 Banner = _ref => {
  var _itemRef$current;
  let {
      clubConditions
    } = _ref,
    others = _objectWithoutProperties(_ref, _excluded);
  const {
    isDesktop,
    isMobile
  } = (0, _ui.useDevice)();
  const {
    locale: stateLocale,
    translate
  } = (0, _locale.useLocale)();
  const {
    value: {
      banner: storeBanner,
      club = {},
      finder: {
        place
      } = {},
      session,
      skeleton = false
    }
  } = (0, _dataSources.useStore)();
  const [banner, setBanner] = (0, _react.useState)(storeBanner);
  const [clubActive, setClubActive] = (0, _react.useState)(club.active);
  const [clubDiscount, setClubDiscount] = (0, _react.useState)(club.discount);
  const [expanded, setExpanded] = (0, _react.useState)(false);
  const [scrollIndex, setScrollIndex] = (0, _react.useState)(0);
  const itemRef = (0, _react.useRef)();
  (0, _react.useEffect)(() => {
    const callback = nextClubDiscount => setClubDiscount(typeof nextClubDiscount !== 'object' ? nextClubDiscount : undefined);
    _dataSources.Event.subscribe(_helpers.EVENT.RATES_FILTER_CLUB, callback);
    return () => _dataSources.Event.unsubscribe(_helpers.EVENT.RATES_FILTER_CLUB, callback);
  }, []);
  (0, _react.useEffect)(() => {
    if (!place) return;
    const {
      id: [hotelId] = [],
      isHotel
    } = place || {};
    const {
      variants = {}
    } = storeBanner || {};
    const {
      variants: clubVariants = []
    } = club;
    if (hotelId && (Object.keys(variants).length || clubVariants.length)) {
      const nextBanner = variants[isHotel && hotelId] || storeBanner || {};
      const clubVariant = isHotel ? clubVariants === null || clubVariants === void 0 ? void 0 : clubVariants.find(function () {
        let {
          id = ''
        } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        return id.toString() === hotelId.toString();
      }) : club;
      nextBanner && setBanner(_objectSpread(_objectSpread({}, banner), nextBanner));
      setClubActive(clubVariant === null || clubVariant === void 0 ? void 0 : clubVariant.active);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [place]);
  const handleExpanded = () => {
    setExpanded(!expanded);
  };
  const handlePressBenefit = index => {
    setScrollIndex(index);
    _dataSources.Event.publish(_helpers.EVENT.METRICS, {
      id: _helpers.METRICS_EVENTS.RATES_INTERACTION,
      action: 'loyalty_banner_interaction',
      loyalty_interaction_subtype: 'slider_move'
    });
  };
  const handlePressFilter = value => {
    _dataSources.Event.publish(_helpers.EVENT.RATES_FILTER_CLUB, value);
    _dataSources.Event.publish(_helpers.EVENT.METRICS, {
      id: _helpers.METRICS_EVENTS.RATES_INTERACTION,
      action: 'loyalty_banner_interaction',
      loyalty_interaction_subtype: 'discount_toggle',
      loyalty_discount_active: value
    });
  };
  const Filter = () => /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.filter
  }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: true,
    medium: true
  }, translate(_Banner2.L10N.LABEL_ONLY_MEMBERS)), /*#__PURE__*/_react.default.createElement(_ui.Switch, {
    checked: clubDiscount,
    name: "switch",
    onChange: handlePressFilter,
    "aria-label": translate(_Banner2.L10N.LABEL_ONLY_MEMBERS)
  }));
  const {
    benefits = [],
    description = {},
    isClub,
    title,
    url
  } = banner || {};
  const buttonProps = {
    secondary: true,
    squared: true,
    rounded: true
  };
  const lastBenefit = scrollIndex === benefits.length - 4;
  const locale = (0, _helpers.getNormalizedLocale)(stateLocale);
  const shouldRender = clubActive && !session || !isClub && banner;
  return shouldRender && skeleton ? /*#__PURE__*/_react.default.createElement(_Banner3.default, null) : shouldRender && title ? /*#__PURE__*/_react.default.createElement(_ui.View, {
    testId: others.testId,
    className: [style.container, others.className]
  }, /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    className: style.highlight
  }, /*#__PURE__*/_react.default.createElement(_ui.View, null, /*#__PURE__*/_react.default.createElement(_ui.Text, {
    bold: true,
    brand: true,
    headline: true,
    level: isDesktop ? 2 : 3,
    className: style.title
  }, title[locale] || title[_Banner.DEFAULT_LOCALE]), /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: isDesktop,
    small: !isDesktop
  }, description[locale] || description[_Banner.DEFAULT_LOCALE], url && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, ' ', /*#__PURE__*/_react.default.createElement(_ui.Action, {
    bold: true,
    href: url,
    target: "_blank",
    underline: true,
    className: style.textInline
  }, translate(_Banner2.L10N.ACTION_MORE_INFO)))), clubConditions && /*#__PURE__*/_react.default.createElement(_ui.Text, {
    action: isDesktop,
    small: !isDesktop
  }, translate(_Banner2.L10N.LABEL_CLUB_CONDITIONS, {
    conditions: /*#__PURE__*/_react.default.createElement(_ui.Action, {
      href: clubConditions,
      target: "_blank",
      underline: true,
      className: style.textInline
    }, translate(_Banner2.L10N.ACTION_CLUB_VIEW_CONDITIONS))
  }))), isMobile ? /*#__PURE__*/_react.default.createElement(_ui.Button, {
    small: true,
    squared: true,
    transparent: true,
    onPress: handleExpanded
  }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    value: expanded ? _helpers.ICON.EXPAND_LESS : _helpers.ICON.EXPAND_MORE
  })) : clubDiscount !== undefined && /*#__PURE__*/_react.default.createElement(Filter, null)), (!isMobile || expanded) && benefits.length > 0 && /*#__PURE__*/_react.default.createElement(_ui.View, {
    row: true,
    wide: true
  }, !isMobile && scrollIndex !== 0 && /*#__PURE__*/_react.default.createElement(_ui.Button, _extends({}, buttonProps, {
    onPress: () => handlePressBenefit(scrollIndex - 1)
  }), /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    value: _helpers.ICON.LEFT
  })), /*#__PURE__*/_react.default.createElement(isMobile ? _ui.View : _ui.ScrollView, _objectSpread(_objectSpread({}, !isMobile ? {
    horizontal: !isMobile,
    scrollTo: scrollIndex * ((itemRef === null || itemRef === void 0 ? void 0 : (_itemRef$current = itemRef.current) === null || _itemRef$current === void 0 ? void 0 : _itemRef$current.offsetWidth) || _Banner.BENEFIT_MIN_WIDTH)
  } : {}), {}, {
    className: style.list
  }), benefits.map(function () {
    let {
      description = {},
      icon
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    let index = arguments.length > 1 ? arguments[1] : undefined;
    return /*#__PURE__*/_react.default.createElement(_ui.View, {
      ref: index === 0 ? itemRef : undefined,
      row: true,
      key: index
    }, /*#__PURE__*/_react.default.createElement(_ui.Icon, {
      accent: true,
      headline: true,
      level: !isDesktop ? 3 : 2,
      value: _helpers.ICON[icon] || _helpers.ICON.AMENITY
    }), /*#__PURE__*/_react.default.createElement(_ui.Text, {
      action: isDesktop,
      small: !isDesktop
    }, description[locale] || description[_Banner.DEFAULT_LOCALE]));
  })), !isMobile && benefits.length > 4 && /*#__PURE__*/_react.default.createElement(_ui.Button, _extends({}, buttonProps, {
    onPress: () => handlePressBenefit(lastBenefit ? 0 : scrollIndex + 1)
  }), /*#__PURE__*/_react.default.createElement(_ui.Icon, {
    value: lastBenefit ? _helpers.ICON.REPLAY : _helpers.ICON.RIGHT
  }))), isMobile && clubDiscount !== undefined && /*#__PURE__*/_react.default.createElement(Filter, null)) : null;
};
exports.Banner = Banner;
Banner.displayName = 'Mirai:Core:Banner';
Banner.propTypes = {
  clubConditions: _propTypes.default.string
};
//# sourceMappingURL=Banner.js.map