function [ outData ] = transformTrajectoryScope( inData )
%transformTrajectoryScope is called during automatic replacement of the Discrete-Time 
% Signal Trajectory Scope with Constellation Diagram blocks in the commsink2 and 
% commviewers2 libraries. This function ensures that Constellation Diagram blocks 
% originating from Discrete-Time Signal Trajectory Scope blocks have 'ShowTrajectory' = true. 

%   Copyright 2014-2023 The MathWorks, Inc.

outData.NewBlockPath = '';
outData.NewInstanceData = [];

instanceData = inData.InstanceData;
% Get the field type 'Name' from instanceData
[ParameterNames{1:length(instanceData)}] = instanceData.Name;

if (~ismember('ShowTrajectory',ParameterNames))
    % ShowTrajectory is not present in old link. Set it to true for automatic
    % block replacement of Discrete-Time Signal Trajectory Scope blocks
    instanceData(end+1).Name = 'ShowTrajectory';
    instanceData(end).Value = 'on';
end
outData.NewInstanceData = instanceData;
TrajBlockParams = {'FigPos', ...;
        'figTitle', ...
        'sampPerSymb', ...
        'offsetEye', ...
        'numTraces', ...
        'LineMarkers', ...
        'LineColors', ...
        'LineStyles', ...
        'fading', ...      
        'AxisGrid', ...
        'xMax', ...
        'yMax', ...
        'inphaseLabel', ...
        'quadratureLabel', ...
        'OpenScopeAtSimStart'};
WebConstellationParams = {'WindowPosition', ...
		'Title', ...
		'SamplesPerSymbol', ...
		'SampleOffset', ...
		'SymbolsToDisplay', ...
		'Marker', ...
		'LineColor', ...
		'LineStyle', ...
		'ColorFading', ...
		'ShowGrid', ...
		'XLimits', ...
		'YLimits', ...
		'XLabel', ...
		'YLabel', ...
		'OpenAtSimulationStart'};
		symDispSrcStruct = [];
for pIdx = 1:numel(TrajBlockParams)
    propIdx = ismember({instanceData.Name},TrajBlockParams{pIdx});
    if any(propIdx)
       if strcmpi(TrajBlockParams{pIdx},'yMin')
           yMaxIdx = ismember({instanceData.Name},'yMax');
           yMaxValue = instanceData(yMaxIdx).Value;
           value = ['[' instanceData(propIdx).Value ',' yMaxValue ']'];
       elseif strcmpi(TrajBlockParams{pIdx},'xMin')
           xMaxIdx = ismember({instanceData.Name},'xMax');
           xMaxValue = instanceData(xMaxIdx).Value;
           value = ['[' instanceData(propIdx).Value ',' xMaxValue ']'];
       elseif strcmpi(TrajBlockParams{pIdx},'numTraces')
	        symDispSrcStruct = struct('Name','SymbolsToDisplaySource','Value','on');
            value = instanceData(propIdx).Value;
       else
           value = instanceData(propIdx).Value;
       end
       mapStruct = struct('Name',WebConstellationParams{pIdx},'Value',value);
       outData.NewInstanceData = [outData.NewInstanceData mapStruct]; 
    end
end
if ~isempty(symDispSrcStruct)
	outData.NewInstanceData = [outData.NewInstanceData symDispSrcStruct]; 
end
% Map Style
styleStruct = struct('Style',[]);
lineStyleIdx = ismember({instanceData.Name},'LineStyles');
if any(lineStyleIdx)
   value = strsplit(strrep(instanceData(lineStyleIdx).Value,' ',''),'|'); 
   styleStruct.Style.LineStyle = value;
end
lineMarkerIdx = ismember({instanceData.Name},'LineMarkers');
if any(lineMarkerIdx)
   value = strsplit(strrep(instanceData(lineMarkerIdx).Value,' ',''),'|'); 
   styleStruct.Style.Marker = value;
end
graphicalSettings = struct('Name','GraphicalSettings','Value',jsonencode(styleStruct));
outData.NewInstanceData = [outData.NewInstanceData graphicalSettings];
end

