"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.undoRedoRegistry = exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _button = _interopRequireDefault(require("@alifd/next/lib/button"));

var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));

var _react = _interopRequireWildcard(require("react"));

require("./index.scss");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var UndoRedo = /*#__PURE__*/function (_PureComponent) {
  (0, _inheritsLoose2["default"])(UndoRedo, _PureComponent);

  function UndoRedo(props) {
    var _this;

    _this = _PureComponent.call(this, props) || this;
    _this.history = void 0;

    _this.handleHistoryChange = function (history) {
      var _this$history;

      _this.history = history;

      _this.updateState(((_this$history = _this.history) === null || _this$history === void 0 ? void 0 : _this$history.getState()) || 0);
    };

    _this.init = function () {
      var _editor$get, _this$history2;

      var editor = _this.props.editor;
      _this.history = (_editor$get = editor.get('designer')) === null || _editor$get === void 0 ? void 0 : _editor$get.currentHistory;

      _this.updateState(((_this$history2 = _this.history) === null || _this$history2 === void 0 ? void 0 : _this$history2.getState()) || 0);

      editor.on('designer.history.change', function (history) {
        var _this$history3;

        _this.history = history;

        _this.updateState(((_this$history3 = _this.history) === null || _this$history3 === void 0 ? void 0 : _this$history3.getState()) || 0);
      });
    };

    _this.updateState = function (state) {
      _this.setState({
        undoEnable: !!(state & 1),
        redoEnable: !!(state & 2)
      });
    };

    _this.handleUndoClick = function () {
      _this.history.back();
    };

    _this.handleRedoClick = function () {
      _this.history.forward();
    };

    _this.state = {
      undoEnable: false,
      redoEnable: false
    };
    return _this;
  }

  var _proto = UndoRedo.prototype;

  _proto.componentDidMount = /*#__PURE__*/function () {
    var _componentDidMount = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var _this$history4;

      var editor, designer;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              editor = this.props.editor;
              editor.on('designer.history.change', this.handleHistoryChange);
              _context.next = 4;
              return editor.onceGot('designer');

            case 4:
              designer = _context.sent;
              this.history = designer.currentHistory;
              this.updateState(((_this$history4 = this.history) === null || _this$history4 === void 0 ? void 0 : _this$history4.getState()) || 0);

            case 7:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, this);
    }));

    function componentDidMount() {
      return _componentDidMount.apply(this, arguments);
    }

    return componentDidMount;
  }();

  _proto.componentWillUnmount = function componentWillUnmount() {
    var editor = this.props.editor;
    editor.off('designer.history.change', this.handleHistoryChange);
  };

  _proto.render = function render() {
    var _this$props = this.props,
        undoIcon = _this$props.undoIcon,
        redoIcon = _this$props.redoIcon;
    console.log('props: ', this.props);
    var _this$state = this.state,
        undoEnable = _this$state.undoEnable,
        redoEnable = _this$state.redoEnable;
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "lowcode-plugin-undo-redo"
    }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
      size: "medium",
      "data-tip": "\u64A4\u9500",
      "data-dir": "bottom",
      onClick: this.handleUndoClick,
      ghost: true,
      disabled: !undoEnable
    }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      type: undoIcon || "arrow-left"
    })), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      size: "medium",
      "data-tip": "\u6062\u590D",
      "data-dir": "bottom",
      onClick: this.handleRedoClick,
      ghost: true,
      disabled: !redoEnable
    }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      type: redoIcon || "arrow-right"
    })));
  };

  return UndoRedo;
}(_react.PureComponent);

UndoRedo.displayName = 'LowcodeUndoRedo';

var undoRedoRegistry = function undoRedoRegistry(ctx) {
  return {
    name: "undo-redo-registry",
    init: function init() {
      return (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                // 注册回退/前进
                ctx.skeleton.add({
                  area: "topArea",
                  type: "Widget",
                  name: "undoRedo",
                  content: UndoRedo,
                  props: {
                    align: "right",
                    width: 88
                  },
                  contentProps: {
                    undoIcon: 'ic_corner_up_left',
                    redoIcon: 'ic_corner_up_right'
                  }
                });

              case 1:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }))();
    }
  };
};

exports.undoRedoRegistry = undoRedoRegistry;
undoRedoRegistry.pluginName = 'undoRedoRegistry';
var _default = undoRedoRegistry;
exports["default"] = _default;