/// class ProspectDirector.Web.UI.Canvas.Charts.DuplicateField
/// using ProspectDirector.Web.UI.Canvas.Charts.FieldListCommand

/**
@class DuplicateField
@constructor
@namespace Web.UI.Canvas.Charts
@extends Web.UI.Command
**/
ProspectDirector.Web.UI.Canvas.Charts.DuplicateField = ProspectDirector.Web.UI.Canvas.Charts.FieldListCommand.extend({

    //===================================
    // PUBLIC METHODS
    //===================================

    execute: function () {

        var selection = this.get_editor().get_selection().get_selections().first(),
            targetData = this._getFieldData(selection.id),
            duplicate = null;

        if (targetData) {

            duplicate = $.extend(true, {}, targetData, {
                id: uuid.v4()
            });

            this.get_editor().get_selection().clear();
            this.get_editor().add(duplicate);

        }

    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _getFieldData: function (id) {

        var result = null,
            fieldType = this.get_editor().get_fieldType(),
            fields = this.get_feature().getValue(fieldType),
            fieldsList = new ProspectDirector.Web.List(fields);

        fieldsList.each(function (index, field) {
            if (id === field.id) {
                result = field;
            }
        });

        return result;

    }

});