"use strict";

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

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

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

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

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

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

var Node = /*#__PURE__*/(0, _createClass2["default"])(function Node(value, prev, next, list) {
  (0, _classCallCheck2["default"])(this, Node);

  if (!(this instanceof Node)) {
    return new Node(value, prev, next, list);
  }

  this.list = list;
  this.value = value;

  if (prev) {
    prev.next = this;
    this.prev = prev;
  } else {
    this.prev = null;
  }

  if (next) {
    next.prev = this;
    this.next = next;
  } else {
    this.next = null;
  }
});

function insert(self, node, value) {
  var inserted = node === self.head ? new Node(value, null, node, self) : new Node(value, node, node.next, self);

  if (inserted.next === null) {
    self.tail = inserted;
  }

  if (inserted.prev === null) {
    self.head = inserted;
  }

  self.length++;
  return inserted;
}

function _push(self, item) {
  self.tail = new Node(item, self.tail, null, self);

  if (!self.head) {
    self.head = self.tail;
  }

  self.length++;
}

function _unshift(self, item) {
  self.head = new Node(item, null, self.head, self);

  if (!self.tail) {
    self.tail = self.head;
  }

  self.length++;
}

var Yallist = /*#__PURE__*/function () {
  function Yallist() {
    (0, _classCallCheck2["default"])(this, Yallist);

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

    if (!(this instanceof Yallist)) {
      return (0, _construct2["default"])(Yallist, args);
    }

    this.tail = null;
    this.head = null;
    this.length = 0;
    var list = args[0];

    if (list && typeof list.forEach === 'function') {
      list.forEach(function (item) {
        this.push(item);
      });
    } else if (args.length > 0) {
      this.push.apply(this, args);
    }
  }

  (0, _createClass2["default"])(Yallist, [{
    key: "removeNode",
    value: function removeNode(node) {
      if (node.list !== this) {
        throw new Error('removing node which does not belong to this list');
      }

      var next = node.next,
          prev = node.prev;

      if (next) {
        next.prev = prev;
      }

      if (prev) {
        prev.next = next;
      }

      if (node === this.head) {
        this.head = next;
      }

      if (node === this.tail) {
        this.tail = prev;
      }

      node.list.length--;
      node.next = null;
      node.prev = null;
      node.list = null;
      return next;
    }
  }, {
    key: "unshiftNode",
    value: function unshiftNode(node) {
      if (node === this.head) {
        return;
      }

      if (node.list) {
        node.list.removeNode(node);
      }

      var head = this.head;
      node.list = this;
      node.next = head;

      if (head) {
        head.prev = node;
      }

      this.head = node;

      if (!this.tail) {
        this.tail = node;
      }

      this.length++;
    }
  }, {
    key: "pushNode",
    value: function pushNode(node) {
      if (node === this.tail) {
        return;
      }

      if (node.list) {
        node.list.removeNode(node);
      }

      var tail = this.tail;
      node.list = this;
      node.prev = tail;

      if (tail) {
        tail.next = node;
      }

      this.tail = node;

      if (!this.head) {
        this.head = node;
      }

      this.length++;
    }
  }, {
    key: "push",
    value: function push() {
      var _this = this;

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

      args.forEach(function (item) {
        return _push(_this, item);
      });
      return this.length;
    }
  }, {
    key: "unshift",
    value: function unshift() {
      var _this2 = this;

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

      args.forEach(function (item) {
        return _unshift(_this2, item);
      });
      return this.length;
    }
  }, {
    key: "pop",
    value: function pop() {
      if (!this.tail) {
        return undefined;
      }

      var value = this.tail.value;
      this.tail = this.tail.prev;

      if (this.tail) {
        this.tail.next = null;
      } else {
        this.head = null;
      }

      this.length--;
      return value;
    }
  }, {
    key: "shift",
    value: function shift() {
      if (!this.head) {
        return undefined;
      }

      var value = this.head.value;
      this.head = this.head.next;

      if (this.head) {
        this.head.prev = null;
      } else {
        this.tail = null;
      }

      this.length--;
      return value;
    }
  }, {
    key: "forEach",
    value: function forEach(fn, thisp) {
      thisp = thisp || this;

      for (var walker = this.head, i = 0; walker !== null; i++) {
        fn.call(thisp, walker.value, i, this);
        walker = walker.next;
      }
    }
  }, {
    key: "forEachReverse",
    value: function forEachReverse(fn, thisp) {
      thisp = thisp || this;

      for (var walker = this.tail, i = this.length - 1; walker !== null; i--) {
        fn.call(thisp, walker.value, i, this);
        walker = walker.prev;
      }
    }
  }, {
    key: "get",
    value: function get(n) {
      var i = 0;
      var walker = this.head;

      for (; walker !== null && i < n; i++) {
        walker = walker.next;
      }

      if (i === n && walker !== null) {
        return walker.value;
      }
    }
  }, {
    key: "getReverse",
    value: function getReverse(n) {
      var i = 0;
      var walker = this.tail;

      for (; walker !== null && i < n; i++) {
        // abort out of the list early if we hit a cycle
        walker = walker.prev;
      }

      if (i === n && walker !== null) {
        return walker.value;
      }
    }
  }, {
    key: "map",
    value: function map(fn, thisp) {
      thisp = thisp || this;
      var res = new Yallist();

      for (var walker = this.head; walker !== null;) {
        res.push(fn.call(thisp, walker.value, this));
        walker = walker.next;
      }

      return res;
    }
  }, {
    key: "mapReverse",
    value: function mapReverse(fn, thisp) {
      thisp = thisp || this;
      var res = new Yallist();

      for (var walker = this.tail; walker !== null;) {
        res.push(fn.call(thisp, walker.value, this));
        walker = walker.prev;
      }

      return res;
    }
  }, {
    key: "reduce",
    value: function reduce() {
      var fn = arguments.length <= 0 ? undefined : arguments[0];
      var acc;
      var walker = this.head;

      if (arguments.length > 1) {
        acc = arguments.length <= 1 ? undefined : arguments[1];
      } else if (this.head) {
        walker = this.head.next;
        acc = this.head.value;
      } else {
        throw new TypeError('Reduce of empty list with no initial value');
      }

      for (var i = 0; walker !== null; i++) {
        acc = fn(acc, walker.value, i);
        walker = walker.next;
      }

      return acc;
    }
  }, {
    key: "reduceReverse",
    value: function reduceReverse() {
      var fn = arguments.length <= 0 ? undefined : arguments[0];
      var acc;
      var walker = this.tail;

      if (arguments.length > 1) {
        acc = arguments.length <= 1 ? undefined : arguments[1];
      } else if (this.tail) {
        walker = this.tail.prev;
        acc = this.tail.value;
      } else {
        throw new TypeError('Reduce of empty list with no initial value');
      }

      for (var i = this.length - 1; walker !== null; i--) {
        acc = fn(acc, walker.value, i);
        walker = walker.prev;
      }

      return acc;
    }
  }, {
    key: "toArray",
    value: function toArray() {
      var arr = new Array(this.length);

      for (var i = 0, walker = this.head; walker !== null; i++) {
        arr[i] = walker.value;
        walker = walker.next;
      }

      return arr;
    }
  }, {
    key: "toArrayReverse",
    value: function toArrayReverse() {
      var arr = new Array(this.length);

      for (var i = 0, walker = this.tail; walker !== null; i++) {
        arr[i] = walker.value;
        walker = walker.prev;
      }

      return arr;
    }
  }, {
    key: "slice",
    value: function slice(from, to) {
      to = to || this.length;

      if (to < 0) {
        to += this.length;
      }

      from = from || 0;

      if (from < 0) {
        from += this.length;
      }

      var ret = new Yallist();

      if (to < from || to < 0) {
        return ret;
      }

      if (from < 0) {
        from = 0;
      }

      if (to > this.length) {
        to = this.length;
      }

      var i = 0;
      var walker = this.head;

      for (; walker !== null && i < from; i++) {
        walker = walker.next;
      }

      for (; walker !== null && i < to; i++, walker = walker.next) {
        ret.push(walker.value);
      }

      return ret;
    }
  }, {
    key: "sliceReverse",
    value: function sliceReverse(from, to) {
      to = to || this.length;

      if (to < 0) {
        to += this.length;
      }

      from = from || 0;

      if (from < 0) {
        from += this.length;
      }

      var ret = new Yallist();

      if (to < from || to < 0) {
        return ret;
      }

      if (from < 0) {
        from = 0;
      }

      if (to > this.length) {
        to = this.length;
      }

      var i = this.length;
      var walker = this.tail;

      for (; walker !== null && i > to; i--) {
        walker = walker.prev;
      }

      for (; walker !== null && i > from; i--, walker = walker.prev) {
        ret.push(walker.value);
      }

      return ret;
    }
  }, {
    key: "splice",
    value: function splice(start, deleteCount) {
      var _this3 = this;

      if (start > this.length) {
        start = this.length - 1;
      }

      if (start < 0) {
        start = this.length + start;
      }

      var walker = this.head;

      for (var i = 0; walker !== null && i < start; i++) {
        walker = walker.next;
      }

      var ret = [];

      for (var _i = 0; walker && _i < deleteCount; _i++) {
        ret.push(walker.value);
        walker = this.removeNode(walker);
      }

      if (walker === null) {
        walker = this.tail;
      }

      if (walker !== this.head && walker !== this.tail && walker !== null) {
        walker = walker.prev;
      }

      for (var _len4 = arguments.length, nodes = new Array(_len4 > 2 ? _len4 - 2 : 0), _key4 = 2; _key4 < _len4; _key4++) {
        nodes[_key4 - 2] = arguments[_key4];
      }

      nodes.forEach(function (item) {
        walker = insert(_this3, walker, item);
      });
      return ret;
    }
  }, {
    key: "reverse",
    value: function reverse() {
      var head = this.head,
          tail = this.tail;

      for (var walker = head; walker !== null; walker = walker.prev) {
        var _walker = walker,
            prev = _walker.prev;
        walker.prev = walker.next;
        walker.next = prev;
      }

      this.head = tail;
      this.tail = head;
      return this;
    }
  }]);
  return Yallist;
}();

exports["default"] = Yallist;
Yallist.Node = Node;
Yallist.create = Yallist;

try {
  Yallist.prototype[Symbol.iterator] = /*#__PURE__*/_regenerator["default"].mark(function _callee() {
    var walker;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            walker = this.head;

          case 1:
            if (!walker) {
              _context.next = 7;
              break;
            }

            _context.next = 4;
            return walker.value;

          case 4:
            walker = walker.next;
            _context.next = 1;
            break;

          case 7:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, this);
  }); // eslint-disable-next-line no-empty
} catch (er) {}
//# sourceMappingURL=Yallist.js.map
