'use strict';

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

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 _deck = require('deck.gl');

var _multiIconLayer = require('./multi-icon-layer/multi-icon-layer');

var _multiIconLayer2 = _interopRequireDefault(_multiIconLayer);

var _fontAtlas = require('./font-atlas');

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

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

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.

var DEFAULT_COLOR = [0, 0, 0, 255];
var TEXT_ANCHOR = {
  start: 1,
  middle: 0,
  end: -1
};
var ALIGNMENT_BASELINE = {
  top: 1,
  center: 0,
  bottom: -1
};
// currently the font family is invisible to the user
var FONT_FAMILY = '"Lucida Console", Monaco, monospace';

var defaultProps = {
  getText: function getText(x) {
    return x.text;
  },
  getPosition: function getPosition(x) {
    return x.coordinates;
  },
  getColor: function getColor(x) {
    return x.color || DEFAULT_COLOR;
  },
  getSize: function getSize(x) {
    return x.size || 32;
  },
  getAngle: function getAngle(x) {
    return x.angle || 0;
  },
  getTextAnchor: function getTextAnchor(x) {
    return x.textAnchor || 'middle';
  },
  getAlignmentBaseline: function getAlignmentBaseline(x) {
    return x.alignmentBaseline || 'center';
  },
  getPixelOffset: function getPixelOffset(x) {
    return x.pixelOffset || [0, 0];
  },
  fp64: false
};

var TextLayer = function (_CompositeLayer) {
  _inherits(TextLayer, _CompositeLayer);

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

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

  _createClass(TextLayer, [{
    key: 'initializeState',
    value: function initializeState() {
      var gl = this.context.gl;

      var _makeFontAtlas = (0, _fontAtlas.makeFontAtlas)(gl, FONT_FAMILY),
          mapping = _makeFontAtlas.mapping,
          texture = _makeFontAtlas.texture;

      this.state = {
        iconAtlas: texture,
        iconMapping: mapping
      };
    }
  }, {
    key: 'updateState',
    value: function updateState(_ref) {
      var props = _ref.props,
          oldProps = _ref.oldProps,
          changeFlags = _ref.changeFlags;

      if (changeFlags.dataChanged || changeFlags.updateTriggersChanged && (changeFlags.updateTriggersChanged.all || changeFlags.updateTriggersChanged.getText || changeFlags.updateTriggersChanged.getPosition)) {
        this.transformStringToLetters();
      }
    }
  }, {
    key: 'transformStringToLetters',
    value: function transformStringToLetters() {
      var _props = this.props,
          data = _props.data,
          getText = _props.getText,
          getPosition = _props.getPosition;

      if (!data || data.length === 0) {
        return;
      }

      var transformedData = data.map(function (val) {
        var text = getText(val);
        var letters = Array.from(text);
        var position = getPosition(val);
        if (!text) {
          return [];
        }
        return letters.map(function (letter, i) {
          return Object.assign({}, val, { text: letter, position: position, index: i, len: text.length });
        });
      }).reduce(function (prev, curr) {
        return [].concat(_toConsumableArray(prev), _toConsumableArray(curr));
      });

      this.setState({ data: transformedData });
    }
  }, {
    key: 'getAnchorXFromTextAnchor',
    value: function getAnchorXFromTextAnchor(textAnchor) {
      if (!TEXT_ANCHOR.hasOwnProperty(textAnchor)) {
        throw new Error('Invalid text anchor parameter: ' + textAnchor);
      }
      return TEXT_ANCHOR[textAnchor];
    }
  }, {
    key: 'getAnchorYFromAlignmentBaseline',
    value: function getAnchorYFromAlignmentBaseline(alignmentBaseline) {
      if (!ALIGNMENT_BASELINE.hasOwnProperty(alignmentBaseline)) {
        throw new Error('Invalid alignment baseline parameter: ' + alignmentBaseline);
      }
      return ALIGNMENT_BASELINE[alignmentBaseline];
    }
  }, {
    key: 'renderLayers',
    value: function renderLayers() {
      var _this2 = this;

      var _state = this.state,
          data = _state.data,
          iconAtlas = _state.iconAtlas,
          iconMapping = _state.iconMapping;


      if (!iconMapping || !iconAtlas || !data) {
        return null;
      }

      var _props2 = this.props,
          getColor = _props2.getColor,
          getSize = _props2.getSize,
          getAngle = _props2.getAngle,
          getTextAnchor = _props2.getTextAnchor,
          getAlignmentBaseline = _props2.getAlignmentBaseline,
          getPixelOffset = _props2.getPixelOffset,
          fp64 = _props2.fp64,
          sizeScale = _props2.sizeScale;


      return [new _multiIconLayer2.default(this.getSubLayerProps({
        id: 'text-multi-icon-layer',
        data: data,
        iconAtlas: iconAtlas,
        iconMapping: iconMapping,
        getIcon: function getIcon(d) {
          return d.text;
        },
        getPosition: function getPosition(d) {
          return d.position;
        },
        getIndexOfIcon: function getIndexOfIcon(d) {
          return d.index;
        },
        getNumOfIcon: function getNumOfIcon(d) {
          return d.len;
        },
        getColor: getColor,
        getSize: getSize,
        getAngle: getAngle,
        getAnchorX: function getAnchorX(d) {
          return _this2.getAnchorXFromTextAnchor(getTextAnchor(d));
        },
        getAnchorY: function getAnchorY(d) {
          return _this2.getAnchorYFromAlignmentBaseline(getAlignmentBaseline(d));
        },
        getPixelOffset: getPixelOffset,
        fp64: fp64,
        sizeScale: sizeScale,
        updateTriggers: {
          getAngle: getAngle,
          getColor: getColor,
          getSize: getSize
        }
      }))];
    }
  }]);

  return TextLayer;
}(_deck.CompositeLayer);

exports.default = TextLayer;


TextLayer.layerName = 'TextLayer';
TextLayer.defaultProps = defaultProps;
//# sourceMappingURL=text-layer.js.map