function outData = transformSerialConfiguration(inData)
% TRANSFORMSERIALCONFIGURATION 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 ~ismember(ParameterName{idx}, {'ComPort', 'ObjConstructor'})
        % Throw warning if Parity was 'mark' or 'space' as they are not supported from R2021a.
        if strcmpi(ParameterName{idx}, 'Parity')
            if ismember(ParameterValue{idx}, {'mark', 'space'})
                warning(message('instrument:instrumentblks:invalidParity',ParameterValue{idx}))
            end
        end

        if strcmpi(ParameterName{idx}, 'ComPortMenu')
            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;
        else
            % Assign the parameter name.
            outData.NewInstanceData(outIdx).Name = ParameterName{idx};
        end
        % Assign new value if the format of old value is changed.
        if strcmpi(ParameterName{idx}, 'ByteOrder')
            if strcmpi(ParameterValue{idx}, 'LittleEndian')
                outData.NewInstanceData(outIdx).Value = 'little-endian';
            elseif strcmpi(ParameterValue{idx}, 'BigEndian')
                outData.NewInstanceData(outIdx).Value = 'big-endian';
            end
        else
            % If there is no change in parameter, just copy the value.
            outData.NewInstanceData(outIdx).Value = ParameterValue{idx};
        end

        % Increase index for outData structure.
        outIdx = outIdx + 1;
    else
        if strcmpi(ParameterName{idx}, 'Port')
            outData.NewInstanceData(outIdx).Name = 'Port';
            outData.NewInstanceData(outIdx+1).Name = 'PortSel'; 
            if strcmpi(ParameterValue{idx}, '<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
            % Increase index for outData structure for new parameter.
            outIdx = outIdx + 1;
        else
            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
