'use strict';

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

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

var _index2 = _interopRequireDefault(_index);

var _image = require('../image.js');

var _image2 = _interopRequireDefault(_image);

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

var _imagestate2 = _interopRequireDefault(_imagestate);

var _dom = require('../dom.js');

var _dom2 = _interopRequireDefault(_dom);

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

var _proj = require('../proj.js');

var _proj2 = _interopRequireDefault(_proj);

var _image3 = require('../source/image.js');

var _image4 = _interopRequireDefault(_image3);

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

/**
 * @classdesc
 * A layer source for displaying a single, static image.
 *
 * @constructor
 * @extends {ol.source.Image}
 * @param {olx.source.ImageStaticOptions} options Options.
 * @api
 */
var _ol_source_ImageStatic_ = function (options) {
  var imageExtent = options.imageExtent;

  var crossOrigin = options.crossOrigin !== undefined ? options.crossOrigin : null;

  var /** @type {ol.ImageLoadFunctionType} */imageLoadFunction = options.imageLoadFunction !== undefined ? options.imageLoadFunction : _image4.default.defaultImageLoadFunction;

  _image4.default.call(this, {
    attributions: options.attributions,
    logo: options.logo,
    projection: _proj2.default.get(options.projection)
  });

  /**
   * @private
   * @type {ol.Image}
   */
  this.image_ = new _image2.default(imageExtent, undefined, 1, options.url, crossOrigin, imageLoadFunction);

  /**
   * @private
   * @type {ol.Size}
   */
  this.imageSize_ = options.imageSize ? options.imageSize : null;

  _events2.default.listen(this.image_, _eventtype2.default.CHANGE, this.handleImageChange, this);
};

_index2.default.inherits(_ol_source_ImageStatic_, _image4.default);

/**
 * @inheritDoc
 */
_ol_source_ImageStatic_.prototype.getImageInternal = function (extent, resolution, pixelRatio, projection) {
  if (_extent2.default.intersects(extent, this.image_.getExtent())) {
    return this.image_;
  }
  return null;
};

/**
 * @inheritDoc
 */
_ol_source_ImageStatic_.prototype.handleImageChange = function (evt) {
  if (this.image_.getState() == _imagestate2.default.LOADED) {
    var imageExtent = this.image_.getExtent();
    var image = this.image_.getImage();
    var imageWidth, imageHeight;
    if (this.imageSize_) {
      imageWidth = this.imageSize_[0];
      imageHeight = this.imageSize_[1];
    } else {
      imageWidth = image.width;
      imageHeight = image.height;
    }
    var resolution = _extent2.default.getHeight(imageExtent) / imageHeight;
    var targetWidth = Math.ceil(_extent2.default.getWidth(imageExtent) / resolution);
    if (targetWidth != imageWidth) {
      var context = _dom2.default.createCanvasContext2D(targetWidth, imageHeight);
      var canvas = context.canvas;
      context.drawImage(image, 0, 0, imageWidth, imageHeight, 0, 0, canvas.width, canvas.height);
      this.image_.setImage(canvas);
    }
  }
  _image4.default.prototype.handleImageChange.call(this, evt);
};
exports.default = _ol_source_ImageStatic_;
module.exports = exports['default'];