"use strict";

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

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

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 _classnames = _interopRequireDefault(require("classnames"));

var PricePropType = {
  price: _propTypes.default.string,
  bonusPrice: _propTypes.default.string,
  discountValue: _propTypes.default.string
};

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

  function CatalogSimplePrice() {
    return _React$PureComponent.apply(this, arguments) || this;
  }

  var _proto = CatalogSimplePrice.prototype;

  _proto.renderPrefix = function renderPrefix(text) {
    var _this$props = this.props,
        shouldPreserveFromPadding = _this$props.shouldPreserveFromPadding,
        isCentered = _this$props.isCentered,
        isAlignRight = _this$props.isAlignRight;

    if (!text && !shouldPreserveFromPadding) {
      return null;
    }

    var className = (0, _classnames.default)('rc-catalog-simple-price__prefix', {
      'rc-catalog-simple-price__prefix--preventFixSize': !shouldPreserveFromPadding || isCentered || isAlignRight
    });
    return _react.default.createElement("span", {
      className: className,
      "data-test-automation-id": "prefix"
    }, text);
  };

  _proto.renderFrom = function renderFrom() {
    var _this$props2 = this.props,
        isPriceFrom = _this$props2.isPriceFrom,
        textFrom = _this$props2.textFrom;
    var text = isPriceFrom ? textFrom : null;
    return this.renderPrefix(text);
  };

  _proto.renderOr = function renderOr() {
    var textOr = this.props.textOr;
    return this.renderPrefix(textOr);
  };

  _proto.renderDiscount = function renderDiscount(_ref) {
    var price = _ref.price,
        bonusPrice = _ref.bonusPrice,
        discountValue = _ref.discountValue;

    if (!discountValue) {
      return null;
    }

    var _this$props3 = this.props,
        textOff = _this$props3.textOff,
        shouldPreserveFromPadding = _this$props3.shouldPreserveFromPadding,
        isCentered = _this$props3.isCentered,
        isAlignRight = _this$props3.isAlignRight;
    var fromPadding = !(isCentered || isAlignRight) && shouldPreserveFromPadding ? this.renderPrefix() : null;
    return _react.default.createElement("div", {
      className: "rc-catalog-simple-price__discount",
      "data-test-automation-id": "discount"
    }, fromPadding, _react.default.createElement("s", {
      className: "rc-catalog-simple-price__old-price"
    }, price), "\xA0", _react.default.createElement("span", {
      className: "rc-catalog-simple-price__discount-amount",
      "data-test-automation-id": "text"
    }, discountValue, " ", textOff));
  };

  _proto.renderPrice = function renderPrice(price, period) {
    if (period === void 0) {
      period = null;
    }

    var bonusPrice = price.bonusPrice;
    var periodEl;

    if (period) {
      periodEl = _react.default.createElement("span", null, "/", _react.default.createElement("span", {
        className: "rc-catalog-simple-price__period"
      }, period));
    }

    return _react.default.createElement("div", {
      className: "rc-catalog-simple-price__price",
      "data-test-automation-id": "price"
    }, bonusPrice, periodEl);
  };

  _proto.renderPriceLineOne = function renderPriceLineOne() {
    var _this$props4 = this.props,
        pricePurchase = _this$props4.pricePurchase,
        priceAnnual = _this$props4.priceAnnual,
        priceMonthly = _this$props4.priceMonthly,
        textPerMonth = _this$props4.textPerMonth,
        textPerYear = _this$props4.textPerYear;
    var price;
    var priceEl;

    if (pricePurchase) {
      price = pricePurchase;
      priceEl = this.renderPrice(pricePurchase);
    } else if (priceAnnual) {
      price = priceAnnual;
      priceEl = this.renderPrice(priceAnnual, textPerYear);
    } else if (priceMonthly) {
      price = priceMonthly;
      priceEl = this.renderPrice(priceMonthly, textPerMonth);
    } else {
      return null;
    }

    return _react.default.createElement(_react.default.Fragment, null, _react.default.createElement("div", {
      className: "rc-catalog-simple-price__line"
    }, this.renderFrom(), priceEl), this.renderDiscount(price));
  };

  _proto.renderPriceLineTwo = function renderPriceLineTwo() {
    var _this$props5 = this.props,
        pricePurchase = _this$props5.pricePurchase,
        priceAnnual = _this$props5.priceAnnual,
        priceMonthly = _this$props5.priceMonthly,
        textPerMonth = _this$props5.textPerMonth,
        textPerYear = _this$props5.textPerYear;
    var price;

    if (pricePurchase && (priceAnnual || priceMonthly)) {
      if (priceAnnual) {
        price = this.renderPrice(priceAnnual, textPerYear);
      } else if (priceMonthly) {
        price = this.renderPrice(priceMonthly, textPerMonth);
      }
    } else if (priceAnnual && priceMonthly) {
      price = this.renderPrice(priceMonthly, textPerMonth);
    } else {
      return null;
    }

    return _react.default.createElement("div", {
      className: "rc-catalog-simple-price__line rc-catalog-simple-price__line--secondary"
    }, this.renderOr(), price);
  };

  _proto.render = function render() {
    var _this$props6 = this.props,
        testAutomationId = _this$props6.testAutomationId,
        isCentered = _this$props6.isCentered,
        isAlignRight = _this$props6.isAlignRight;
    var className = (0, _classnames.default)('rc-catalog-simple-price', {
      'rc-catalog-simple-price--centered': isCentered,
      'rc-catalog-simple-price--right': isAlignRight
    });
    return _react.default.createElement("div", {
      className: className,
      "data-test-automation-id": testAutomationId
    }, this.renderPriceLineOne(), this.renderPriceLineTwo());
  };

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

exports.CatalogSimplePrice = CatalogSimplePrice;
(0, _defineProperty2.default)(CatalogSimplePrice, "propTypes", {
  pricePurchase: _propTypes.default.shape(PricePropType),
  priceAnnual: _propTypes.default.shape(PricePropType),
  priceMonthly: _propTypes.default.shape(PricePropType),
  isPriceFrom: _propTypes.default.bool,
  shouldPreserveFromPadding: _propTypes.default.bool,
  isCentered: _propTypes.default.bool,
  isAlignRight: _propTypes.default.bool,
  testAutomationId: _propTypes.default.string,
  textFrom: _propTypes.default.string,
  textOr: _propTypes.default.string,
  textPerMonth: _propTypes.default.string,
  textPerYear: _propTypes.default.string,
  textOff: _propTypes.default.string
});
(0, _defineProperty2.default)(CatalogSimplePrice, "defaultProps", {
  pricePurchase: null,
  priceAnnual: null,
  priceMonthly: null,
  priceOneTime: null,
  isPriceFrom: false,
  shouldPreserveFromPadding: false,
  isCentered: false,
  isAlignRight: false,
  testAutomationId: 'catalogItemPrice',
  textFrom: 'from',
  textOr: 'or',
  textPerMonth: 'month',
  textPerYear: 'year',
  textOff: 'off'
});