function outData = commDVBSAPSKTransform(inData)
%COMMDVBSAPSKTRANSFORM Transform function for DVBS-APSK Blocks from R2019a
%to R2019b

% Copyright 2018-2019 The MathWorks, Inc.

InstanceData = inData.InstanceData;
oldBlockParams = {InstanceData.Name};

% Find MSimulinkS* property name to map to ModOrder
[~, pSSIdx] = find(strcmp('StdSuffix', oldBlockParams));
[~, pFLIdx] = find(strcmp('FrameLength', oldBlockParams));
pSSVal = InstanceData(pSSIdx).Value;
pFLVal = InstanceData(pFLIdx).Value;

if strcmp(pSSVal,'S2X') && strcmp(pFLVal,'Normal')
    pModName = 'MSimulinkS2XN';
elseif strcmp(pSSVal,'SH')
    pModName = 'MSimulinkSH';
else
    pModName =  'MSimulinkS2';
end
[~, pModIdx] = find(strcmp(pModName, oldBlockParams));

% Find CodeIDF* property name to map to CodeIDF
if strcmp(pSSVal,'S2X') || strcmp(pSSVal,'S2')
    pCIDFName = ['CodeIDF' InstanceData(pModIdx).Value pSSVal pFLVal(1)];
else
    pCIDFName = 'CodeIDF16S2N';
end

outData.NewInstanceData = InstanceData;
% Common parameter mapping from old block to new block
commonParamMap = ...
    ...  % Old block param       New block param
    {pModName               'ModOrder';...
    pCIDFName              'CodeIDF'};
for i = 1:size(commonParamMap, 1)
    [~, paramIdx] = find(strcmp(commonParamMap{i,1},{outData.NewInstanceData.Name}));
    if ~isempty(paramIdx)
        outData.NewInstanceData(paramIdx).Name = commonParamMap{i,2};
    end
end
% Remove extra parameters
[~,MOldIdx] = find(strncmpi({outData.NewInstanceData.Name},'MSimulink',9));
if ~isempty(MOldIdx)
    outData.NewInstanceData(MOldIdx) = [];
end

[~,codeIdfOldIList] = find(strncmpi({outData.NewInstanceData.Name},'CodeIDF',7));
[~,codeIdfNewIdx] = find(strcmp({outData.NewInstanceData.Name},'CodeIDF'));

if ~isempty(codeIdfOldIList)
    codeIdfOldIList(codeIdfOldIList == codeIdfNewIdx) = [];
    outData.NewInstanceData(codeIdfOldIList) = [];
end

[~, pOTIdx] = find(strcmp('OutputType', oldBlockParams));
% Demod specific parameter mapping from old block to new block
if ~isempty(pOTIdx)
    [~, pDCTIdx] = find(strcmp('DecisionType', oldBlockParams));
    pOTVal = InstanceData(pOTIdx).Value;
    pDCTVal = InstanceData(pDCTIdx).Value;
    isInt = strcmp(pOTVal,'Integer');
    % Find OutputDataType* property name to map to CodeIDF
    if isInt
        pODTTypeName = ['OutputDataType' pOTVal(1:3)];
    elseif ~isInt && strcmp(pDCTVal,'Hard decision')
        pODTTypeName = ['OutputDataType' pOTVal(1:3)];
    else
        pODTTypeName = 'OutputDataTypeSoft';
    end
    [~, paramIdx] = find(strcmp(pODTTypeName,{outData.NewInstanceData.Name}));
    if ~isempty(paramIdx)
        outData.NewInstanceData(paramIdx).Name = 'OutputDataType';
    end
    [~,outDTypeOldList] = find(strncmpi({outData.NewInstanceData.Name},'OutputDataType',14));
    if ~isempty(outDTypeOldList)
        if ~isempty(paramIdx)
          outDTypeOldList(outDTypeOldList == paramIdx) = [];
        end
        outData.NewInstanceData(outDTypeOldList) = [];
    end
end
outData.NewBlockPath = inData.ForwardingTableEntry.('__slOldName__');
end
