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 { isFunction } from 'lodash';
import { Collapse } from 'react-bootstrap';
import { filterKeyCodes, stripTags } from '../../utils/functions';
import { CatalogItemIcon } from './CatalogItemIcon';
import { CatalogSimplePrice } from './CatalogSimplePrice';
import { Icon } from '../Icon';
export var CatalogListItem =
/*#__PURE__*/
function (_React$PureComponent) {
  _inheritsLoose(CatalogListItem, _React$PureComponent);

  function CatalogListItem() {
    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), "handleClick", function () {
      var onClick = _this.props.onClick;

      if (isFunction(onClick)) {
        onClick();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleKeyPress", filterKeyCodes(_this.handleClick));

    return _this;
  }

  var _proto = CatalogListItem.prototype;

  _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"
    }, stripTags(description));
  };

  _proto.renderPrice = function renderPrice() {
    var price = this.props.price;

    if (!price) {
      return null;
    }

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

  _proto.renderArrow = function renderArrow() {
    var _this$props = this.props,
        children = _this$props.children,
        isExpanded = _this$props.isExpanded;
    var arrow = 'chevron-right-big';

    if (children) {
      if (isExpanded) {
        arrow = 'chevron-up-big';
      } else {
        arrow = 'chevron-down-big';
      }
    }

    return React.createElement("div", {
      className: "rc-catalog-list-item__arrow"
    }, React.createElement(Icon, {
      name: arrow,
      testAutomationId: "arrow"
    }));
  };

  _proto.renderChildren = function renderChildren() {
    var _this$props2 = this.props,
        children = _this$props2.children,
        isExpanded = _this$props2.isExpanded;

    if (!children || !isExpanded) {
      return null;
    }

    return React.createElement(Collapse, {
      in: isExpanded,
      "data-test-automation-id": "children"
    }, React.createElement("div", {
      className: "rc-catalog-list-item__children"
    }, children));
  };

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

    if (React.isValidElement(icon)) {
      return icon;
    }

    return React.createElement(CatalogItemIcon, {
      isRound: true,
      iconName: icon,
      backgroundColor: color
    });
  };

  _proto.render = function render() {
    var _this$props4 = this.props,
        name = _this$props4.name,
        testAutomationId = _this$props4.testAutomationId;
    return React.createElement("div", {
      className: "rc-catalog-list-item",
      "data-test-automation-id": testAutomationId
    }, React.createElement("div", {
      "data-test-automation-id": "header",
      className: "rc-catalog-list-item__header",
      tabIndex: 0,
      role: "button",
      title: name,
      onClick: this.handleClick,
      onKeyDown: this.handleKeyPress
    }, React.createElement("div", {
      className: "rc-catalog-list-item__main"
    }, React.createElement("div", {
      className: "rc-catalog-list-item__icon"
    }, this.renderIcon()), React.createElement("div", {
      className: "rc-catalog-list-item__content"
    }, React.createElement("div", {
      className: "rc-catalog-list-item__text rc-catalog-list-item__name",
      "data-test-automation-id": "name"
    }, name), this.renderDescription())), React.createElement("div", {
      className: "rc-catalog-list-item__price-container"
    }, this.renderPrice()), this.renderArrow()), this.renderChildren());
  };

  return CatalogListItem;
}(React.PureComponent);

_defineProperty(CatalogListItem, "propTypes", {
  name: PropTypes.string.isRequired,
  description: PropTypes.string,
  icon: PropTypes.node,
  color: PropTypes.string,
  image: PropTypes.string,
  price: PropTypes.shape({
    pricePurchase: PropTypes.object,
    priceAnnual: PropTypes.object,
    priceMonthly: PropTypes.object,
    isPriceFrom: PropTypes.bool
  }),
  isExpanded: PropTypes.bool,
  onClick: PropTypes.func,
  testAutomationId: PropTypes.string
});

_defineProperty(CatalogListItem, "defaultProps", {
  description: null,
  icon: null,
  color: null,
  image: null,
  price: null,
  isExpanded: false,
  onClick: null,
  testAutomationId: 'catalogListItem'
});