
ProspectDirector.Web.UI.FeatureSets.ValidationErrorKey = Class.extend({
});

//================================
// STATIC METHODS
//================================

ProspectDirector.Web.UI.FeatureSets.ValidationErrorKey.getParts = function (path) {

    var result = path.split(".");

    if (result.length === 4) {
        result.pop()
    }

    return result.join(".");

};

ProspectDirector.Web.UI.FeatureSets.ValidationErrorKey.getFeatureProperty = function (id) {

    var parts = id.split("."),
        result = null;

    if (parts.length < 4) {
        return null;
    }

    return parts[3];

};

ProspectDirector.Web.UI.FeatureSets.ValidationErrorKey.partToListItem = function (list, part) {

    var index = ProspectDirector.Web.UI.FeatureSets.ValidationErrorKey.getPartListItemIndex(part),
        result = null;

    if (index > -1) {
        result = list.getAtIndex(index);
    }

    return result;

};

ProspectDirector.Web.UI.FeatureSets.ValidationErrorKey.getPartListItemIndex = function (value) {

    var result = -1,
        matches = null,
        expression = /(.*\[)(.*)(\])/

    matches = expression.exec(value);

    if (matches !== null && matches.length > 2) {
        result = matches[2] * 1;
    }

    return result;

};

ProspectDirector.Web.UI.FeatureSets.ValidationErrorKey.resolve = function (key, featureSet) {

    var parts = null,
        result = null;

    if (key == null) {
        return;
    }

    parts = this.getParts(key).split(".");

    if (parts.length < 3) {
        return null;
    }

    result = ProspectDirector.Web.UI.FeatureSets.ValidationErrorKey.partToListItem(featureSet.get_groups(), parts[1]);

    if (!result) {
        return null;
    }

    result = ProspectDirector.Web.UI.FeatureSets.ValidationErrorKey.partToListItem(result.get_features(), parts[2]);

    return result;

};