function outData = transformSerialReceive(inData)
%TRANSFORMSERIALRECEIVE constructs outData structure from inData
% to create parameters names and values for blocks when models
% saved in older versions are opened.

% Copyright 2020-2021 The MathWorks, Inc.

% Output parameter variable.
outData.NewBlockPath = '';
outData.NewInstanceData = [];

instanceData = inData.InstanceData;

% Get the field type 'Name' and 'Value' from instanceData
[ParameterName{1:length(instanceData)}] = instanceData.Name;
[ParameterValue{1:length(instanceData)}] = instanceData.Value;

outIdx = 1;

% Run the for loop for all the parameters in inData.
for idx = 1:length(ParameterName)
    
    % Don't copy the parameters if they are removed.
    if ~strcmpi(ParameterName{idx}, 'ObjConstructor')
        
        switch ParameterName{idx}
            case 'Header'
                outData.NewInstanceData(outIdx).Name = 'ToggleHeader';
                outData.NewInstanceData(outIdx+1).Name = ParameterName{idx};
                if isempty(ParameterValue{idx})
                    outData.NewInstanceData(outIdx).Value = 'off';
                    outData.NewInstanceData(outIdx+1).Value = '[]';
                else
                    val = uint8(ParameterValue{idx});
                    outData.NewInstanceData(outIdx).Value = 'on';
                    outData.NewInstanceData(outIdx+1).Value = join(["[", num2str(val), "]"]);
                end
                % Increment outIdx here as there are two parameters
                % corresponding to Header
                outIdx = outIdx + 1;
                
            case 'Terminator'
                % Set the parameter names.
                outData.NewInstanceData(outIdx).Name = ParameterName{idx};
                outData.NewInstanceData(outIdx+1).Name = 'CustomTerminator';
                outData.NewInstanceData(outIdx+2).Name = 'ToggleTerminator';
                % Terminator checkbox will be always 'on' as the Terminator cannot
                % be set to empty in previous versions.
                outData.NewInstanceData(outIdx+2).Value = 'on';
                termStr = ParameterValue{idx};
                
                switch termStr
                    case {'CR (''\r'')' 'LF (''\n'')' 'CR/LF (''\r\n'')' 'NULL (''\0'')'} % Default combo values.
                        outData.NewInstanceData(outIdx).Value = termStr ;
                        outData.NewInstanceData(outIdx+1).Value = '[]';
                    otherwise
                        if isempty(termStr) || strcmpi(termStr, '<none>')
                            outData.NewInstanceData(outIdx).Value = termStr;
                            outData.NewInstanceData(outIdx+1).Value = '[]';
                        else % Custom value.
                            val = uint8(termStr);
                            outData.NewInstanceData(outIdx).Value = 'Custom terminator';
                            outData.NewInstanceData(outIdx+1).Value = join(["[", num2str(val), "]"]);
                        end
                end
                % Increment outIdx by two here as there are three parameters
                % corresponding to Terminator.
                outIdx = outIdx + 2;
                
            case 'DataSize'
                outData.NewInstanceData(outIdx).Name = 'ToggleDataSize';
                outData.NewInstanceData(outIdx+1).Name = ParameterName{idx};
                outData.NewInstanceData(outIdx).Value = 'on';
                outData.NewInstanceData(outIdx+1).Value = ParameterValue{idx};
                outIdx = outIdx + 1;
                
            case 'ComPortMenu'
                % Assign the parameter name and value.
                outData.NewInstanceData(outIdx).Name = 'Port';
                outData.NewInstanceData(outIdx+1).Name = 'PortSel';
                if strcmpi(ParameterValue{idx}, '<Please select a port...>')
                    outData.NewInstanceData(outIdx).Value = '<Select a port...>';
                    outData.NewInstanceData(outIdx+1).Value = '<Select a port...>';
                    
                else
                    outData.NewInstanceData(outIdx).Value = ParameterValue{idx};
                    outData.NewInstanceData(outIdx+1).Value = ParameterValue{idx};
                end
                outIdx = outIdx + 1;
            otherwise
                % Assign the parameter name and value.
                outData.NewInstanceData(outIdx).Name = ParameterName{idx};
                outData.NewInstanceData(outIdx).Value = ParameterValue{idx};
        end
        
        % Increase index for outData structure.
        outIdx = outIdx + 1;
    end
end

% Add BlockInModel parameter also to outIdx. Since the BlockInModel value
% should be 'on' when the block is in model, set it explicitly here.
outData.NewInstanceData(outIdx).Name = 'BlockInModel';
outData.NewInstanceData(outIdx).Value = 'on';

end

