import _extends from "@babel/runtime/helpers/extends";
import "core-js/modules/es.symbol.js";
import "core-js/modules/es.array.concat.js";
import "core-js/modules/es.array.index-of.js";
import "core-js/modules/es.array.map.js";
import "core-js/modules/es.string.link.js";
var __rest = this && this.__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;
};
import * as React from 'react';
import { Button, Header, Paragraph, PriceBadge, PromoBadge, Tooltip } from '@megafon/ui-core';
import { cnCreate, convert, filterDataAttrs } from '@megafon/ui-helpers';
import { setRelAttribute } from "../../../../helpers/setRelAttribute";
import "./PromoCard.css";
var listItemConvertConfig = {
  b: {
    component: function component(_ref) {
      var children = _ref.children;
      return /*#__PURE__*/React.createElement("b", null, children);
    }
  }
};
var cn = cnCreate('mfui-9-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(PromoBadge, {
          className: cn('badge'),
          dataAttrs: {
            root: dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.badge
          },
          color: promoBadgeColor
        }, promoBadgeText);
      case !!priceBadgeText:
        return (
          /*#__PURE__*/
          // TODO: заменить компонент на InfoBadge после релиза UI-219
          React.createElement(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", _extends({}, 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", _extends({}, 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(Tooltip, _extends({
      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' ? convert(children, {}) : children)));
  };
  return /*#__PURE__*/React.createElement("a", _extends({}, 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: setRelAttribute(linkRel, linkTarget),
    onClick: onClick
  }), !!adMark && renderAdMark(), /*#__PURE__*/React.createElement("img", _extends({
    className: cn('image')
  }, 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", _extends({}, 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(Header, {
    as: "h3",
    className: classes === null || classes === void 0 ? void 0 : classes.title,
    dataAttrs: {
      root: dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.title
    }
  }, convert(title, {})), !!subtitle && /*#__PURE__*/React.createElement("div", {
    className: cn('subtitle')
  }, /*#__PURE__*/React.createElement(Paragraph, {
    hasMargin: false,
    dataAttrs: {
      root: dataAttrs === null || dataAttrs === void 0 ? void 0 : dataAttrs.subtitle
    }
  }, convert(subtitle, {}))), !!list.length && /*#__PURE__*/React.createElement("ul", _extends({}, 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
    }, convert(item, listItemConvertConfig));
  })), /*#__PURE__*/React.createElement("div", {
    className: cn('footer')
  }, /*#__PURE__*/React.createElement(Button, {
    sizeAll: "small",
    type: "text",
    showArrow: true
  }, linkTitle))));
};
export default PromoCard;