import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _defineProperty from "@babel/runtime/helpers/defineProperty";

var _dec, _class, _class2, _temp;

import React, { PureComponent } from 'react';
import { Button, Paper, Zoom } from '@material-ui/core';
import { withTheme } from '@material-ui/core/styles';
import PropTypes from 'prop-types';
import { style } from 'typestyle';
import red from '@material-ui/core/colors/red';
import ActionRestore from '@material-ui/icons/Restore';
var cn = {
  dialogRoot: style({
    position: 'absolute',
    top: 0,
    left: 0,
    width: '100%',
    zIndex: 1001
  }),
  error: style({
    margin: 16,
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
    borderStyle: 'double',
    borderColor: red['500'],
    backgroundColor: red['50'],
    overflow: 'hidden'
  }),
  heading: style({
    color: 'rgba(255, 0, 0, .5)'
  }),
  close: style({
    alignSelf: 'flex-end'
  }),
  blank: style({
    flex: '0 0 1rem'
  }),
  messageText: style({
    margin: 8,
    color: red['500'],
    fontFamily: 'Consolas, "Liberation Mono", Menlo, Courier, monospace'
  })
};

var getCn = function getCn(props, state) {
  return {
    root: style({
      borderTopWidth: 3,
      borderTopStyle: state.show ? 'double' : 'none',
      borderTopColor: props.theme.palette.primary.main
    }),
    message: style({
      position: 'relative',
      maxHeight: props.error ? state.expanded ? 1000 : 48 : 0,
      width: '100%',
      boxSizing: 'border-box',
      paddingLeft: 8,
      overflow: 'scroll',
      cursor: 'pointer'
    })
  };
};

var ErrorPane = (_dec = withTheme(), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_PureComponent) {
  _inherits(ErrorPane, _PureComponent);

  function ErrorPane() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, ErrorPane);

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

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

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "state", {
      show: false,
      expanded: false
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleClose", function () {
      _this.setState({
        show: false
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleRestore", function () {
      _this.setState({
        show: false
      }, function () {
        _this.props.onRestore();
      });
    });

    return _this;
  }

  _createClass(ErrorPane, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      if (prevProps.error !== this.props.error) {
        this.setState({
          show: !!this.props.error
        });
      }
    }
  }, {
    key: "renderAsDialog",
    value: function renderAsDialog() {
      var _this$props = this.props,
          localization = _this$props.localization,
          canRestore = _this$props.canRestore;
      return React.createElement("div", {
        className: cn.dialogRoot
      }, React.createElement(Paper, {
        key: "error",
        elevation: 2,
        className: cn.error
      }, React.createElement(Button, {
        variant: "text",
        color: "primary",
        className: cn.close,
        onClick: this.handleClose
      }, localization.common.close), React.createElement("h2", {
        className: cn.heading
      }, localization.editorCard.error), React.createElement("div", {
        className: cn.blank
      }), React.createElement(Button, {
        variant: "contained",
        color: "primary",
        onClick: this.handleRestore,
        disabled: !canRestore
      }, React.createElement(ActionRestore, null), localization.editorCard.restore), React.createElement("div", {
        className: cn.blank
      })));
    }
  }, {
    key: "renderAsDock",
    value: function renderAsDock(className) {
      var _this2 = this;

      var expanded = this.state.expanded;
      return React.createElement("div", {
        className: className,
        onClick: function onClick() {
          return _this2.setState({
            expanded: !expanded
          });
        }
      }, React.createElement("pre", {
        className: cn.messageText
      }, this.props.error && this.props.error.message));
    }
  }, {
    key: "render",
    value: function render() {
      var dcn = getCn(this.props, this.state);
      var show = this.state.show;
      return React.createElement("div", {
        className: dcn.root
      }, this.renderAsDock(dcn.message), React.createElement(Zoom, {
        in: show
      }, this.renderAsDialog()));
    }
  }]);

  return ErrorPane;
}(PureComponent), _defineProperty(_class2, "propTypes", {
  theme: PropTypes.object.isRequired,
  error: PropTypes.object,
  localization: PropTypes.object.isRequired,
  onRestore: PropTypes.func.isRequired,
  canRestore: PropTypes.bool.isRequired
}), _temp)) || _class);
export { ErrorPane as default };