'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CartRewardCard = undefined;

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _radium = require('radium');

var _radium2 = _interopRequireDefault(_radium);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _marger = require('kitten/components/layout/marger');

var _garbageIcon = require('kitten/components/icons/garbage-icon');

var _buttonIcon = require('kitten/components/buttons/button-icon');

var _title = require('kitten/components/typography/title');

var _text = require('kitten/components/typography/text');

var _paragraph = require('kitten/components/typography/paragraph');

var _colorsConfig = require('kitten/constants/colors-config');

var _colorsConfig2 = _interopRequireDefault(_colorsConfig);

var _screenConfig = require('kitten/constants/screen-config');

var _mediaQueries = require('kitten/hoc/media-queries');

var _elementHelper = require('kitten/helpers/dom/element-helper');

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

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; }

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

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

var Marger = (0, _radium2.default)(_marger.Marger);

var CartRewardCardComponent = function (_Component) {
  _inherits(CartRewardCardComponent, _Component);

  function CartRewardCardComponent(props) {
    _classCallCheck(this, CartRewardCardComponent);

    var _this = _possibleConstructorReturn(this, (CartRewardCardComponent.__proto__ || Object.getPrototypeOf(CartRewardCardComponent)).call(this, props));

    _this.handleCloseClick = function () {
      if (_this.props.onCloseClick) {
        _this.props.onCloseClick();
      } else {
        _this.close();
      }
    };

    _this.handleAnimationEnd = function () {
      _this.props.onAfterClose();
    };

    _this.close = function () {
      _this.setState({
        isHidden: true,

        // The css animation on the garbage button requires a fixed height.
        height: _elementHelper.domElementHelper.getComputedHeight(_this.container)
      });
    };

    _this.state = {
      isHidden: false,
      height: 'auto'
    };
    return _this;
  }

  _createClass(CartRewardCardComponent, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      var cartRewardStyles = [styles.card, this.state.isHidden && styles.card.hidden, { height: this.state.height }];

      return _react2.default.createElement(
        _radium.StyleRoot,
        null,
        _react2.default.createElement(
          'div',
          {
            ref: function ref(div) {
              _this2.container = div;
            },
            style: cartRewardStyles,
            onAnimationEnd: this.handleAnimationEnd
          },
          this.renderGarbage(),
          this.renderDescription()
        )
      );
    }
  }, {
    key: 'renderDescription',
    value: function renderDescription() {
      var _props = this.props,
          titleTag = _props.titleTag,
          titleAmount = _props.titleAmount,
          subtitle = _props.subtitle,
          textDescription = _props.textDescription,
          viewportIsMobile = _props.viewportIsMobile;


      return _react2.default.createElement(
        Marger,
        { bottom: '4', style: styles.description },
        _react2.default.createElement(
          Marger,
          { bottom: viewportIsMobile && !subtitle ? 1 : 2 },
          _react2.default.createElement(
            _title.Title,
            { italic: true, modifier: 'quinary', margin: false, tag: titleTag },
            titleAmount
          )
        ),
        subtitle && _react2.default.createElement(
          Marger,
          { top: '2', bottom: '1' },
          _react2.default.createElement(
            _text.Text,
            { weight: 'bold', size: 'default' },
            subtitle
          )
        ),
        _react2.default.createElement(
          Marger,
          { top: viewportIsMobile && !subtitle ? 1 : 2, bottom: '2' },
          _react2.default.createElement(
            _paragraph.Paragraph,
            { margin: false, modifier: 'quaternary' },
            textDescription
          )
        ),
        this.renderBottomContent()
      );
    }
  }, {
    key: 'renderGarbage',
    value: function renderGarbage() {
      return _react2.default.createElement(
        'div',
        { style: styles.cart },
        _react2.default.createElement(
          _buttonIcon.ButtonIcon,
          {
            type: 'button',
            title: 'Garbage Button',
            'aria-label': 'Garbage Button',
            modifier: 'beryllium',
            size: 'tiny',
            onClick: this.handleCloseClick,
            style: styles.garbage
          },
          _react2.default.createElement(_garbageIcon.GarbageIcon, { className: 'k-ButtonIcon__svg' })
        )
      );
    }
  }, {
    key: 'renderBottomContent',
    value: function renderBottomContent() {
      var _props2 = this.props,
          shippingTitle = _props2.shippingTitle,
          shippingValue = _props2.shippingValue,
          updateAmountTitle = _props2.updateAmountTitle,
          updateAmountLink = _props2.updateAmountLink;


      return _react2.default.createElement(
        _react.Fragment,
        null,
        shippingTitle && shippingValue && _react2.default.createElement(
          Marger,
          { top: '2' },
          _react2.default.createElement(
            _text.Text,
            { weight: 'regular', size: 'tiny' },
            shippingTitle,
            ' ',
            _react2.default.createElement(
              _text.Text,
              { weight: 'light', size: 'tiny' },
              shippingValue
            )
          )
        ),
        updateAmountTitle && _react2.default.createElement(
          Marger,
          { top: '2' },
          _react2.default.createElement(
            _text.Text,
            {
              tag: 'a',
              href: updateAmountLink,
              color: 'primary1',
              decoration: 'none',
              weight: 'regular',
              size: 'tiny'
            },
            updateAmountTitle
          )
        )
      );
    }
  }]);

  return CartRewardCardComponent;
}(_react.Component);

CartRewardCardComponent.propTypes = {
  titleAmount: _propTypes2.default.string.isRequired,
  textDescription: _propTypes2.default.string.isRequired,
  titleTag: _propTypes2.default.string,
  subtitle: _propTypes2.default.string,
  shippingTitle: _propTypes2.default.string,
  shippingValue: _propTypes2.default.string,
  updateAmountTitle: _propTypes2.default.string,
  updateAmountLink: _propTypes2.default.string,
  onAfterClose: _propTypes2.default.func,
  onCloseClick: _propTypes2.default.func
};
CartRewardCardComponent.defaultProps = {
  titleTag: 'h1',
  subtitle: '',
  shippingTitle: '',
  shippingValue: '',
  updateAmountTitle: '',
  updateAmountLink: '',
  onAfterClose: function onAfterClose() {},
  onCloseClick: null
};


var garbageButtonKeyframes = _radium2.default.keyframes({
  '0%': { opacity: 1 },
  '100%': { opacity: 0, height: 0 }
});

var cartRewardAnimationClass = {
  animation: 'x .4s cubic-bezier(.895, .03, .685, .22) forwards',
  animationName: garbageButtonKeyframes,
  pointerEvents: 'none',
  opacity: 1
};

var borderWidth = 2;

var styles = {
  card: {
    borderWidth: borderWidth,
    borderStyle: 'solid',
    borderColor: _colorsConfig2.default.line1,

    hidden: cartRewardAnimationClass
  },

  description: _defineProperty({
    marginLeft: 20,
    marginRight: 20,
    maxWidth: 500

  }, '@media (min-width: ' + _screenConfig.ScreenConfig['S'].min + 'px)', {
    marginLeft: 40,
    marginRight: 40
  }),

  cart: {
    display: 'flex',
    flexDirection: 'row',
    justifyContent: 'flex-end'
  },

  garbage: {
    position: 'relative',
    top: '-' + borderWidth + 'px',
    right: '-' + borderWidth + 'px'
  }
};

var CartRewardCard = exports.CartRewardCard = (0, _mediaQueries.mediaQueries)(CartRewardCardComponent, {
  viewportIsMobile: true,
  exposedMethods: ['close']
});