'use strict';

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

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

var _index2 = _interopRequireDefault(_index);

var _imagebase = require('./imagebase.js');

var _imagebase2 = _interopRequireDefault(_imagebase);

var _imagestate = require('./imagestate.js');

var _imagestate2 = _interopRequireDefault(_imagestate);

var _events = require('./events.js');

var _events2 = _interopRequireDefault(_events);

var _eventtype = require('./events/eventtype.js');

var _eventtype2 = _interopRequireDefault(_eventtype);

var _extent = require('./extent.js');

var _extent2 = _interopRequireDefault(_extent);

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

/**
 * @constructor
 * @extends {ol.ImageBase}
 * @param {ol.Extent} extent Extent.
 * @param {number|undefined} resolution Resolution.
 * @param {number} pixelRatio Pixel ratio.
 * @param {string} src Image source URI.
 * @param {?string} crossOrigin Cross origin.
 * @param {ol.ImageLoadFunctionType} imageLoadFunction Image load function.
 */
var _ol_Image_ = function (extent, resolution, pixelRatio, src, crossOrigin, imageLoadFunction) {

  _imagebase2.default.call(this, extent, resolution, pixelRatio, _imagestate2.default.IDLE);

  /**
   * @private
   * @type {string}
   */
  this.src_ = src;

  /**
   * @private
   * @type {HTMLCanvasElement|Image|HTMLVideoElement}
   */
  this.image_ = new Image();
  if (crossOrigin !== null) {
    this.image_.crossOrigin = crossOrigin;
  }

  /**
   * @private
   * @type {Array.<ol.EventsKey>}
   */
  this.imageListenerKeys_ = null;

  /**
   * @protected
   * @type {ol.ImageState}
   */
  this.state = _imagestate2.default.IDLE;

  /**
   * @private
   * @type {ol.ImageLoadFunctionType}
   */
  this.imageLoadFunction_ = imageLoadFunction;
};

_index2.default.inherits(_ol_Image_, _imagebase2.default);

/**
 * @inheritDoc
 * @api
 */
_ol_Image_.prototype.getImage = function () {
  return this.image_;
};

/**
 * Tracks loading or read errors.
 *
 * @private
 */
_ol_Image_.prototype.handleImageError_ = function () {
  this.state = _imagestate2.default.ERROR;
  this.unlistenImage_();
  this.changed();
};

/**
 * Tracks successful image load.
 *
 * @private
 */
_ol_Image_.prototype.handleImageLoad_ = function () {
  if (this.resolution === undefined) {
    this.resolution = _extent2.default.getHeight(this.extent) / this.image_.height;
  }
  this.state = _imagestate2.default.LOADED;
  this.unlistenImage_();
  this.changed();
};

/**
 * Load the image or retry if loading previously failed.
 * Loading is taken care of by the tile queue, and calling this method is
 * only needed for preloading or for reloading in case of an error.
 * @override
 * @api
 */
_ol_Image_.prototype.load = function () {
  if (this.state == _imagestate2.default.IDLE || this.state == _imagestate2.default.ERROR) {
    this.state = _imagestate2.default.LOADING;
    this.changed();
    this.imageListenerKeys_ = [_events2.default.listenOnce(this.image_, _eventtype2.default.ERROR, this.handleImageError_, this), _events2.default.listenOnce(this.image_, _eventtype2.default.LOAD, this.handleImageLoad_, this)];
    this.imageLoadFunction_(this, this.src_);
  }
};

/**
 * @param {HTMLCanvasElement|Image|HTMLVideoElement} image Image.
 */
_ol_Image_.prototype.setImage = function (image) {
  this.image_ = image;
};

/**
 * Discards event handlers which listen for load completion or errors.
 *
 * @private
 */
_ol_Image_.prototype.unlistenImage_ = function () {
  this.imageListenerKeys_.forEach(_events2.default.unlistenByKey);
  this.imageListenerKeys_ = null;
};
exports.default = _ol_Image_;
module.exports = exports['default'];