import { Class, clone } from './lib/objects';

function includeValue(operation) {
  return operation.op !== 'remove';
}

/**
 `Operation` provides a thin wrapper over a JSON Patch operation.

 Operations maintain the standard Patch attributes: `op`, `path`, and `value`.

 Operations are automatically assigned a UUID `id`. They can maintain their
 ancestry in a `log`. In this way, it is possible to determine whether
 operations preceded each other.

 Operations can `spawn` descendants, which automatically adds the parent to
 the child's history.

 @class Operation
 @namespace Orbit
 @param {Object}    [options]
 @param {String}    [options.op] Patch attribute `op`
 @param {String}    [options.path] Patch attribute `path`
 @param {Object}    [options.value] Patch attribute `value`
 @constructor
 */
export default Class.extend({
  op: null,
  path: null,
  value: null,

  init: function(options) {
    options = options || {};

    var path = options.path;
    if (typeof path === 'string') {
      if (path.indexOf('/') === 0) {
        path = path.substr(1);
      }
      if (path.length === 0) {
        path = [];
      } else {
        path = path.split('/');
      }
    }

    this.op = options.op;
    this.path = path;
    if (includeValue(this)) {
      this.value = options.value;
    } else {
      this.value = undefined;
    }
  },

  serialize: function() {
    var serialized = {
      op: this.op,
      path: this.path.join('/')
    };

    if (includeValue(this)) {
      serialized.value = this.value;
    }

    return serialized;
  }
});

@@ -269,288 +269,8 @@
 %60.%0A%0A
- Operations are automatically assigned a UUID %60id%60. They can maintain their%0A ancestry in a %60log%60. In this way, it is possible to determine whether%0A operations preceded each other.%0A%0A Operations can %60spawn%60 descendants, which automatically adds the parent to%0A the child's history.%0A%0A
  @cl

Remove n/a comment about operation ancestry.