"use strict";

/**
 * @param {object} options
 */

var Marcssist = function(options) {
  this._options = options;
  this._id = 0;
};


/**
 * Flatten all styles to objects with selector/style pairs.
 *
 * @param {object} style
 * @param {string} selector Optional prefix for class name.
 * @returns {string}
 */

Marcssist.prototype.insert = function(style, selector) {
  var className = "mx__"+(this._id++);
  selector = (selector ? selector+" ." : ".") + className;
  var declarations = this._process(selector, style);
  declarations.forEach(function(decl) {
    this._prefix(decl.style);
  }, this);
  return className;
};


/**
 * Flatten all styles to objects with selector/style pairs.
 *
 * @param {selector} obj
 * @param {object} obj
 * @returns {array}
 */

Marcssist.prototype._process = function(sel, obj) {
  var prop, value, style = {}, styles = [];
  for (prop in obj) {
    value = obj[prop];
    // found an object, recurse with property as added selector
    if (value === Object(value)) {
      styles = styles.concat(
        this._process(sel + prop, value)
      );
    } else {
      style[hyphenate(prop)] = value;
    }
  }
  styles.push({
    selector: sel,
    style: style
  });
  return styles;
};

Marcssist.prototype._prefix = function (style) {
  var value, prop, values;

  for (prop in style) {
    value = style[prop];

    // assign the same values array to all aliased properties
    style[prop] = values = [value];
  }
  return style;
};

function hyphenate(str) {
  return str.replace(/[A-Z]/g, function($0) { return '-'+$0.toLowerCase() });
}

@@ -1293,16 +1293,16 @@
 op, 
-values;%0A
+pfxProp;
 %0A  f
@@ -1351,82 +1351,62 @@
 p%5D;%0A
-%0A
     
-// assign the same values array to all aliased properties
+if (pfxProp = prefixProp(prop)) %7B
 %0A    
+  
 style%5Bp
+fxP
 rop%5D
@@ -1417,20 +1417,15 @@
 alue
-s = %5Bvalue%5D;
+;%0A    %7D
 %0A  %7D
@@ -1551,8 +1551,540 @@
 ) %7D);%0A%7D%0A
+%0A%0Avar prefixRe = /%5E(-%5Ba-z%5D+-)(.*)/;%0Avar prefixedProps = %7B%7D;%0Avar prefix = null;%0Afunction updatePrefixedProps() %7B%0A  var style = window.getComputedStyle(document.documentElement);%0A  var l = style.length, match, i;%0A  for (i = 0; i %3C l; i++) %7B%0A    if ((match = style%5Bi%5D.match(prefixRe)) && !(match%5B2%5D in style)) %7B%0A      prefix %7C%7C (prefix = match%5B1%5D);%0A      prefixedProps%5Bmatch%5B2%5D%5D = true;%0A    %7D%0A  %7D%0A%7D%0A%0Afunction prefixProp(prop) %7B%0A  if (prefix === null) updatePrefixedProps();%0A  if (prefixedProps%5Bprop%5D) %7B%0A    return prefix + prop;%0A  %7D%0A%7D%0A

Add auto prefixing of properties