'use strict';

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

var _block = require('../models/block');

var _block2 = _interopRequireDefault(_block);

var _document = require('../models/document');

var _document2 = _interopRequireDefault(_document);

var _state = require('../models/state');

var _state2 = _interopRequireDefault(_state);

var _text = require('../models/text');

var _text2 = _interopRequireDefault(_text);

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

/**
 * Deserialize a plain text `string` to a state.
 *
 * @param {String} string
 * @return {State}
 */

function deserialize(string) {
  var characters = string.split('').map(function (char) {
    return { text: char };
  });

  var text = _text2.default.create({ characters: characters });
  var block = _block2.default.create({
    type: 'paragraph',
    nodes: [text]
  });

  var document = _document2.default.create({ nodes: [block] });
  var state = _state2.default.create({ document: document });
  return state;
}

/**
 * Serialize a `state` to plain text.
 *
 * @param {State} state
 * @return {String}
 */

function serialize(state) {
  return state.blocks.map(function (block) {
    return block.text;
  }).join('\n');
}

/**
 * Export.
 */

exports.default = {
  deserialize: deserialize,
  serialize: serialize
};