"use strict";

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

exports.__esModule = true;
exports.CatalogListItem = 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 _lodash = require("lodash");

var _reactBootstrap = require("react-bootstrap");

var _functions = require("../../utils/functions");

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

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

var _Icon = require("../Icon");

var CatalogListItem =
/*#__PURE__*/
function (_React$PureComponent) {
  (0, _inheritsLoose2.default)(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;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleClick", function () {
      var onClick = _this.props.onClick;

      if ((0, _lodash.isFunction)(onClick)) {
        onClick();
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleKeyPress", (0, _functions.filterKeyCodes)(_this.handleClick));
    return _this;
  }

  var _proto = CatalogListItem.prototype;

  _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"
    }, (0, _functions.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.default.createElement(_CatalogSimplePrice.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.default.createElement("div", {
      className: "rc-catalog-list-item__arrow"
    }, _react.default.createElement(_Icon.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.default.createElement(_reactBootstrap.Collapse, {
      in: isExpanded,
      "data-test-automation-id": "children"
    }, _react.default.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.default.isValidElement(icon)) {
      return icon;
    }

    return _react.default.createElement(_CatalogItemIcon.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.default.createElement("div", {
      className: "rc-catalog-list-item",
      "data-test-automation-id": testAutomationId
    }, _react.default.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.default.createElement("div", {
      className: "rc-catalog-list-item__main"
    }, _react.default.createElement("div", {
      className: "rc-catalog-list-item__icon"
    }, this.renderIcon()), _react.default.createElement("div", {
      className: "rc-catalog-list-item__content"
    }, _react.default.createElement("div", {
      className: "rc-catalog-list-item__text rc-catalog-list-item__name",
      "data-test-automation-id": "name"
    }, name), this.renderDescription())), _react.default.createElement("div", {
      className: "rc-catalog-list-item__price-container"
    }, this.renderPrice()), this.renderArrow()), this.renderChildren());
  };

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

exports.CatalogListItem = CatalogListItem;
(0, _defineProperty2.default)(CatalogListItem, "propTypes", {
  name: _propTypes.default.string.isRequired,
  description: _propTypes.default.string,
  icon: _propTypes.default.node,
  color: _propTypes.default.string,
  image: _propTypes.default.string,
  price: _propTypes.default.shape({
    pricePurchase: _propTypes.default.object,
    priceAnnual: _propTypes.default.object,
    priceMonthly: _propTypes.default.object,
    isPriceFrom: _propTypes.default.bool
  }),
  isExpanded: _propTypes.default.bool,
  onClick: _propTypes.default.func,
  testAutomationId: _propTypes.default.string
});
(0, _defineProperty2.default)(CatalogListItem, "defaultProps", {
  description: null,
  icon: null,
  color: null,
  image: null,
  price: null,
  isExpanded: false,
  onClick: null,
  testAutomationId: 'catalogListItem'
});