"use strict";

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

exports.__esModule = true;
exports.CatalogListDetails = 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 _reactBootstrap = require("react-bootstrap");

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

var _DangerousHTML = require("../DangerousHTML");

var _Block = require("../Block");

var _Button = require("../Button");

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

var _flexBlock = require("../flexBlock");

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

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

  function CatalogListDetails() {
    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), "ref", _react.default.createRef());
    return _this;
  }

  var _proto = CatalogListDetails.prototype;

  _proto.componentDidMount = function componentDidMount() {
    var shouldScrollIntoView = this.props.shouldScrollIntoView;

    if (shouldScrollIntoView) {
      (0, _functions.scrollEelementIntoView)(this.ref.current);
    }
  };

  _proto.renderAlert = function renderAlert() {
    var alertMessage = this.props.alertMessage;

    if (!alertMessage) {
      return null;
    }

    return _react.default.createElement(_Block.Block, {
      addTopOffset: true,
      addHorizontalOffset: true,
      cancelBottomOffset: true
    }, _react.default.createElement(_reactBootstrap.Alert, {
      bsStyle: "warning",
      "data-test-automation-id": "alertMessage"
    }, _react.default.createElement(_DangerousHTML.DangerousHTML, null, alertMessage)));
  };

  _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("div", {
      className: "rc-catalog-list-details__price-container"
    }, _react.default.createElement(_CatalogSimplePrice.CatalogSimplePrice, {
      shouldPreserveFromPadding: true,
      pricePurchase: pricePurchase,
      priceAnnual: priceAnnual,
      priceMonthly: priceMonthly,
      isPriceFrom: isPriceFrom
    }));
  };

  _proto.renderActionButton = function renderActionButton() {
    var _this$props = this.props,
        action = _this$props.action,
        textPurchase = _this$props.textPurchase,
        isLoading = _this$props.isLoading,
        isActionDisabled = _this$props.isActionDisabled;
    var isDisabled = isLoading || isActionDisabled || typeof action !== 'function';
    var loadingSpinner = isLoading ? _react.default.createElement(_Spinner.Spinner, {
      size: 18
    }) : null;
    return _react.default.createElement(_flexBlock.FlexBlock, {
      align: _flexBlock.FlexBlock.CENTER,
      justify: _flexBlock.FlexBlock.END,
      gutterWidth: 10
    }, loadingSpinner, _react.default.createElement(_Button.Button, {
      bsStyle: "primary",
      onClick: action,
      testAutomationId: "catalogListDetailsButton",
      disabled: isDisabled
    }, textPurchase));
  };

  _proto.render = function render() {
    var _this$props2 = this.props,
        description = _this$props2.description,
        testAutomationId = _this$props2.testAutomationId,
        textDetails = _this$props2.textDetails;
    return _react.default.createElement("section", {
      ref: this.ref,
      className: "rc-catalog-list-details",
      "data-test-automation-id": testAutomationId
    }, _react.default.createElement("div", {
      className: "rc-catalog-list-details__content"
    }, _react.default.createElement("div", {
      className: "rc-catalog-list-details__description-container"
    }, _react.default.createElement("div", {
      className: "rc-catalog-list-details__description-heading"
    }, textDetails), _react.default.createElement("div", {
      className: "rc-catalog-list-details__description",
      "data-test-automation-id": "catalogListDetailsDescription"
    }, _react.default.createElement(_DangerousHTML.DangerousHTML, null, description))), _react.default.createElement("div", {
      className: "rc-catalog-list-details__right"
    }, this.renderPrice(), this.renderAlert())), _react.default.createElement("div", {
      className: "rc-catalog-list-details__actions text-right"
    }, this.renderActionButton()));
  };

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

exports.CatalogListDetails = CatalogListDetails;
(0, _defineProperty2.default)(CatalogListDetails, "propTypes", {
  description: _propTypes.default.string.isRequired,
  action: _propTypes.default.func,
  testAutomationId: _propTypes.default.string,
  textDetails: _propTypes.default.string,
  textPurchase: _propTypes.default.string,
  price: _propTypes.default.shape({
    pricePurchase: _propTypes.default.object,
    priceAnnual: _propTypes.default.object,
    priceMonthly: _propTypes.default.object,
    isPriceFrom: _propTypes.default.bool
  }).isRequired,
  shouldScrollIntoView: _propTypes.default.bool,
  alertMessage: _propTypes.default.string,
  isActionDisabled: _propTypes.default.bool,
  isLoading: _propTypes.default.bool
});
(0, _defineProperty2.default)(CatalogListDetails, "defaultProps", {
  testAutomationId: 'catalogListDetails',
  action: null,
  textDetails: 'Details',
  textPurchase: 'Purchase',
  shouldScrollIntoView: false,
  alertMessage: null,
  isActionDisabled: false,
  isLoading: false,
  isDisabled: false
});