"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.CatalogSearchResultItem = void 0;

var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _CatalogItemIcon = require("./CatalogItemIcon");

var _CatalogSimplePrice = require("./CatalogSimplePrice");

var _Spinner = require("../Spinner/Spinner");

var _Highlighter = require("../Highlighter");

var CatalogSearchResultItem =
/*#__PURE__*/
function (_React$PureComponent) {
  (0, _inheritsLoose2.default)(CatalogSearchResultItem, _React$PureComponent);

  function CatalogSearchResultItem() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getQueryRegex", function () {
      var query = _this.props.query;
      return !!query ? new RegExp(query, 'i') : null;
    });
    return _this;
  }

  var _proto = CatalogSearchResultItem.prototype;

  _proto.renderIcon = function renderIcon() {
    var _this$props = this.props,
        icon = _this$props.icon,
        color = _this$props.color;

    if (_react.default.isValidElement(icon)) {
      return icon;
    }

    return _react.default.createElement(_CatalogItemIcon.CatalogItemIcon, {
      isRound: true,
      size: 40,
      iconName: icon,
      backgroundColor: color
    });
  };

  _proto.renderPrice = function renderPrice() {
    var _this$props2 = this.props,
        price = _this$props2.price,
        isLoading = _this$props2.isLoading;

    if (isLoading) {
      return _react.default.createElement(_Spinner.Spinner, {
        size: 18
      });
    }

    if (!price) {
      return null;
    }

    var pricePurchase = price.pricePurchase,
        priceAnnual = price.priceAnnual,
        priceMonthly = price.priceMonthly,
        isPriceFrom = price.isPriceFrom;
    return _react.default.createElement(_CatalogSimplePrice.CatalogSimplePrice, {
      isAlignRight: true,
      shouldPreserveFromPadding: true,
      pricePurchase: pricePurchase,
      priceAnnual: priceAnnual,
      priceMonthly: priceMonthly,
      isPriceFrom: isPriceFrom
    });
  };

  _proto.renderDescription = function renderDescription() {
    var description = this.props.description;

    if (!description) {
      return null;
    }

    return _react.default.createElement("div", {
      className: "rc-catalog-list-item__text rc-catalog-list-item__description",
      "data-test-automation-id": "description"
    }, _react.default.createElement(_Highlighter.Highlighter, {
      highlightRegexp: this.getQueryRegex()
    }, description));
  };

  _proto.render = function render() {
    var _this$props3 = this.props,
        name = _this$props3.name,
        testAutomationId = _this$props3.testAutomationId;
    return _react.default.createElement("div", {
      className: "rc-catalog-search-result-item",
      "data-test-automation-id": testAutomationId
    }, _react.default.createElement("div", {
      className: "rc-catalog-search-result-item__side-content"
    }, this.renderIcon()), _react.default.createElement("div", {
      className: "rc-catalog-search-result-item__content"
    }, _react.default.createElement("div", {
      className: "rc-catalog-list-item__text rc-catalog-list-item__name",
      "data-test-automation-id": "name"
    }, _react.default.createElement(_Highlighter.Highlighter, {
      highlightRegexp: this.getQueryRegex()
    }, name)), this.renderDescription()), _react.default.createElement("div", {
      className: "rc-catalog-search-result-item__side-content"
    }, this.renderPrice()));
  };

  return CatalogSearchResultItem;
}(_react.default.PureComponent);

exports.CatalogSearchResultItem = CatalogSearchResultItem;
(0, _defineProperty2.default)(CatalogSearchResultItem, "propTypes", {
  name: _propTypes.default.string.isRequired,
  description: _propTypes.default.string,
  price: _propTypes.default.object,
  icon: _propTypes.default.node,
  color: _propTypes.default.string,
  isLoading: _propTypes.default.bool,
  query: _propTypes.default.string,
  testAutomationId: _propTypes.default.string
});
(0, _defineProperty2.default)(CatalogSearchResultItem, "defaultProps", {
  description: null,
  price: null,
  icon: null,
  color: null,
  isLoading: false,
  query: '',
  testAutomationId: 'catalogSearchResultItem'
});