var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

// Copyright (c) 2015 - 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

/* global setTimeout */
import { experimental } from 'deck.gl';
var Deck = experimental.Deck,
    DeckGLJS = experimental.DeckGLJS,
    MapView = experimental.MapView;


import { getImageFromContext } from './luma.gl/io-basic/browser-image-utils';

function noop() {}

function makePromise() {
  var resolve = void 0;
  var reject = void 0;
  var promise = new Promise(function (resolve_, reject_) {
    resolve = resolve_;
    reject = reject_;
  });
  promise.resolve = resolve;
  promise.reject = reject;
  return promise;
}

var SceneRenderer = function () {
  function SceneRenderer() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        scenes = _ref.scenes,
        _ref$width = _ref.width,
        width = _ref$width === undefined ? 800 : _ref$width,
        _ref$height = _ref.height,
        height = _ref$height === undefined ? 450 : _ref$height,
        onSceneRendered = _ref.onSceneRendered,
        _ref$onComplete = _ref.onComplete,
        onComplete = _ref$onComplete === undefined ? noop : _ref$onComplete;

    _classCallCheck(this, SceneRenderer);

    this.width = width;
    this.height = height;
    this.scenes = scenes;
    this.running = false;
    this.complete = true;
    this.onSceneRendered = onSceneRendered;
    this.onComplete = onComplete;
  }

  _createClass(SceneRenderer, [{
    key: 'run',
    value: function run() {
      // TODO - for 5.1 compatibility, remove when 5.2 is released
      var DeckGL = Deck || DeckGLJS;
      this.deckgl = new DeckGL({
        id: 'default-deckgl-overlay',
        style: { position: 'absolute', left: '0px', top: '0px' },
        layers: [],
        width: this.width,
        height: this.height,
        debug: true,
        autoResizeDrawingBuffer: false,
        useDevicePixels: false
      });

      this.running = true;
      this.complete = false;
      this.sceneIndex = 0;
      this._renderNextScene();
    }
  }, {
    key: 'stop',
    value: function stop() {
      this.running = false;
    }

    // PRIVATE METHODS

  }, {
    key: '_onAfterRender',
    value: function _onAfterRender(promise, scene, _ref2) {
      var _this = this;

      var gl = _ref2.gl;

      getImageFromContext(gl).then(function (image) {
        image.style.mixBlendMode = 'difference';
        var params = Object.assign({ gl: gl, image: image }, scene);
        return _this.onSceneRendered(params);
      }).then(function (x) {
        return promise.resolve(x);
      }).catch(function (error) {
        return promise.reject(error);
      });
    }
  }, {
    key: '_renderScene',
    value: function _renderScene(scene) {
      var _this2 = this;

      var viewState = scene.viewState,
          layers = scene.layers,
          views = scene.views,
          viewports = scene.viewports,
          _scene$timeout = scene.timeout,
          timeout = _scene$timeout === undefined ? 0 : _scene$timeout;


      var promise = makePromise();

      // Initial render to get layer loading
      this.deckgl.setProps({
        layers: layers,
        views: views || [new MapView()],
        viewState: viewState,
        onAfterRender: function onAfterRender() {},

        // DEPRECATED - temporary 5.1 compatibility, remove when 5.2 is released
        viewports: viewports
      });

      // Render again after a timeout to allow layer to load
      // This time the afterRender function will resolve the promise
      setTimeout(function () {
        _this2.deckgl.setProps({
          layers: layers,
          onAfterRender: _this2._onAfterRender.bind(_this2, promise, scene)
        });
      }, timeout);

      // promise that resolves when scene is rendered
      return promise;
    }
  }, {
    key: '_renderNextScene',
    value: function _renderNextScene() {
      var _this3 = this;

      var scene = this.scenes[this.sceneIndex];
      if (this.sceneIndex >= this.scenes.length) {
        this.running = false;
      } else {
        this.sceneIndex++;
      }

      if (this.running) {
        var params = Object.assign({ index: this.sceneIndex }, scene);
        console.log('Rendering new scene with layers', params); // eslint-disable-line

        this._renderScene(params).then(function () {
          return _this3._renderNextScene();
        }).catch(function () {
          return _this3._renderNextScene();
        });
      } else if (!this.complete) {
        this.complete = true;
        this.onComplete();
      }
    }
  }]);

  return SceneRenderer;
}();

export default SceneRenderer;
//# sourceMappingURL=scene-renderer.js.map