function _extends() { _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; }; return _extends.apply(this, arguments); }

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

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { omit } from 'lodash';
import * as DialogVariants from './Dialog';
/*
 * Displays a stack of Dialogs.
 *
 * <Dialogs dialogs closeDialog />
 *
 * `closeDialog` called when Dialog is cancelled or confirmed, may be used to track
 * state.
 *
 * `dialogs` prop in the format (note many of the props simple map
 * to the specific Dialog type):
 *
 * dialogs: [
 *   {
 *     id: '1234-1234-1',
 *     type: 'Confirm',
 *     title: 'Something to confirm',
 *     message: 'More detail about confirmation',
 *     onConfirm: () => {},
 *     onCancel: () => {},
 *   },
 *   {
 *     id: '1234-1234-2',
 *     type: 'Notice',
 *     title: 'Something info for the user',
 *     message: 'More detail...',
 *     onConfirm: () => {},
 *   },
*   {
 *     id: '1234-1234-3',
 *     type: 'Warning',
 *     title: 'Something to warn the user about, maybe a non-failing error',
 *     message: 'More detail...',
 *     onConfirm: () => {},
 *   },
 *   {
 *     id: '1234-1234-4',
 *     type: 'Error',
 *     error: new Error('message and title are automatic'),
 *     onConfirm: () => {},
 *     onCancel: () => {},
 *   },
 * ]
 */

class Dialogs extends Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "handleConfirm", dialog => {
      const {
        closeDialog
      } = this.props;

      if (dialog.onConfirm) {
        dialog.onConfirm(dialog);
      }

      closeDialog(dialog.id);
    });

    _defineProperty(this, "handleCancel", dialog => {
      const {
        closeDialog
      } = this.props;

      if (dialog.onCancel) {
        dialog.onCancel(dialog);
      }

      closeDialog(dialog.id);
    });

    _defineProperty(this, "renderDialog", dialog => {
      const Dialog = DialogVariants[dialog.type];

      const onConfirm = () => this.handleConfirm(dialog);

      const onCancel = () => this.handleCancel(dialog);

      return /*#__PURE__*/React.createElement(Dialog, _extends({
        show: true,
        key: dialog.id,
        onConfirm: onConfirm,
        onCancel: onCancel // eslint-disable-next-line react/jsx-props-no-spreading

      }, omit(dialog, ['onConfirm', 'onCancel'])));
    });
  }

  get dialogs() {
    const {
      dialogs
    } = this.props;
    return dialogs;
  }

  render() {
    return this.dialogs.map(this.renderDialog);
  }

}

Dialogs.propTypes = {
  dialogs: PropTypes.array,
  closeDialog: PropTypes.func.isRequired
};
Dialogs.defaultProps = {
  dialogs: []
};
export { Dialogs };
export default Dialogs;