var Sync = require("elegant-sync");
var clone = require("clone");
var interceptors = [];

function Interceptor(weight, id) {
    this._sync = new Sync(this);
    this._execute;
    this._next = {};

    this.complete;
    this._exports = {};
    this.id = id || interceptors.length - 1;
    this.weight = weight;

    for (var i = 0; i < interceptors.length; i++) {
        if (interceptors[i].weight == weight) {
            interceptors.splice(i, 1);
        }
    }
    interceptors.push(this);
}
// class methods
Interceptor.prototype.execute = function (fun) {
    this._execute = fun;
    return this;
};
Interceptor.prototype.start = function () {
    this._sync.start();
};

Interceptor.prototype.$ = function (fun) {
    return this._sync.$(fun, arguments);
};

Interceptor.prototype.ready = function (fun) {
    return this._sync.ready(fun);
};

Interceptor.prototype.export = function (name, val, export_to_theme, by_ref) {
    this._exports[name] = {
        value: val,
        export_to_theme: export_to_theme ? true : false,
        by_ref: by_ref ? true : false
    };
};
Interceptor.prototype.error = function (fun) {
    return this._sync.error(fun);
};

// export the class
var interceptor = module.exports = Interceptor;

// get all interceptors
interceptor.all = function () {
    interceptors.sort(function (a, b) {
        return (a.weight > b.weight) ? 1 : -1;
    });
    return clone(interceptors, true);
};
