"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var React = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _core = require("@material-ui/core");

var _isEqual = _interopRequireDefault(require("lodash/isEqual"));

var _SubscriptionCard = _interopRequireDefault(require("../../bosons/SubscriptionCard"));

var _Dropdown = _interopRequireDefault(require("../../bosons/Dropdown"));

var _Loading = _interopRequireDefault(require("../../bosons/Loading"));

var _withStyles = _interopRequireDefault(require("../../hoc/withStyles"));

var _styles = _interopRequireDefault(require("./styles"));

var _currency = require("./currency");

var _helpers = require("../../helpers");

var _dinero = _interopRequireDefault(require("dinero.js"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var License =
/*#__PURE__*/
function (_React$Component) {
  _inherits(License, _React$Component);

  function License() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, License);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(License)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      isConvertCurrencyDDVisible: false,
      selectedCurrency: "USD",
      justify: _this.props.isVizierRepo ? "flex-start" : "center",
      lg: _this.props.isVizierRepo ? 4 : 5,
      isLoading: false,
      // To store value after calculation
      calculatedSubscriptionPrice: _this.props.subscriptionPrice,
      calculatedSubscriptionRange: _this.props.subscriptionRange,
      component: {
        justifyContent: _this.props.isVizierRepo ? "flex-start" : "center"
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_handleOnChange", function (name, e) {
      _this.props.onConvertCurrencyDDChange(e.value);

      _this._handleConvertCurrencyChange(e.value);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_handleConvertCurrencyChange", function (value) {
      _this.setState({
        isLoading: true
      }, function () {
        var _this$props = _this.props,
            subscriptionPrice = _this$props.subscriptionPrice,
            subscriptionRange = _this$props.subscriptionRange;
        var _this$state = _this.state,
            calculatedSubscriptionRange = _this$state.calculatedSubscriptionRange,
            calculatedSubscriptionPrice = _this$state.calculatedSubscriptionPrice;
        (0, _helpers.getCurrencyBaseAmount)("USD", value).then(function (res) {
          calculatedSubscriptionRange = Math.round(subscriptionRange * res);

          for (var i = 0; i < subscriptionPrice.length; i++) {
            calculatedSubscriptionPrice[i] = Math.round(subscriptionPrice[i] * res);
          }

          _this.setState({
            calculatedSubscriptionRange: calculatedSubscriptionRange,
            calculatedSubscriptionPrice: calculatedSubscriptionPrice,
            selectedCurrency: value,
            isLoading: false
          });
        });
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_renderDropDown", function () {
      var classes = _this.props.classes;
      var _this$state2 = _this.state,
          isConvertCurrencyDDVisible = _this$state2.isConvertCurrencyDDVisible,
          selectedCurrency = _this$state2.selectedCurrency;
      return React.createElement("div", {
        className: classes.cardBtnWrapper
      }, isConvertCurrencyDDVisible && React.createElement(_Dropdown.default, {
        label: "Currency",
        className: classes.currencyDropdown,
        items: _currency.CURRENCY,
        value: selectedCurrency,
        onChange: function onChange(e) {
          _this._handleOnChange("selectedCurrency", e);
        }
      }));
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_onCurrencyBtnClick", function () {
      _this.setState({
        isConvertCurrencyDDVisible: true
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "_renderCard", function () {
      var _this$props2 = _this.props,
          classes = _this$props2.classes,
          currency = _this$props2.currency;
      var _this$state3 = _this.state,
          calculatedSubscriptionRange = _this$state3.calculatedSubscriptionRange,
          calculatedSubscriptionPrice = _this$state3.calculatedSubscriptionPrice,
          justify = _this$state3.justify,
          lg = _this$state3.lg,
          isLoading = _this$state3.isLoading,
          component = _this$state3.component;
      var subscriptionPriceFree = (0, _dinero.default)({
        amount: calculatedSubscriptionPrice[0] * 100,
        currency: currency
      }).toFormat("$0,0");
      var subscriptionPricePaid = (0, _dinero.default)({
        amount: calculatedSubscriptionPrice[1] * 100,
        currency: currency
      }).toFormat("$0,0");
      var subscriptionRange = (0, _dinero.default)({
        amount: calculatedSubscriptionRange * 100,
        currency: currency
      }).toFormat("$0,0");
      return React.createElement(React.Fragment, null, isLoading ? React.createElement(_Loading.default, {
        color: "#000",
        size: 200,
        classes: {
          component: component
        }
      }) : React.createElement(_core.Grid, {
        container: true,
        spacing: 40,
        classes: {
          container: classes.container
        },
        justify: justify
      }, React.createElement(_core.Grid, {
        className: classes.subscriptionCardGrid,
        item: true,
        xs: 12,
        sm: 6,
        md: 6,
        lg: lg
      }, React.createElement(_SubscriptionCard.default, {
        type: "FREE",
        price: subscriptionPriceFree,
        shortDescription: "Kudoo operates on a pay per usage model. If you use Kudoo to invoice for less than ".concat(subscriptionRange, " then Kudoo will be free")
      })), React.createElement(_core.Grid, {
        className: classes.subscriptionCardGrid,
        item: true,
        xs: 12,
        sm: 6,
        md: 6,
        lg: lg
      }, React.createElement(_SubscriptionCard.default, {
        type: "PAID",
        price: subscriptionPricePaid,
        period: "One time fee",
        shortDescription: "If you Invoice your customers for more than ".concat(subscriptionRange, " through Kudoo you will be charged a one off fee")
      }))));
    });

    return _this;
  }

  _createClass(License, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this._handleConvertCurrencyChange(this.props.currency);
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      if (!(0, _isEqual.default)(prevProps.currency, this.props.currency)) {
        this._handleConvertCurrencyChange(this.props.currency);
      }
    }
  }, {
    key: "_renderButton",
    value: function _renderButton() {
      var _this$props3 = this.props,
          isWebsite = _this$props3.isWebsite,
          classes = _this$props3.classes,
          isVizierRepo = _this$props3.isVizierRepo;
      var isConvertCurrencyDDVisible = this.state.isConvertCurrencyDDVisible;
      return React.createElement("div", {
        className: classes.cardBtnWrapper
      }, !isVizierRepo && isWebsite && !isConvertCurrencyDDVisible && React.createElement(_core.Button, {
        className: classes.cardBtn,
        onClick: this._onCurrencyBtnClick,
        variant: "contained"
      }, "Convert Currency"));
    }
  }, {
    key: "render",
    value: function render() {
      return React.createElement(React.Fragment, null, this._renderButton(), this._renderDropDown(), this._renderCard());
    }
  }]);

  return License;
}(React.Component);

_defineProperty(License, "propTypes", {
  isConvertCurrencyBtnVisible: _propTypes.default.bool,
  onConvertCurrencyDDChange: _propTypes.default.func,
  classes: _propTypes.default.any,
  currency: _propTypes.default.string,
  isVizierRepo: _propTypes.default.bool,
  isWebsite: _propTypes.default.bool,
  subscriptionPrice: _propTypes.default.array,
  subscriptionRange: _propTypes.default.number
});

var _default = (0, _withStyles.default)(_styles.default)(License);

exports.default = _default;