'use strict';

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

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

/**
 * Offset key parser regex.
 */

var PARSER = /^(\w+)(?::(\d+)-(\d+))?$/;

/**
 * Offset key attribute name.
 */

var ATTRIBUTE = 'data-offset-key';
var SELECTOR = '[' + ATTRIBUTE + ']';

/**
 * From a `node`, find the closest parent's offset key.
 *
 * @param {Node} node
 * @return {String} key
 */

function findKey(node) {
  if (node.nodeType == 3) node = node.parentNode;

  // If a parent with an offset key exists, use it.
  var parent = node.closest(SELECTOR);
  if (parent) return parent.getAttribute(ATTRIBUTE);

  // Otherwise, if a child with an offset key exists, use it.
  var child = node.querySelector(SELECTOR);
  if (child) return child.getAttribute(ATTRIBUTE);

  // Otherwise, move up the tree looking for cousin offset keys in parents.
  while (node = node.parentNode) {
    var cousin = node.querySelector(SELECTOR);
    if (cousin) return cousin.getAttribute(ATTRIBUTE);
  }

  // Shouldn't get here... else we have an edge case to handle.
  throw new Error('No offset key found for node.');
}

/**
 * From a `node` and `offset`, find the closest parent's point.
 *
 * @param {Node} node
 * @param {Offset} offset
 * @return {String} key
 */

function findPoint(node, offset) {
  var key = findKey(node);
  var parsed = parse(key);

  // Don't let the offset be outside the start and end bounds.
  offset = parsed.start + offset;
  offset = Math.max(offset, parsed.start);
  offset = Math.min(offset, parsed.end);

  return {
    key: parsed.key,
    offset: offset
  };
}

/**
 * Parse an offset key `string`.
 *
 * @param {String} string
 * @return {Object} parsed
 */

function parse(string) {
  var matches = PARSER.exec(string);
  if (!matches) throw new Error('Invalid offset key string "' + string + '".');

  var _matches = _slicedToArray(matches, 4);

  var original = _matches[0];
  var key = _matches[1];
  var start = _matches[2];
  var end = _matches[3];

  start = parseInt(start, 10);
  end = parseInt(end, 10);

  return {
    key: key,
    start: start,
    end: end
  };
}

/**
 * Stringify an offset key `object`.
 *
 * @param {Object} object
 *   @property {String} key
 *   @property {Number} start
 *   @property {Number} end
 * @return {String} key
 */

function stringify(object) {
  return object.key + ':' + object.start + '-' + object.end;
}

/**
 * Export.
 */

exports.default = {
  findKey: findKey,
  findPoint: findPoint,
  parse: parse,
  stringify: stringify
};