"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.array.concat.js");
require("core-js/modules/es.array.index-of.js");
require("core-js/modules/es.array.map.js");
require("core-js/modules/es.string.link.js");
var React = _interopRequireWildcard(require("react"));
var _uiCore = require("@megafon/ui-core");
var _uiHelpers = require("@megafon/ui-helpers");
var _setRelAttribute = require("../../../../helpers/setRelAttribute");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};
var listItemConvertConfig = {
  b: {
    component: function component(_ref) {
      var children = _ref.children;
      return /*#__PURE__*/React.createElement("b", null, children);
    }
  }
};
var cn = (0, _uiHelpers.cnCreate)('mfui-promo-card');
var PromoCard = function PromoCard(_ref2) {
  var title = _ref2.title,
    subtitle = _ref2.subtitle,
    _ref2$list = _ref2.list,
    list = _ref2$list === void 0 ? [] : _ref2$list,
    link = _ref2.link,
    _ref2$background = _ref2.background,
    background = _ref2$background === void 0 ? 'shadow' : _ref2$background,
    image = _ref2.image,
    logo = _ref2.logo,
    badge = _ref2.badge,
    _ref2$promoBadge = _ref2.promoBadge,
    promoBadge = _ref2$promoBadge === void 0 ? null : _ref2$promoBadge,
    _ref2$priceBadge = _ref2.priceBadge,
    priceBadge = _ref2$priceBadge === void 0 ? null : _ref2$priceBadge,
    _ref2$adMark = _ref2.adMark,
    adMark = _ref2$adMark === void 0 ? null : _ref2$adMark,
    className = _ref2.className,
    classes = _ref2.classes,
    dataAttrs = _ref2.dataAttrs,
    onClick = _ref2.onClick;
  var adTriggerRef = React.useRef(null);
  var imageSrc = image.src,
    imageSrc2x = image.src2x,
    imageAlt = image.alt;
  var linkHref = link.href,
    _link$target = link.target,
    linkTarget = _link$target === void 0 ? '_self' : _link$target,
    linkRel = link.rel,
    linkTitle = link.title;
  var _ref3 = promoBadge || {},
    promoBadgeText = _ref3.text,
    promoBadgeColor = _ref3.color;
  var _ref4 = priceBadge || {},
    priceBadgeText = _ref4.text,
    priceBadgeIcon = _ref4.iconType,
    priceBadgeTheme = _ref4.theme;
  var imageSrcSet = imageSrc2x ? "".concat(imageSrc, ", ").concat(imageSrc2x, " 2x") : undefined;
  var hasBadge = !!promoBadgeText || !!priceBadgeText || !!badge;
  var handleAdTitleClick = React.useCallback(function (e) {
    e.preventDefault();
    e.stopPropagation();
  }, []);
  var renderBadge = React.useMemo(function () {
    switch (true) {
      case !!promoBadgeText:
        return /*#__PURE__*/React.createElement(_uiCore.PromoBadge, {
          className: cn('badge'),
          dataAttrs: {
            root: dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.badge
          },
          color: promoBadgeColor
        }, promoBadgeText);
      case !!priceBadgeText:
        return /*#__PURE__*/React.createElement(_uiCore.PriceBadge, {
          className: cn('badge'),
          dataAttrs: {
            root: dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.badge
          },
          iconType: priceBadgeIcon,
          theme: priceBadgeTheme
        }, priceBadgeText);
      default:
        return /*#__PURE__*/React.createElement("div", (0, _extends2["default"])({}, (0, _uiHelpers.filterDataAttrs)(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.badge), {
          className: cn('badge')
        }), badge);
    }
  }, [dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.badge, promoBadgeText, promoBadgeColor, priceBadgeText, priceBadgeIcon, priceBadgeTheme, badge]);
  var renderAdMark = function renderAdMark() {
    var _ref5 = adMark || {},
      adTitle = _ref5.title,
      adTooltip = _ref5.tooltip;
    var _a = adTooltip || {},
      placement = _a.placement,
      offset = _a.offset,
      triggerEvent = _a.triggerEvent,
      colorTheme = _a.colorTheme,
      children = _a.children,
      restAdTooltip = __rest(_a, ["placement", "offset", "triggerEvent", "colorTheme", "children"]);
    return /*#__PURE__*/React.createElement(React.Fragment, null, !!adTitle && /*#__PURE__*/React.createElement("button", (0, _extends2["default"])({}, (0, _uiHelpers.filterDataAttrs)(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.adTitle), {
      className: cn('ad-title'),
      ref: adTriggerRef,
      onClick: handleAdTitleClick,
      type: "button"
    }), adTitle), !!adTooltip && /*#__PURE__*/React.createElement(_uiCore.Tooltip, (0, _extends2["default"])({
      className: cn('ad-tooltip')
    }, restAdTooltip, {
      placement: placement || 'bottom',
      offset: offset || 'small',
      triggerEvent: triggerEvent || 'hover',
      colorTheme: colorTheme || 'blue',
      triggerElement: adTriggerRef
    }), !!children && /*#__PURE__*/React.createElement("div", {
      className: cn('ad-tooltip-content')
    }, typeof children === 'string' ? (0, _uiHelpers.convert)(children, {}) : children)));
  };
  return /*#__PURE__*/React.createElement("a", (0, _extends2["default"])({}, (0, _uiHelpers.filterDataAttrs)(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.root), {
    className: cn({
      background: background
    }, [className, classes === null || classes === void 0 ? void 0 : classes.root]),
    href: linkHref,
    target: linkTarget,
    rel: (0, _setRelAttribute.setRelAttribute)(linkRel, linkTarget),
    onClick: onClick
  }), !!adMark && renderAdMark(), /*#__PURE__*/React.createElement("img", (0, _extends2["default"])({
    className: cn('image')
  }, (0, _uiHelpers.filterDataAttrs)(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.image), {
    srcSet: imageSrcSet,
    src: imageSrc,
    alt: imageAlt || ''
  })), /*#__PURE__*/React.createElement("div", {
    className: cn('content')
  }, hasBadge && renderBadge, !!(logo === null || logo === void 0 ? void 0 : logo.src) && !hasBadge && /*#__PURE__*/React.createElement("div", (0, _extends2["default"])({}, (0, _uiHelpers.filterDataAttrs)(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.logo), {
    className: cn('logo')
  }), /*#__PURE__*/React.createElement("img", {
    className: cn('logo-img'),
    src: logo.src,
    alt: logo.alt || ''
  })), /*#__PURE__*/React.createElement(_uiCore.Header, {
    as: "h3",
    className: classes === null || classes === void 0 ? void 0 : classes.title,
    dataAttrs: {
      root: dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.title
    }
  }, (0, _uiHelpers.convert)(title, {})), !!subtitle && /*#__PURE__*/React.createElement("div", {
    className: cn('subtitle')
  }, /*#__PURE__*/React.createElement(_uiCore.Paragraph, {
    hasMargin: false,
    dataAttrs: {
      root: dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.subtitle
    }
  }, (0, _uiHelpers.convert)(subtitle, {}))), !!list.length && /*#__PURE__*/React.createElement("ul", (0, _extends2["default"])({}, (0, _uiHelpers.filterDataAttrs)(dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.list), {
    className: cn('list')
  }), list.map(function (item, i) {
    return /*#__PURE__*/React.createElement("li", {
      className: cn('list-item'),
      key: i
    }, (0, _uiHelpers.convert)(item, listItemConvertConfig));
  })), /*#__PURE__*/React.createElement("div", {
    className: cn('footer')
  }, /*#__PURE__*/React.createElement(_uiCore.Button, {
    sizeAll: "small",
    type: "text",
    showArrow: true
  }, linkTitle))));
};
var _default = exports["default"] = PromoCard;