module.exports.addExtension = function (resource, extension) {
    var assert = require('../assert');

    if (!resource) {
        throw new Error('resource undefined');
    }

    if (!extension) {
        throw new Error('extension undefined');
    }

    resource.extension = resource.extension || [];
    var hasUrl = assert.extension.hasUrl(extension.url);
    var match = resource.extension.filter(hasUrl);
    if (match.length > 0) {
        var index = resource.extension.indexOf(match[0]);
        resource.extension.splice(index, 1)
    }
    resource.extension.push(extension);

    return resource;
};

module.exports.removeExtension = function (resource, url) {
    var assert = require('../assert');

    if (!resource) {
        throw new Error('resource undefined');
    }

    if (!url) {
        throw new Error('url undefined');
    }

    if (!resource.extension) {
        return resource;
    }

    var hasUrl = assert.extension.hasUrl(url);
    var match = resource.extension.filter(hasUrl);
    if (match.length > 0) {
        var index = resource.extension.indexOf(match[0]);
        resource.extension.splice(index, 1)
    }

    if (resource.extension.length === 0) {
        delete resource.extension;
    }

    return resource;
};