'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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 _marger = require('kitten/components/layout/marger');

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

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

var _popover = require('kitten/components/popovers/popover');

var _button = require('kitten/components/buttons/button');

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

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 CallToActionPopover = exports.CallToActionPopover = function (_Component) {
  _inherits(CallToActionPopover, _Component);

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

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

    _this.renderButton = _this.renderButton.bind(_this);
    return _this;
  }

  _createClass(CallToActionPopover, [{
    key: 'getButtonClickHandler',
    value: function getButtonClickHandler(clickOptions) {
      if (!clickOptions) return;

      if (clickOptions.closeOnClick) {
        return this.props.onCloseClick;
      }
    }
  }, {
    key: 'renderIllustration',
    value: function renderIllustration() {
      if (!this.props.illustration) return;

      var Illustration = this.props.illustration;

      return _react2.default.createElement(
        'div',
        { className: 'k-Popover__illustration' },
        _react2.default.createElement(Illustration, null)
      );
    }
  }, {
    key: 'renderButton',
    value: function renderButton(options, i) {
      var label = options.label,
          clickOptions = options.clickOptions,
          others = _objectWithoutProperties(options, ['label', 'clickOptions']);

      var clickHandler = this.getButtonClickHandler(clickOptions);

      return _react2.default.createElement(_button.Button, _extends({ onClick: clickHandler, key: i, children: label }, others));
    }
  }, {
    key: 'renderButtonsList',
    value: function renderButtonsList() {
      return this.props.buttons.map(this.renderButton);
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          title = _props.title,
          text = _props.text,
          titleAriaLabelId = _props.titleAriaLabelId,
          buttons = _props.buttons,
          illustration = _props.illustration,
          popoverProps = _objectWithoutProperties(_props, ['title', 'text', 'titleAriaLabelId', 'buttons', 'illustration']);

      return _react2.default.createElement(
        _popover.Popover,
        _extends({ titleAriaLabelId: titleAriaLabelId }, popoverProps),
        this.renderIllustration(),
        _react2.default.createElement(
          _marger.Marger,
          { top: '4', bottom: '4', className: 'k-Popover__content' },
          _react2.default.createElement(
            _marger.Marger,
            { bottom: '1' },
            _react2.default.createElement(
              _title.Title,
              { id: titleAriaLabelId, modifier: 'senary', margin: false },
              title
            )
          ),
          _react2.default.createElement(
            _marger.Marger,
            { top: '1', bottom: '3' },
            _react2.default.createElement(
              _paragraph.Paragraph,
              { modifier: 'tertiary', margin: false },
              text
            )
          ),
          _react2.default.createElement(
            _marger.Marger,
            { top: '3' },
            _react2.default.createElement(
              'div',
              { className: 'k-Popover__navigation' },
              _react2.default.createElement(
                'div',
                { className: 'k-Popover__buttons' },
                this.renderButtonsList()
              )
            )
          )
        )
      );
    }
  }]);

  return CallToActionPopover;
}(_react.Component);

CallToActionPopover.defaultProps = {
  title: '',
  text: '',
  illustration: null,
  buttons: [{
    label: 'Ok',
    modifier: 'helium',
    size: 'big'
  }],
  onCloseClick: null
};