"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _cloneDeep = _interopRequireDefault(require("lodash/cloneDeep"));

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

/**
 * 历史队列，使用数组存放
 * 有新的操作时存入数组的0位置，游标设置为0
 *
 */
var debug = require('debug')('worker:History');

var History =
/** @class */
function () {
  function History(hist) {
    var _this = this;
    /**
     * 游标位置
     */


    this.cursor = 0;
    /**
     * 历史数据
     */

    this.list = [];

    this.redoAble = function () {
      return _this.cursor !== 0;
    };

    this.undoAble = function () {
      return _this.cursor !== _this.list.length - 1;
    };
    /**
     * 清空0～cursor之间的数据，将data插入 位置0
     */


    this.push = function (data) {
      debug('push data', data);
      var cloneData = (0, _cloneDeep["default"])(data);

      _this.list.splice(0, _this.cursor, cloneData);

      return data;
    };
    /**
     * redu操作只修改当前游标-1
     * 并且返回后一步的json对象
     */


    this.redo = function () {
      debug('redo click');
      var newCursor = _this.cursor - 1;
      _this.cursor = newCursor;
      var newData = _this.list[newCursor];
      return newData;
    };
    /**
     * undo操作只修改当前的游标+1
     * 并且返回前一步的json对象
     */


    this.undo = function () {
      debug('undo click');
      var newCursor = _this.cursor + 1;
      _this.cursor = newCursor;
      var newData = _this.list[newCursor];
      return newData;
    };

    this.cursor = 0;
    this.list = [hist];
  }

  return History;
}();

var _default = History;
exports["default"] = _default;