'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 _character = require('./character');

var _character2 = _interopRequireDefault(_character);

var _mark = require('./mark');

var _mark2 = _interopRequireDefault(_mark);

var _uid = require('../utils/uid');

var _uid2 = _interopRequireDefault(_uid);

var _immutable = require('immutable');

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

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

/**
 * Default properties.
 */

var DEFAULTS = {
  characters: new _immutable.List(),
  decorations: null,
  key: null,
  cache: null
};

/**
 * Text.
 */

var Text = function (_ref) {
  _inherits(Text, _ref);

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

    return _possibleConstructorReturn(this, Object.getPrototypeOf(Text).apply(this, arguments));
  }

  _createClass(Text, [{
    key: 'decorateCharacters',


    /**
     * Decorate the text node's characters with a `decorator` function.
     *
     * @param {Function} decorator
     * @return {Text} text
     */

    value: function decorateCharacters(decorator) {
      var characters = this.characters;
      var cache = this.cache;

      if (characters == cache) return this;

      var decorations = decorator(this);
      if (decorations == characters) return this;

      return this.merge({
        cache: characters,
        decorations: decorations
      });
    }

    /**
     * Remove characters from the text node from `start` to `end`.
     *
     * @param {Number} start
     * @param {Number} end
     * @return {Text} text
     */

  }, {
    key: 'removeCharacters',
    value: function removeCharacters(start, end) {
      var characters = this.characters;


      characters = characters.filterNot(function (char, i) {
        return start <= i && i < end;
      });

      return this.merge({ characters: characters });
    }

    /**
     * Insert text `string` at `index`.
     *
     * @param {String} string
     * @param {Numbder} index
     * @return {Text} text
     */

  }, {
    key: 'insertText',
    value: function insertText(string, index) {
      var characters = this.characters;

      var prev = index ? characters.get(index - 1) : null;
      var marks = prev ? prev.marks : _mark2.default.createSet();
      var chars = _character2.default.createList(string.split('').map(function (char) {
        return {
          text: char,
          marks: marks
        };
      }));

      characters = characters.slice(0, index).concat(chars).concat(characters.slice(index));

      return this.merge({ characters: characters });
    }
  }, {
    key: 'kind',


    /**
     * Get the node's kind.
     *
     * @return {String} kind
     */

    get: function get() {
      return 'text';
    }

    /**
     * Get the length of the concatenated text of the node.
     *
     * @return {Number} length
     */

  }, {
    key: 'length',
    get: function get() {
      return this.text.length;
    }

    /**
     * Get the concatenated text of the node.
     *
     * @return {String} text
     */

  }, {
    key: 'text',
    get: function get() {
      return this.characters.map(function (char) {
        return char.text;
      }).join('');
    }
  }], [{
    key: 'create',


    /**
     * Create a new `Text` with `properties`.
     *
     * @param {Object} properties
     * @return {Text} text
     */

    value: function create() {
      var properties = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

      if (properties instanceof Text) return properties;
      properties.key = (0, _uid2.default)(4);
      properties.characters = _character2.default.createList(properties.characters);
      properties.decorations = null;
      properties.cache = null;
      return new Text(properties);
    }
  }]);

  return Text;
}(new _immutable.Record(DEFAULTS));

/**
 * Export.
 */

exports.default = Text;