"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.DispatcherContainer = exports.CodeContainer = exports.ActionContainer = void 0;
var _react = _interopRequireWildcard(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _ui = require("@redux-devtools/ui");
var _reactRedux = require("react-redux");
var _actions = require("../../actions");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
// Based on https://github.com/YoruNoHikage/redux-devtools-dispatch

const DispatcherContainer = exports.DispatcherContainer = _styledComponents.default.div`
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  padding-top: 2px;
  background: ${props => props.theme.base01};
`;
const CodeContainer = exports.CodeContainer = _styledComponents.default.div`
  height: 75px;
  padding-right: 6px;
  overflow: auto;
`;
const ActionContainer = exports.ActionContainer = _styledComponents.default.div`
  display: table;
  width: 100%;
  color: ${props => props.theme.base06};

  > div {
    display: table-row;

    > div:first-child {
      width: 1px;
      padding-left: 8px;
      display: table-cell;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    > div:nth-child(2) {
      display: table-cell;
      width: 100%;
      padding: 6px;
    }
  }
`;
class Dispatcher extends _react.Component {
  state = {
    selected: 'default',
    customAction: this.props.options.lib === 'redux' ? "{\n  type: ''\n}" : 'this.',
    args: [],
    rest: '[]',
    changed: false
  };
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (this.state.selected !== 'default' && !nextProps.options.actionCreators) {
      this.setState({
        selected: 'default',
        args: []
      });
    }
  }
  shouldComponentUpdate(nextProps, nextState) {
    return nextState !== this.state || nextProps.options.actionCreators !== this.props.options.actionCreators;
  }
  selectActionCreator = selected => {
    if (selected === 'actions-help') {
      window.open('https://github.com/reduxjs/redux-devtools/blob/main/extension/docs/' + 'API/Arguments.md#actioncreators');
      return;
    }
    const args = [];
    if (selected !== 'default') {
      args.length = this.props.options.actionCreators[selected].args.length;
    }
    this.setState({
      selected,
      args,
      rest: '[]',
      changed: false
    });
  };
  handleArg = argIndex => value => {
    const args = [...this.state.args.slice(0, argIndex), value || undefined, ...this.state.args.slice(argIndex + 1)];
    this.setState({
      args,
      changed: true
    });
  };
  handleRest = rest => {
    this.setState({
      rest,
      changed: true
    });
  };
  handleCustomAction = customAction => {
    this.setState({
      customAction,
      changed: true
    });
  };
  dispatchAction = () => {
    const {
      selected,
      customAction,
      args,
      rest
    } = this.state;
    if (selected !== 'default') {
      // remove trailing `undefined` arguments
      let i = args.length - 1;
      while (i >= 0 && typeof args[i] === 'undefined') {
        args.pop();
        i--;
      }
      this.props.dispatch({
        name: this.props.options.actionCreators[selected].name,
        selected,
        args,
        rest
      });
    } else {
      this.props.dispatch(customAction);
    }
    this.setState({
      changed: false
    });
  };
  render() {
    const actionCreators = this.props.options.actionCreators;
    let actionElement;
    if (this.state.selected === 'default' || !actionCreators) {
      actionElement = /*#__PURE__*/_react.default.createElement(CodeContainer, null, /*#__PURE__*/_react.default.createElement(_ui.Editor, {
        value: this.state.customAction,
        onChange: this.handleCustomAction
      }));
    } else {
      actionElement = /*#__PURE__*/_react.default.createElement(ActionContainer, null, actionCreators[this.state.selected].args.map((param, i) => /*#__PURE__*/_react.default.createElement("div", {
        key: `${param}${i}`
      }, /*#__PURE__*/_react.default.createElement("div", null, param), /*#__PURE__*/_react.default.createElement(_ui.Editor, {
        lineNumbers: false,
        value: this.state.args[i],
        onChange: this.handleArg(i)
      }))), /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", null, "...rest"), /*#__PURE__*/_react.default.createElement(_ui.Editor, {
        lineNumbers: false,
        value: this.state.rest,
        onChange: this.handleRest
      })));
    }
    let options = [{
      value: 'default',
      label: 'Custom action'
    }];
    if (actionCreators && actionCreators.length > 0) {
      options = options.concat(actionCreators.map((_ref, i) => {
        let {
          name,
          args
        } = _ref;
        return {
          value: i,
          label: `${name}(${args.join(', ')})`
        };
      }));
    } else {
      options.push({
        value: 'actions-help',
        label: 'Add your app built-in actions…'
      });
    }
    return /*#__PURE__*/_react.default.createElement(DispatcherContainer, null, actionElement, /*#__PURE__*/_react.default.createElement(_ui.Toolbar, null, /*#__PURE__*/_react.default.createElement(_ui.Select, {
      menuPlacement: "top",
      onChange: option => this.selectActionCreator(option.value),
      value: options.find(option => option.value === this.state.selected) || options.find(option => option.value === 'default'),
      options: options
    }), /*#__PURE__*/_react.default.createElement(_ui.Button, {
      onClick: this.dispatchAction,
      primary: this.state.changed
    }, "Dispatch")));
  }
}
const actionCreators = {
  dispatch: _actions.dispatchRemotely
};
var _default = exports.default = (0, _reactRedux.connect)(null, actionCreators)(Dispatcher);