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

var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

// 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.

import { Layer, experimental } from 'deck.gl';
var fp64LowPart = experimental.fp64LowPart;

import { GL, Model, Geometry } from 'luma.gl';

import vs from './bezier-curve-layer-vertex.glsl';
import fs from './bezier-curve-layer-fragment.glsl';

var NUM_SEGMENTS = 40;
var DEFAULT_COLOR = [0, 0, 0, 255];

var defaultProps = {
  strokeWidth: 1,
  fp64: false,
  getSourcePosition: function getSourcePosition(x) {
    return x.sourcePosition;
  },
  getTargetPosition: function getTargetPosition(x) {
    return x.targetPosition;
  },
  getControlPoint: function getControlPoint(x) {
    return x.controlPoint;
  },
  getColor: function getColor(x) {
    return x.color || DEFAULT_COLOR;
  }
};

var BezierCurveLayer = function (_Layer) {
  _inherits(BezierCurveLayer, _Layer);

  function BezierCurveLayer() {
    _classCallCheck(this, BezierCurveLayer);

    return _possibleConstructorReturn(this, (BezierCurveLayer.__proto__ || Object.getPrototypeOf(BezierCurveLayer)).apply(this, arguments));
  }

  _createClass(BezierCurveLayer, [{
    key: 'getShaders',
    value: function getShaders() {
      return { vs: vs, fs: fs, modules: ['picking'] };
    }
  }, {
    key: 'initializeState',
    value: function initializeState() {
      var attributeManager = this.state.attributeManager;

      /* eslint-disable max-len */

      attributeManager.addInstanced({
        instanceSourcePositions: {
          size: 3,
          transition: true,
          accessor: 'getSourcePosition',
          update: this.calculateInstanceSourcePositions
        },
        instanceTargetPositions: {
          size: 3,
          transition: true,
          accessor: 'getTargetPosition',
          update: this.calculateInstanceTargetPositions
        },
        instanceControlPoints: {
          size: 3,
          transition: false,
          accessor: 'getControlPoint',
          update: this.calculateInstanceControlPoints
        },
        instanceColors: {
          size: 4,
          type: GL.UNSIGNED_BYTE,
          transition: true,
          accessor: 'getColor',
          update: this.calculateInstanceColors
        }
      });
      /* eslint-enable max-len */
    }
  }, {
    key: 'updateAttribute',
    value: function updateAttribute(_ref) {
      var props = _ref.props,
          oldProps = _ref.oldProps,
          changeFlags = _ref.changeFlags;

      if (props.fp64 !== oldProps.fp64) {
        var attributeManager = this.state.attributeManager;

        attributeManager.invalidateAll();
      }
    }
  }, {
    key: 'updateState',
    value: function updateState(_ref2) {
      var props = _ref2.props,
          oldProps = _ref2.oldProps,
          changeFlags = _ref2.changeFlags;

      _get(BezierCurveLayer.prototype.__proto__ || Object.getPrototypeOf(BezierCurveLayer.prototype), 'updateState', this).call(this, { props: props, oldProps: oldProps, changeFlags: changeFlags });

      if (props.fp64 !== oldProps.fp64) {
        var gl = this.context.gl;

        this.setState({ model: this._getModel(gl) });
      }
      this.updateAttribute({ props: props, oldProps: oldProps, changeFlags: changeFlags });
    }
  }, {
    key: 'draw',
    value: function draw(_ref3) {
      var uniforms = _ref3.uniforms;
      var strokeWidth = this.props.strokeWidth;


      this.state.model.render(Object.assign({}, uniforms, {
        strokeWidth: strokeWidth
      }));
    }
  }, {
    key: '_getModel',
    value: function _getModel(gl) {
      /*
       *  (0, -1)-------------_(1, -1)
       *       |          _,-"  |
       *       o      _,-"      o
       *       |  _,-"          |
       *   (0, 1)"-------------(1, 1)
       */
      var positions = [];
      for (var i = 0; i <= NUM_SEGMENTS; i++) {
        positions = positions.concat([i, -1, 0, i, 1, 0]);
      }

      var model = new Model(gl, Object.assign({}, this.getShaders(), {
        id: this.props.id,
        geometry: new Geometry({
          drawMode: GL.TRIANGLE_STRIP,
          attributes: {
            positions: new Float32Array(positions)
          }
        }),
        isInstanced: true,
        shaderCache: this.context.shaderCache
      }));
      model.setUniforms({ numSegments: NUM_SEGMENTS });
      return model;
    }
  }, {
    key: 'calculateInstanceSourcePositions',
    value: function calculateInstanceSourcePositions(attribute) {
      var _props = this.props,
          data = _props.data,
          getSourcePosition = _props.getSourcePosition;
      var value = attribute.value,
          size = attribute.size;

      var i = 0;
      data.forEach(function (object) {
        var sourcePosition = getSourcePosition(object);
        value[i + 0] = sourcePosition[0];
        value[i + 1] = sourcePosition[1];
        value[i + 2] = isNaN(sourcePosition[2]) ? 0 : sourcePosition[2];
        i += size;
      });
    }
  }, {
    key: 'calculateInstanceTargetPositions',
    value: function calculateInstanceTargetPositions(attribute) {
      var _props2 = this.props,
          data = _props2.data,
          getTargetPosition = _props2.getTargetPosition;
      var value = attribute.value,
          size = attribute.size;

      var i = 0;
      data.forEach(function (object) {
        var targetPosition = getTargetPosition(object);
        value[i + 0] = targetPosition[0];
        value[i + 1] = targetPosition[1];
        value[i + 2] = isNaN(targetPosition[2]) ? 0 : targetPosition[2];
        i += size;
      });
    }
  }, {
    key: 'calculateInstanceControlPoints',
    value: function calculateInstanceControlPoints(attribute) {
      var _props3 = this.props,
          data = _props3.data,
          getControlPoint = _props3.getControlPoint;
      var value = attribute.value,
          size = attribute.size;

      var i = 0;
      data.forEach(function (object) {
        var controlPoint = getControlPoint(object);
        value[i + 0] = controlPoint[0];
        value[i + 1] = controlPoint[1];
        value[i + 2] = isNaN(controlPoint[2]) ? 0 : controlPoint[2];
        i += size;
      });
    }
  }, {
    key: 'calculateInstanceSourceTargetPositions64xyLow',
    value: function calculateInstanceSourceTargetPositions64xyLow(attribute) {
      var _props4 = this.props,
          data = _props4.data,
          getSourcePosition = _props4.getSourcePosition,
          getTargetPosition = _props4.getTargetPosition;
      var value = attribute.value,
          size = attribute.size;

      var i = 0;
      data.forEach(function (object) {
        var sourcePosition = getSourcePosition(object);
        var targetPosition = getTargetPosition(object);
        value[i + 0] = fp64LowPart(sourcePosition[0]);
        value[i + 1] = fp64LowPart(sourcePosition[1]);
        value[i + 2] = fp64LowPart(targetPosition[0]);
        value[i + 3] = fp64LowPart(targetPosition[1]);
        i += size;
      });
    }
  }, {
    key: 'calculateInstanceColors',
    value: function calculateInstanceColors(attribute) {
      var _props5 = this.props,
          data = _props5.data,
          getColor = _props5.getColor;
      var value = attribute.value,
          size = attribute.size;

      var i = 0;
      data.forEach(function (object) {
        var color = getColor(object);
        value[i + 0] = color[0];
        value[i + 1] = color[1];
        value[i + 2] = color[2];
        value[i + 3] = isNaN(color[3]) ? 255 : color[3];
        i += size;
      });
    }
  }]);

  return BezierCurveLayer;
}(Layer);

export default BezierCurveLayer;


BezierCurveLayer.layerName = 'BezierCurveLayer';
BezierCurveLayer.defaultProps = defaultProps;
//# sourceMappingURL=bezier-curve-layer.js.map