import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import { CatalogItemIcon } from './CatalogItemIcon';
import { CatalogSimplePrice } from './CatalogSimplePrice';
import { Spinner } from '../Spinner/Spinner';
import { Highlighter } from '../Highlighter';
export var CatalogSearchResultItem =
/*#__PURE__*/
function (_React$PureComponent) {
  _inheritsLoose(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;

    _defineProperty(_assertThisInitialized(_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.isValidElement(icon)) {
      return icon;
    }

    return React.createElement(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.createElement(Spinner, {
        size: 18
      });
    }

    if (!price) {
      return null;
    }

    var pricePurchase = price.pricePurchase,
        priceAnnual = price.priceAnnual,
        priceMonthly = price.priceMonthly,
        isPriceFrom = price.isPriceFrom;
    return React.createElement(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.createElement("div", {
      className: "rc-catalog-list-item__text rc-catalog-list-item__description",
      "data-test-automation-id": "description"
    }, React.createElement(Highlighter, {
      highlightRegexp: this.getQueryRegex()
    }, description));
  };

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

  return CatalogSearchResultItem;
}(React.PureComponent);

_defineProperty(CatalogSearchResultItem, "propTypes", {
  name: PropTypes.string.isRequired,
  description: PropTypes.string,
  price: PropTypes.object,
  icon: PropTypes.node,
  color: PropTypes.string,
  isLoading: PropTypes.bool,
  query: PropTypes.string,
  testAutomationId: PropTypes.string
});

_defineProperty(CatalogSearchResultItem, "defaultProps", {
  description: null,
  price: null,
  icon: null,
  color: null,
  isLoading: false,
  query: '',
  testAutomationId: 'catalogSearchResultItem'
});