'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 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; }; }();

require('codemirror/mode/javascript/javascript');

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _SelectField = require('material-ui/SelectField');

var _SelectField2 = _interopRequireDefault(_SelectField);

var _MenuItem = require('material-ui/MenuItem');

var _MenuItem2 = _interopRequireDefault(_MenuItem);

var _Button = require('./Button');

var _Button2 = _interopRequireDefault(_Button);

var _add = require('react-icons/lib/md/add');

var _add2 = _interopRequireDefault(_add);

var _edit = require('react-icons/lib/md/edit');

var _edit2 = _interopRequireDefault(_edit);

var _reduxDevtoolsTestGenerator = require('redux-devtools-test-generator');

var _reduxDevtoolsTestGenerator2 = _interopRequireDefault(_reduxDevtoolsTestGenerator);

var _template = require('redux-devtools-test-generator/lib/redux/mocha/template');

var _template2 = _interopRequireDefault(_template);

var _template3 = require('redux-devtools-test-generator/lib/redux/tape/template');

var _template4 = _interopRequireDefault(_template3);

var _template5 = require('redux-devtools-test-generator/lib/redux/ava/template');

var _template6 = _interopRequireDefault(_template5);

var _template7 = require('redux-devtools-test-generator/lib/vanilla/mocha/template');

var _template8 = _interopRequireDefault(_template7);

var _template9 = require('redux-devtools-test-generator/lib/vanilla/tape/template');

var _template10 = _interopRequireDefault(_template9);

var _template11 = require('redux-devtools-test-generator/lib/vanilla/ava/template');

var _template12 = _interopRequireDefault(_template11);

var _TestForm = require('./TestForm');

var _TestForm2 = _interopRequireDefault(_TestForm);

var _localStorage = require('../utils/localStorage');

var _styles = require('../styles');

var _styles2 = _interopRequireDefault(_styles);

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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 testTemplates = void 0;
var selected = void 0;
var isDefaultTemplate = void 0;

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

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

    var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(TestGen).call(this, props));

    _this.onSelect = function (event, index, value) {
      selected = (0, _localStorage.saveToStorage)('test-templates-sel', value);
      _this.setState({ selected: selected });
    };

    _this.editTemplate = function () {
      _this.setState({ dialogStatus: 1 });
    };

    _this.addTemplate = function () {
      _this.setState({ dialogStatus: 2 });
    };

    _this.handleSave = function (template) {
      testTemplates = [].concat(_toConsumableArray(_this.state.testTemplates));
      selected = _this.state.selected;

      if (_this.state.dialogStatus === 1) {
        testTemplates[_this.state.selected] = template;
      } else {
        testTemplates.push(template);
        selected = testTemplates.length - 1;
        (0, _localStorage.saveToStorage)('test-templates-sel', selected);
      }

      (0, _localStorage.saveToStorage)('test-templates', testTemplates);
      _this.setState({ testTemplates: testTemplates, selected: selected, dialogStatus: 0 });
      isDefaultTemplate = false;
    };

    _this.handleRemove = function () {
      // Todo: add snackbar with undo
      selected = 0;
      testTemplates = [].concat(_toConsumableArray(_this.state.testTemplates));
      testTemplates.splice(_this.state.selected, 1);
      if (testTemplates.length === 0) testTemplates = _this.getDefaultTemplates();
      (0, _localStorage.saveToStorage)('test-templates-sel', selected);
      (0, _localStorage.saveToStorage)('test-templates', testTemplates);
      _this.setState({ testTemplates: testTemplates, selected: selected, dialogStatus: 0 });
      isDefaultTemplate = false;
    };

    _this.handleCloseDialog = function () {
      _this.setState({ dialogStatus: 0 });
    };

    if (!testTemplates) {
      testTemplates = props.testTemplates || (0, _localStorage.getFromStorage)('test-templates');
      selected = props.selectedTemplate || (0, _localStorage.getFromStorage)('test-templates-sel') || 0;
      if (typeof testTemplates === 'string') {
        testTemplates = JSON.parse(testTemplates);
      }
      if (!testTemplates || testTemplates.length === 0 || !testTemplates[0].dispatcher) {
        testTemplates = _this.getDefaultTemplates();
        isDefaultTemplate = true;
      }
      if (typeof selected === 'string') {
        selected = Number(selected);
      }
    } else if (isDefaultTemplate) {
      testTemplates = _this.getDefaultTemplates();
    }

    _this.state = { testTemplates: testTemplates, selected: selected, dialogStatus: 0 };
    return _this;
  }

  _createClass(TestGen, [{
    key: 'componentWillUpdate',
    value: function componentWillUpdate(nextProps) {
      if (isDefaultTemplate && this.props.isRedux !== nextProps.isRedux) {
        testTemplates = this.getDefaultTemplates(nextProps.isRedux);
        this.setState({ testTemplates: testTemplates });
      }
    }
  }, {
    key: 'getDefaultTemplates',
    value: function getDefaultTemplates() {
      var isRedux = arguments.length <= 0 || arguments[0] === undefined ? this.props.isRedux : arguments[0];

      if (isRedux) return [_template2.default, _template4.default, _template6.default];
      return [_template8.default, _template10.default, _template12.default];
    }
  }, {
    key: 'render',
    value: function render() {
      var _state = this.state;
      var dialogStatus = _state.dialogStatus;
      var selected = _state.selected;
      var testTemplates = _state.testTemplates; // eslint-disable-line

      var template = testTemplates[selected];
      var assertion = template.assertion;
      var dispatcher = template.dispatcher;
      var wrap = template.wrap;


      return _react2.default.createElement(_reduxDevtoolsTestGenerator2.default, _extends({
        isVanilla: !this.props.isRedux,
        assertion: assertion, dispatcher: dispatcher, wrap: wrap,
        theme: 'night', useCodemirror: this.props.useCodemirror,
        header: _react2.default.createElement(
          'div',
          { style: { height: '2.5em', minHeight: '2.5em', display: 'flex' } },
          _react2.default.createElement(
            _SelectField2.default,
            {
              style: _styles2.default.select,
              labelStyle: _styles2.default.selectLabel,
              iconStyle: _styles2.default.selectIcon,
              onChange: this.onSelect,
              value: selected
            },
            testTemplates.map(function (item, i) {
              return _react2.default.createElement(_MenuItem2.default, { key: i, value: i, primaryText: item.name });
            })
          ),
          _react2.default.createElement(_Button2.default, { Icon: _edit2.default, onClick: this.editTemplate }),
          _react2.default.createElement(_Button2.default, { Icon: _add2.default, onClick: this.addTemplate }),
          _react2.default.createElement(_TestForm2.default, {
            template: template, dialogStatus: dialogStatus,
            onSave: this.handleSave,
            onRemove: this.handleRemove,
            onClose: this.handleCloseDialog
          })
        )
      }, this.props));
    }
  }]);

  return TestGen;
}(_react.Component);

exports.default = TestGen;


TestGen.propTypes = {
  isRedux: _react.PropTypes.bool,
  testTemplates: _react.PropTypes.oneOfType([_react.PropTypes.array, _react.PropTypes.string]),
  selectedTemplate: _react.PropTypes.oneOfType([_react.PropTypes.number, _react.PropTypes.string]),
  useCodemirror: _react.PropTypes.bool
};
module.exports = exports['default'];