/**
 * Parses a Swarm spec and returns its data as an object.
 * @param spec
 * @returns {{source: *, op: *}}
 */
"use strict";

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

function parseSpec(spec) {
  // spec seems to have some internal parsing state "index" which prevents accessing it consistently
  // https://github.com/gritzko/swarm/issues/53
  var oldIndex = spec.index;
  spec.index = 0;
  var source = undefined;
  try {
    source = spec.source();
  } catch (e) {
    source = null;
  }
  var op = undefined;
  try {
    op = spec.op();
  } catch (e) {
    op = null;
  }
  spec.index = oldIndex;
  return {
    source: source,
    op: op
  };
}

/**
 * Returns the source from within a Swarm spec.
 * @param spec
 * @returns {*}
 */

function sourceOf(spec) {
  return parseSpec(spec).source;
}