'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _index = require('./index.js');

var _index2 = _interopRequireDefault(_index);

var _lrucache = require('./structs/lrucache.js');

var _lrucache2 = _interopRequireDefault(_lrucache);

var _tilecoord = require('./tilecoord.js');

var _tilecoord2 = _interopRequireDefault(_tilecoord);

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

/**
 * @constructor
 * @extends {ol.structs.LRUCache.<ol.Tile>}
 * @param {number=} opt_highWaterMark High water mark.
 * @struct
 */
var _ol_TileCache_ = function (opt_highWaterMark) {

  _lrucache2.default.call(this, opt_highWaterMark);
};

_index2.default.inherits(_ol_TileCache_, _lrucache2.default);

/**
 * @param {Object.<string, ol.TileRange>} usedTiles Used tiles.
 */
_ol_TileCache_.prototype.expireCache = function (usedTiles) {
  var tile, zKey;
  while (this.canExpireCache()) {
    tile = this.peekLast();
    zKey = tile.tileCoord[0].toString();
    if (zKey in usedTiles && usedTiles[zKey].contains(tile.tileCoord)) {
      break;
    } else {
      this.pop().dispose();
    }
  }
};

/**
 * Prune all tiles from the cache that don't have the same z as the newest tile.
 */
_ol_TileCache_.prototype.pruneExceptNewestZ = function () {
  if (this.getCount() === 0) {
    return;
  }
  var key = this.peekFirstKey();
  var tileCoord = _tilecoord2.default.fromKey(key);
  var z = tileCoord[0];
  this.forEach(function (tile) {
    if (tile.tileCoord[0] !== z) {
      this.remove(_tilecoord2.default.getKey(tile.tileCoord));
      tile.dispose();
    }
  }, this);
};
exports.default = _ol_TileCache_;
module.exports = exports['default'];