'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);

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

/**
 * @constructor
 * @extends {ol.ImageBase}
 * @param {ol.Extent} extent Extent.
 * @param {number} resolution Resolution.
 * @param {number} pixelRatio Pixel ratio.
 * @param {HTMLCanvasElement} canvas Canvas.
 * @param {ol.ImageCanvasLoader=} opt_loader Optional loader function to
 *     support asynchronous canvas drawing.
 */
var _ol_ImageCanvas_ = function (extent, resolution, pixelRatio, canvas, opt_loader) {

  /**
   * Optional canvas loader function.
   * @type {?ol.ImageCanvasLoader}
   * @private
   */
  this.loader_ = opt_loader !== undefined ? opt_loader : null;

  var state = opt_loader !== undefined ? _imagestate2.default.IDLE : _imagestate2.default.LOADED;

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

  /**
   * @private
   * @type {HTMLCanvasElement}
   */
  this.canvas_ = canvas;

  /**
   * @private
   * @type {Error}
   */
  this.error_ = null;
};

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

/**
 * Get any error associated with asynchronous rendering.
 * @return {Error} Any error that occurred during rendering.
 */
_ol_ImageCanvas_.prototype.getError = function () {
  return this.error_;
};

/**
 * Handle async drawing complete.
 * @param {Error} err Any error during drawing.
 * @private
 */
_ol_ImageCanvas_.prototype.handleLoad_ = function (err) {
  if (err) {
    this.error_ = err;
    this.state = _imagestate2.default.ERROR;
  } else {
    this.state = _imagestate2.default.LOADED;
  }
  this.changed();
};

/**
 * @inheritDoc
 */
_ol_ImageCanvas_.prototype.load = function () {
  if (this.state == _imagestate2.default.IDLE) {
    this.state = _imagestate2.default.LOADING;
    this.changed();
    this.loader_(this.handleLoad_.bind(this));
  }
};

/**
 * @inheritDoc
 */
_ol_ImageCanvas_.prototype.getImage = function () {
  return this.canvas_;
};
exports.default = _ol_ImageCanvas_;
module.exports = exports['default'];