'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactBootstrap = require('react-bootstrap');

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

const { i18n } = window;
const __ = i18n.others.__.bind(i18n.others);

// Notification modal
class ModalTrigger extends _react2.default.Component {
  constructor(...args) {
    var _temp;

    return _temp = super(...args), this.state = {
      isModalOpen: false,
      title: null,
      content: null
    }, this.handleToggle = () => {
      window.modalLocked = false;
      this.setState({
        isModalOpen: false
      });
      window.showModal();
    }, this.handleModal = e => {
      window.modalLocked = true;
      this.setState({
        isModalOpen: true,
        title: e.detail.title,
        content: e.detail.content,
        footer: e.detail.footer,
        onExiting: e.detail.onExiting
      });
    }, this.componentDidMount = () => {
      window.addEventListener('poi.modal', this.handleModal);
    }, this.componentWillUnmount = () => {
      window.removeEventListener('poi.modal', this.handleModal);
    }, this.renderFooter = footer => {
      if (!(typeof footer !== "undefined" && footer !== null && footer.length != null && footer.length > 0)) {
        return;
      }
      return footer.map((button, index) => {
        return _react2.default.createElement(
          _reactBootstrap.Button,
          { key: index, onClick: e => {
              this.handleToggle();
              button.func();
            }, bsStyle: button.style },
          button.name
        );
      });
    }, _temp;
  }

  render() {
    return _react2.default.createElement(
      _reactBootstrap.Modal,
      { autoFocus: true,
        animation: true,
        show: this.state.isModalOpen,
        onHide: this.handleToggle,
        onExiting: this.state.onExiting },
      _react2.default.createElement(
        _reactBootstrap.Modal.Header,
        { closeButton: true },
        _react2.default.createElement(
          _reactBootstrap.Modal.Title,
          null,
          this.state.title
        )
      ),
      _react2.default.createElement(
        _reactBootstrap.Modal.Body,
        null,
        this.state.content
      ),
      _react2.default.createElement(
        _reactBootstrap.Modal.Footer,
        null,
        _react2.default.createElement(
          _reactBootstrap.Button,
          { onClick: this.handleToggle },
          __((this.state.footer || []).length === 0 ? 'Close' : 'Cancel')
        ),
        this.renderFooter(this.state.footer)
      )
    );
  }
}
exports.ModalTrigger = ModalTrigger;